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
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Utility

Utility 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.

Package Information

  • Package Name: utility
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install utility

Core Imports

import { md5, sha256, YYYYMMDD, randomString } from "utility";

For CommonJS:

const { md5, sha256, YYYYMMDD, randomString } = require("utility");

Namespace import:

import * as utils from "utility";

Basic Usage

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);

Architecture

The utility package is organized into focused modules, each providing related functionality:

  • Cryptographic Functions: Hash functions (MD5, SHA1, SHA256, SHA512), HMAC, Base64 encoding/decoding
  • Date Operations: Multiple formatting options, timezone handling, timestamp conversion, caching for performance
  • String Processing: Random generation, splitting, HTTP header validation, character replacement
  • Number Utilities: Safe integer operations, random number generation, precision handling
  • Object Operations: High-performance assignment, property enumeration, clean object creation
  • JSON Handling: Safe parsing, file I/O operations with proper error handling
  • Optimization Utilities: Try-catch wrappers, safe property access, performance optimizations
  • Timeout Management: Promise timeout handling with custom error types
  • Web Utilities: HTML escaping, URI encoding/decoding with error safety
  • File System: Async file existence checking
  • Function Utilities: Parameter introspection, no-op functions

Capabilities

Cryptographic Operations

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;

Cryptographic Operations

Date Formatting and Processing

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',
}

Date Formatting

String Processing

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;

String Processing

Number Operations

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;

Number Operations

Object Utilities

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>;

Object Utilities

JSON Operations

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;
}

JSON Operations

Optimization Utilities

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;
};

Optimization Utilities

Timeout Management

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>;

Timeout Management

Web Utilities

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;

Web Utilities

Array Operations

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[];

Array Operations

File System Operations

Asynchronous file system utilities for checking file existence with proper error handling.

function exists(file: string): Promise<Stats | false>;

File System Operations

Function Utilities

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[];

Function Utilities

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/utility@2.5.x
Publish Source
CLI
Badge
tessl/npm-utility badge