The TS Utilities is a collection of small common utility functions to simplify common programming tasks. It is designed to be lightweight, efficient, and easy to use.
The functions are fully typescript typed, and implement type-assertion where relevant.
You can install the library using npm:
npm install @mkholt/utilities
or using yarn:
yarn add @mkholt/utilities
Here is an example of how to use the Utilities Library:
import { chunkArray } from "@mkholt/utilities";
// Example usage
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for (const chunk of chunkArray(arr, 3)) {
console.log(chunk)
}
// [1, 2, 3]
// [4, 5, 6]
// [7, 8, 9]
const arr2 = [1, 2, 3, 1, 2, 3, 4, 5]
const unique = arr2.filter(isUnique)
// unique = [1, 2, 3, 4, 5]
- assert:
assert(condition, "error")
: Given a boolean condition, checks the condition and throws and error if it is false. - chunkArray:
chunkArray(arr, size)
: Given an array, returns an iterator which returns the array in chunks of the givensize
- isDefined:
isDefined(obj)
: Returnstrue
if the object is defined and non-null. - isUnique:
isUnique(value, index, arr)
: Returnstrue
if this is the first occurence of thevalue
in the given arrayarr
,false
otherwise.
This project is licensed under the MIT License. See the LICENSE file for details.
For any questions or feedback, please open an issue on GitHub.