What is @nevware21/ts-utils?
@nevware21/ts-utils is a utility library for TypeScript that provides a variety of helper functions and utilities to simplify common programming tasks. It includes features for object manipulation, array operations, type checking, and more.
What are @nevware21/ts-utils's main functionalities?
Object Manipulation
The `objMap` function allows you to transform the values of an object based on a provided mapping function.
const obj = { a: 1, b: 2, c: 3 };
const newObj = objMap(obj, (value, key) => value * 2);
console.log(newObj); // { a: 2, b: 4, c: 6 }
Array Operations
The `arrayMap` function is similar to JavaScript's native `map` function but provides additional type safety and utility.
const arr = [1, 2, 3, 4];
const newArr = arrayMap(arr, (value) => value * 2);
console.log(newArr); // [2, 4, 6, 8]
Type Checking
The `isString` function is a type guard that checks if a value is a string, providing better type safety in TypeScript.
const value = 'hello';
if (isString(value)) {
console.log('Value is a string');
}
Deep Cloning
The `deepClone` function creates a deep copy of an object, ensuring that nested objects are also cloned.
const obj = { a: 1, b: { c: 2 } };
const clonedObj = deepClone(obj);
console.log(clonedObj); // { a: 1, b: { c: 2 } }
Other packages similar to @nevware21/ts-utils
lodash
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and other data types. It is more comprehensive and widely used compared to @nevware21/ts-utils, but it may be heavier in terms of bundle size.
ramda
Ramda is a functional programming library for JavaScript that emphasizes immutability and side-effect-free functions. It offers similar utilities for object and array manipulation but follows a functional programming paradigm.
underscore
Underscore is another utility library that provides a variety of functions for working with arrays, objects, and other data types. It is similar to Lodash but has a smaller feature set and is less frequently updated.
@nevware21 ts-utils
Common JavaScript/TypeScript helper functions for better minification
Description
This is a collection of general JavaScript functions (written in and for TypeScript) to aid with removing code duplication to assist with minification, the provided functions are expected to only rarely be included in their namespaced environment.
Support for standard JavaScript functions (ES5+) that are not support in all environments will be backed by internal polyfill implementations when not available. All of the polyfill functions are tested against the standard native implementations for node, browser and web-worker to ensure compatibility.
Documentation and details
Some polyfills are provided for simple backward compatability to enable the utility functions in older environments (such as ES3 / IE8), you don't have to use or include the provided polyfils (AND they are NOT exported as part of the main module). If you need them you will need to import the "polyfill" file directly or host and load the provided bundle/ts-polyfills-utils.min.js
or provide your own alternatives.
See Browser Support for details.
Quickstart
Install the npm packare: npm install @nevware21/ts-utils --save
And then just import the helpers and use them.
import { isArray, arrForEach, objForEachKey, objHasOwnProperty } from "@nevware21/ts-utils";
export function simpleTest(theValue: any): string[] {
let result: any[] = [];
if (isArray(theValue)) {
arrForEach(theValue, (value, idx) => {
if (objHasOwnProperty(theValue, value)) {
result.push(idx + ":" + value);
}
});
} else {
objForEachKey(theValue, (key, value) => {
if (value) {
result.push(key + "=" + value);
}
});
}
return result;
}
Or checking if a variable is a string
import { isString } from "@nevware21/ts-utils";
function checkString(value: any) {
let ug = 1;
return isString(value);
}
Browser Support
General support is currently set to ES5 supported runtimes higher.
| | | | |
---|
Latest ? | Latest ? | 9+ Full ? 8- polyfills needed | Latest ? | Latest ? |
Note: While some polyfills are provided to "somewhat" support ES3/IE8 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.
Contributing
Read our contributing guide to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes.