A collection of useful TypeScript/JavaScript utilities for crypto, date, string, number, JSON operations and more.
npx @tessl/cli install tessl/npm-utility@2.5.0Utility is a comprehensive TypeScript/JavaScript library providing 50+ utility functions for common programming tasks. It offers zero-dependency, high-performance solutions for cryptographic operations, date formatting, string processing, number validation, JSON handling, and more, designed for server-side Node.js applications.
npm install utilityimport { md5, sha256, YYYYMMDD, randomString } from "utility";For CommonJS:
const { md5, sha256, YYYYMMDD, randomString } = require("utility");Namespace import:
import * as utils from "utility";import { md5, sha256, logDate, randomString, base64encode } from "utility";
// Cryptographic operations
const hash = md5("hello world");
const secureHash = sha256("sensitive data", "base64");
// Date formatting
const timestamp = logDate(); // "2025-09-06 12:05:30.123"
const dateOnly = YYYYMMDD(); // "2025-09-06"
// String utilities
const randomId = randomString(16); // Generate 16-char random string
const encoded = base64encode("data to encode");
// Number operations
import { toSafeNumber, random } from "utility";
const safeNum = toSafeNumber("12345678901234567890");
const randomNum = random(10, 100);The utility package is organized into focused modules, each providing related functionality:
Core cryptographic functionality including multiple hash algorithms, HMAC generation, and Base64 encoding/decoding with support for URL-safe variants.
function hash(method: string, s: HashInput, format?: BinaryToTextEncoding): string;
function md5(s: HashInput, format?: BinaryToTextEncoding): string;
function sha256(s: HashInput, format?: BinaryToTextEncoding): string;
function hmac(algorithm: string, key: string, data: string | Buffer, encoding?: BinaryToTextEncoding): string;
function base64encode(s: string | Buffer, urlSafe?: boolean): string;
function base64decode(encodeStr: string, urlSafe?: boolean, encoding?: BufferEncoding | 'buffer'): string | Buffer;
type HashInput = string | Buffer | ArrayBuffer | DataView | object;Comprehensive date formatting utilities with multiple output formats, timezone support, caching for performance, and timestamp conversion operations.
function logDate(d?: Date | string | null, msSep?: string): string;
function YYYYMMDD(d?: Date | string, sep?: string): string;
function YYYYMMDDHHmmss(d?: Date | string | number, options?: YYYYMMDDHHmmssOptions): string;
function accessLogDate(d?: Date): string;
function timestamp(t?: number | string): number | Date;
function getDateFromMilliseconds(milliseconds: number, format?: DateFormat): string;
interface YYYYMMDDHHmmssOptions {
dateSep?: string;
timeSep?: string;
}
enum DateFormat {
DateTimeWithTimeZone = 'DateTimeWithTimeZone',
DateTimeWithMilliSeconds = 'DateTimeWithMilliSeconds',
DateTimeWithSeconds = 'DateTimeWithSeconds',
UnixTimestamp = 'UnixTimestamp',
}String manipulation utilities including random generation, splitting with trimming, HTTP header validation, and character replacement operations.
function randomString(length?: number, charSet?: string): string;
function split(str?: string, sep?: string): string[];
function replace(str: string, substr: string | RegExp, newSubstr: string | StringReplacer): string;
function replaceInvalidHttpHeaderChar(val: string, replacement?: string | Replacement): {val: string, invalid: boolean};
function includesInvalidHttpHeaderChar(val: string): boolean;
type StringReplacer = (substring: string, ...args: any[]) => string;
type Replacement = (char: string) => string;Safe number operations with precision handling, random number generation, and validation for JavaScript's safe integer limits.
function toSafeNumber(s: string | number): number | string;
function isSafeNumberString(s: string): boolean;
function random(lower?: number, upper?: number): number;
const MAX_SAFE_INTEGER: number;
const MIN_SAFE_INTEGER: number;
const MAX_SAFE_INTEGER_STR: string;High-performance object operations including assignment, property enumeration, ownership checking, and clean object creation.
function assign(target: any, objects: any | any[]): any;
function has(obj: object, prop: string): boolean;
function getOwnEnumerables(obj: any, ignoreNull?: boolean): Array<string>;
function map(obj?: any): Record<string, any>;Safe JSON parsing and file I/O operations with proper error handling, directory creation, and configurable formatting options.
function strictJSONParse<T extends object = object>(content: string): T;
function readJSONSync<T = any>(filepath: string): T;
function writeJSONSync(filepath: string, content: string | object, options?: JSONStringifyOptions): void;
function readJSON<T = any>(filepath: string): Promise<T>;
function writeJSON(filepath: string, content: string | object, options?: JSONStringifyOptions): Promise<void>;
interface JSONStringifyOptions {
space?: number | string;
replacer?: (this: any, key: string, value: any) => any;
}Performance optimization utilities including try-catch wrappers, safe property access, and efficient argument handling.
function tryCatch<T = any>(fn: () => T): {error: Error | undefined, value: T | undefined};
function dig(obj?: any, ...keys: string[]): any;
function argumentsToArray(args: any[]): any[];
const UNSTABLE_METHOD: {
try: typeof tryCatch;
};Promise timeout handling with custom error types and wrapper functions for time-limited operations.
class TimeoutError extends Error {
timeout: number;
constructor(timeout: number);
}
function promiseTimeout<T>(promiseArg: Promise<T>, timeout: number): Promise<T>;
function runWithTimeout<T>(scope: () => Promise<T>, timeout: number): Promise<T>;Web-related utilities for HTML escaping/unescaping and safe URI component encoding/decoding with error handling.
function escape(html: string): string;
function unescape(html: string, type?: string): string;
function encodeURIComponent(text: string): string;
function decodeURIComponent(encodeText: string): string;Array manipulation utilities for random slicing and efficient element removal operations.
function randomSlice<T = any>(arr: T[], num?: number): T[];
function spliceOne<T = any>(arr: T[], index: number): T[];Asynchronous file system utilities for checking file existence with proper error handling.
function exists(file: string): Promise<Stats | false>;Function introspection and utility operations including parameter name extraction and no-op functions.
function noop(..._args: any[]): any;
function getParamNames(func: (...args: any[]) => any, cache?: boolean): string[];