A comprehensive TypeScript/JavaScript utility library that provides:
- Cross-Environment Support: Works seamlessly in Node.js, browsers, and web workers
- Helper Functions: Extensive collection of utility functions for common operations
- Type Checking: Robust type checking utilities to improve code reliability
- Optimized for Minification: Function naming designed for better minification
- Zero Dependencies: Lightweight and self-contained
- ECMAScript Support: Supports modern JavaScript features across ES5 to ES2023, ensuring broad usability in various environments
- Polyfills: Uses built-in functions to support older environments, providing modern JavaScript features without sacrificing compatibility
If you find this library useful, please consider sponsoring @nevware21 to support ongoing development and maintenance.
npm install @nevware21/ts-utils --save
Recommended Version Specification:
"@nevware21/ts-utils": ">= 0.12.4 < 2.x"
Note: v0.x and v1.x maintain ES5 compatibility. Future v2.x releases will update the baseline to newer ECMAScript versions.
- Global object detection and access
- Environment detection (Node.js, browser, web worker)
- Safe access to environment-specific APIs
- Comprehensive type checking for all JavaScript types
- Value validation helpers
- Advanced type checks for promises, iterables, and more
- Safe array manipulation with polyfills for older environments
- Cross-environment array utilities
- Performance-optimized implementations
- Property manipulation
- Deep copy/extend
- Object transformation helpers
- Case conversion (camelCase, kebab-case, snake_case)
- String transformation utilities
- HTML and JSON encoding
- Function binding and proxying utilities
- Argument manipulation
- Function creation helpers
- Consistent timing APIs across environments
- Performance measurement utilities
- Scheduling helpers with automatic polyfills
- Customizable timeout handling via package-level and global overrides
For advanced timeout customization options, including global overrides, see our Timeout Overrides Guide.
- Math utilities
- Symbol polyfills
- RegExp helpers
- Iterator utilities
Visit our documentation site for comprehensive guides and references.
- Full API documentation is available in the TypeDoc documentation.
- For practical examples and usage patterns, check out our Usage Guide.
- For detailed documentation on creating and using custom deep copy handlers, see the Advanced Deep Copy Handlers guide.
Category | Documentation |
---|---|
Getting Started | Usage Guide |
Advanced Features | Timeout Overrides, Deep Copy |
Performance | Bundle Size Optimization |
API Reference | TypeDoc Documentation |
Below is a categorized list of all available utilities with direct links to their documentation:
Unless otherwise stated in the functions documentation polyfills are used to automatically backfill unsupported functions in older ES5 runtimes
Using ts-utils helper functions can significantly reduce your bundle size compared to standard JavaScript methods:
// Standard JavaScript - Can't be minified effectively
function stdCode(obj) {
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
if (Object.prototype.hasOwnProperty.call(obj, i)) {
// Do something
}
}
}
}
// Using ts-utils - Allows better minification
import { isArray, arrForEach, objHasOwnProperty } from "@nevware21/ts-utils";
function optimizedCode(obj) {
if (isArray(obj)) {
arrForEach(obj, (value, idx) => {
if (objHasOwnProperty(obj, idx)) {
// Do something
}
});
}
}
When minified, the ts-utils version is significantly smaller as function names can be reduced to single characters.
Write code once that works across all environments without worrying about platform-specific APIs:
import { getGlobal, isNode, isWebWorker } from "@nevware21/ts-utils";
// Safely access the global object in any environment
const globalObj = getGlobal();
// Environment detection
if (isNode()) {
// Node.js specific code
} else if (isWebWorker()) {
// Web Worker specific code
} else {
// Browser specific code
}
Robust type checking utilities to make your code more reliable:
import {
isString, isNumber, isObject, isArray,
isNullOrUndefined, isPromise
} from "@nevware21/ts-utils";
function processValue(value: any) {
if (isString(value)) {
return value.toUpperCase();
} else if (isNumber(value)) {
return value * 2;
} else if (isArray(value)) {
return value.length;
} else if (isPromise(value)) {
return value.then(result => result);
} else if (isNullOrUndefined(value)) {
return "No value provided";
}
return "Unknown type";
}
Support for newer ECMAScript features with backward compatibility:
import {
arrFindLast, arrFindLastIndex, // ES2023
objGetOwnPropertyDescriptors, // ES2017
strPadStart, strPadEnd, // ES2017
isBigInt, // ES2020
isWeakMap, isWeakSet // ES2015+
} from "@nevware21/ts-utils";
// Using ES2023 array methods with backward compatibility
const numbers = [5, 12, 8, 130, 44];
const lastBigNumber = arrFindLast(numbers, num => num > 10); // 44
const lastBigNumberIndex = arrFindLastIndex(numbers, num => num > 10); // 4
// Safe property descriptor access (ES2017)
const descriptors = objGetOwnPropertyDescriptors(myObject);
// String padding (ES2017)
const paddedString = strPadStart("123", 5, "0"); // "00123"
const paddedEnd = strPadEnd("hello", 10, "."); // "hello....."
// Safe type checks for modern types
if (isBigInt(someValue)) {
// Handle BigInt (ES2020) safely
}
This library is thoroughly tested in:
- Node.js (16, 18, 20, 22)
- Modern browsers (via Chromium headless)
- Web Workers (via Chromium headless)
All polyfill functions are tested against native implementations to ensure full compatibility.
The library supports multiple module formats to accommodate different project setups:
- ES Modules (ESM)
- CommonJS (CJS)
- Universal Module Definition (UMD)
This library maintains ES5 compatibility for all v0.x and v1.x releases, ensuring support for any runtime that supports ES5 or higher.
Internal polyfills are used to backfill ES5 functionality which is not provided by older runtimes / browsers.
Starting with v2.x, the library plans to update its baseline to target newer ECMAScript versions, reducing the need for polyfills as browser and runtime support evolves.
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 9+ ✔ |
Internet Explorer support will be dropped in v2.x
All of the included polyfills are tested against the current native implementation running in node
, browser
and worker
environments to ensure that they conform to the current specification, these polyfills are only internally used for ES5 compatibility and when running in an environment (mostly IE) that does not support the required function.
Some additional polyfills are provided for simple backward compatibility to enable the utility functions in older environments, however, you don't have to use or include these provided polyfils. If you need to use them you will need to import the pre-packaged "polyfill" bundle (bundle/ts-polyfills-utils.min.js
) directly by hosting it on your own CDN or all of the non-internal polyfill implementations are exported so you could implement your own version of the polyfill initializer or more simply provide your own alternatives.
Notes:
- While some polyfills are provided to "somewhat" support older environments this library does not intend to become a fully fledged polyfill library. And the polyfills provided (or contributed) are just the minimum set that have been required over time. And should be less necessary are time moves forward.
- Several functions use the Object.defineProperty and therefore support is limited to runtimes or good polyfills that can correctly implement this functionality. (eg. createIterator; createIterable)
Built with TypeScript v5.2.2, with minimal requirements of TypeScript v2.8+ for the type definitions.
The ts-utils library is designed with size optimization as a primary goal. Each function is independently importable and has been optimized for tree-shaking in modern bundlers like Webpack and Rollup. This allows you to include only what you need in your final bundle.
Here's a comparison of bundle sizes when using ts-utils versus standard JavaScript approaches:
Scenario | Standard JS | With ts-utils | Size Reduction |
---|---|---|---|
Basic type checking | ~1.2KB | ~0.3KB | ~75% |
Array operations | ~0.9KB | ~0.4KB | ~55% |
String utilities | ~1.5KB | ~0.6KB | ~60% |
Object helpers | ~2.1KB | ~0.8KB | ~62% |
For detailed byte-level measurements and concrete size optimization strategies, check out our Bundle Size Optimization Guide.
Note: Actual size savings depend on your specific usage patterns, minification settings, and bundler configuration.
You should consider using ts-utils helper functions when:
-
Your code uses the same operations repeatedly: If you're frequently checking types, manipulating arrays/objects, or working with strings, using ts-utils can reduce repetition and improve minification.
-
Bundle size is a concern: For applications where download size matters (e.g., mobile web apps, SPAs), ts-utils can significantly reduce your bundle size through better minification.
-
Cross-environment compatibility is needed: When your code needs to run across Node.js, browsers, and web workers without environment-specific code paths.
-
You want better tree-shaking: The library is designed to allow bundlers to eliminate unused code effectively.
-
Consistent API access is important: ts-utils provides a unified API for accessing functionality that might be implemented differently across environments.
For example, instead of repeating this pattern across your codebase:
// Before: Multiple instances of this pattern across your code
if (typeof value === 'object' && value !== null && Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
if (Object.prototype.hasOwnProperty.call(value, i)) {
// Do something with value[i]
}
}
}
Use ts-utils to make it more concise and minification-friendly:
// After: More concise, better minification
import { isArray, arrForEach, objHasOwnProperty } from "@nevware21/ts-utils";
if (isArray(value)) {
arrForEach(value, (item, idx) => {
if (objHasOwnProperty(value, idx)) {
// Do something with item
}
});
}
For more information on performance and minification benefits, see our Usage Guide.
For detailed documentation on all available utilities, refer to the main documentation site.
MIT