A collection of useful TypeScript/JavaScript utilities for crypto, date, string, number, JSON operations and more.
—
Web-related utilities for HTML escaping/unescaping and safe URI component encoding/decoding with error handling, essential for web applications and API development.
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: "<script>alert("xss")</script>"
// User input escaping
const userInput = escape('User said: "Hello & goodbye"');
// Result: "User said: "Hello & goodbye""
// Template usage
const template = `<div>${escape(userContent)}</div>`;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("<div>Hello & goodbye</div>");
// Result: "<div>Hello & goodbye</div>"
// Quote unescaping
const quotes = unescape("He said "Hello"");
// Result: 'He said "Hello"'
// With type parameter
const typed = unescape("&nbsp;", "html");
// Result depends on unescape library implementationSafe 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 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