CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-utility

A collection of useful TypeScript/JavaScript utilities for crypto, date, string, number, JSON operations and more.

Pending
Overview
Eval results
Files

web.mddocs/

Web Utilities

Web-related utilities for HTML escaping/unescaping and safe URI component encoding/decoding with error handling, essential for web applications and API development.

Capabilities

HTML Escaping

Escape HTML strings to prevent XSS attacks and ensure safe rendering.

/**
 * Escape HTML string to prevent XSS
 * @param html - HTML string to escape
 * @returns Escaped HTML string
 */
function escape(html: string): string;

Usage Examples:

import { escape } from "utility";

// Basic HTML escaping
const safe = escape('<script>alert("xss")</script>');
// Result: "&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;"

// User input escaping
const userInput = escape('User said: "Hello & goodbye"');
// Result: "User said: &quot;Hello &amp; goodbye&quot;"

// Template usage
const template = `<div>${escape(userContent)}</div>`;

HTML Unescaping

Unescape HTML strings back to original form with optional type specification.

/**
 * Unescape HTML string back to original form
 * @param html - Escaped HTML string
 * @param type - Optional unescape type
 * @returns Unescaped HTML string
 */
function unescape(html: string, type?: string): string;

Usage Examples:

import { unescape } from "utility";

// Basic HTML unescaping
const original = unescape("&lt;div&gt;Hello &amp; goodbye&lt;/div&gt;");
// Result: "<div>Hello & goodbye</div>"

// Quote unescaping
const quotes = unescape("He said &quot;Hello&quot;");
// Result: 'He said "Hello"'

// With type parameter
const typed = unescape("&amp;nbsp;", "html");
// Result depends on unescape library implementation

Safe URI Component Encoding

Safe URI component encoding that won't throw errors on invalid input.

/**
 * Safe encodeURIComponent that won't throw errors
 * @param text - Text to encode
 * @returns URL encoded string or original text if encoding fails
 */
function encodeURIComponent(text: string): string;

Usage Examples:

import { encodeURIComponent } from "utility";

// Normal encoding
const encoded = encodeURIComponent("hello world");
// Result: "hello%20world"

// Special characters
const special = encodeURIComponent("user@example.com");
// Result: "user%40example.com"

// Invalid input (won't throw)
const invalid = encodeURIComponent("invalid\uD800");
// Result: "invalid\uD800" (returns original on error)

// Query parameter encoding
const params = `name=${encodeURIComponent(userName)}&email=${encodeURIComponent(userEmail)}`;

Safe URI Component Decoding

Safe URI component decoding that won't throw errors on malformed input.

/**
 * Safe decodeURIComponent that won't throw errors
 * @param encodeText - Encoded text to decode
 * @returns URL decoded string or original text if decoding fails
 */
function decodeURIComponent(encodeText: string): string;

Usage Examples:

import { decodeURIComponent } from "utility";

// Normal decoding
const decoded = decodeURIComponent("hello%20world");
// Result: "hello world"

// Email decoding
const email = decodeURIComponent("user%40example.com");
// Result: "user@example.com"

// Malformed input (won't throw)
const malformed = decodeURIComponent("invalid%");
// Result: "invalid%" (returns original on error)

// Query parameter parsing
function parseQuery(queryString: string): Record<string, string> {
  const params: Record<string, string> = {};
  const pairs = queryString.split('&');
  
  for (const pair of pairs) {
    const [key, value] = pair.split('=');
    if (key && value) {
      params[decodeURIComponent(key)] = decodeURIComponent(value);
    }
  }
  
  return params;
}

const query = parseQuery("name=John%20Doe&email=john%40example.com");
// Result: { name: "John Doe", email: "john@example.com" }

Install with Tessl CLI

npx tessl i tessl/npm-utility

docs

array.md

crypto.md

date.md

fs.md

function.md

index.md

json.md

number.md

object.md

optimize.md

string.md

timeout.md

web.md

tile.json