Typescript/Javascript utilities for arrays, objects, collections and more.
npm i @celleb/js-utils --save
Utils for arrays
Usage:
import arrays from '@celleb/js-utils/arrays';
// or
import { arrays } from '@celleb/js-utils';
A filter to return unique values in an array
Example:
import { uniqueFilter } from '@celleb/js-utils/arrays';
const cities = ['Windhoek', 'Bloemfontein', 'Windhoek'].filter(uniqueFilter); // ['Windhoek', 'Bloemfontein']
Returns an array of unique values
Example:
import { filterUnique } from '@celleb/js-utils/arrays';
const cities = filterUnique(['Windhoek', 'Bloemfontein', 'Windhoek']); // ['Windhoek', 'Bloemfontein']
Sort function to sort strings in alphabetical order
import { stringSorter } from '@celleb/js-utils/arrays';
['a', 'b', '1', '5'].sort(stringSorter)) // ['1', '5', 'a', 'b'];
Sort function to sort strings in alphabetical order
import { sortStrings } from '@celleb/js-utils/arrays';
sortStrings(['a', 'b', '1', '5']); // ['1', '5', 'a', 'b'];
Sort an array base on the order of the reference array.
import { sortByReference } from '@celleb/js-utils/arrays';
const order = ['faceTime', 'name'];
const input = ['name', 'lastName', 'faceTime', 'taxi'];
sortByReference(input, order)); // ['faceTime', 'name', 'lastName', 'taxi']
Utils for collections. A collection is an array of objects.
Usage:
import coll from '@celleb/js-utils/coll';
// or
import { coll } from '@celleb/js-utils';
Filters for unique items in a collection by the specified key
Example
import { uniqueFilterByKey } from '@celleb/js-utils/coll';
const coll = [
{ id: 1, name: 'Jonas' },
{ id: 1, name: 'Tomanga' },
];
coll.filter(uniqueFilterByKey('id')); // [{id: 1, name: 'Jonas'}]
Returns a collection of items filtered by unique key
Example
import { filterByUniqueKey } from '@celleb/js-utils/coll';
const coll = [
{ id: 1, name: 'Jonas' },
{ id: 1, name: 'Tomanga' },
];
filterByUniqueKey(coll, 'id'); // [{id: 1, name: 'Jonas'}]
Adds or update items of a collection based on the specified key
Example
import { updateColl } from '@celleb/js-utils/coll';
const coll = [
{ id: 1, name: 'Jonas' },
{ id: 2, name: 'Tomanga' },
];
updateColl('id', coll, [{ id: '2', name: 'Manga' }]); // [{id: 1, name: 'Jonas'}, {id: 2, name: 'Manga'}]
Utils for objects.
Usage:
import obj from '@celleb/js-utils/obj';
// or
import { obj } from '@celleb/js-utils';
Returns an object with the specified keys omitted
The first parameter is the object, and the subsequent parameters are the keys to be omitted. There's an autocomplete for keys in typescript.
Example
import { omit } from '@celleb/js-utils/obj';
const data = { 1: 'one', id: 'one', city: 'Opuwo', country: 'Namibia' };
omit(data, 1, 'id'); // {city: 'Opuwo', country: 'Namibia'}
Same as omit
but also accounts for symbol keys
Example
import { omitExtra } from '@celleb/js-utils/obj';
const ID = Symbol('id');
const data = { [ID]: 'one', id: 'one', city: 'Opuwo', country: 'Namibia' };
omitExtra(data, ID, 'id'); // {city: 'Opuwo', country: 'Namibia'}
Transforms the keys of the input object to the values of the given dictionary.
Example
import { shallowTransform } from '@celleb/js-utils/obj';
const input = { town: 'Windhoek', state: 'Namibia' };
const dictionary = { town: 'city', state: 'country' };
shallowTransform(input, dictionary); // {city: 'Windhoek', country: 'Namibia'}
Swaps keys with values in a shallow dictionary.
Example
import { swapKeysAndValues } from '@celleb/js-utils/obj';
const dictionary = { town: 'city', state: 'country' };
swapKeysAndValues(dictionary); // {city: 'town', country: 'state'}
Transforms the input keys to those in a shallow dictionary
Example
import { shallowToDeepTransform } from '@celleb/js-utils/obj';
const input = { town: 'Windhoek', state: 'Namibia', cood: { lat: 1, lon: 2 } };
const dictionary = { town: 'city', state: 'country', 'cood.lat': 'point.x', 'cood.lon': 'point.y' };
shallowToDeepTransform(input, dictionary); // {city: 'Windhoek', country: 'Namibia', point: {x: 1, y: 2} }
Additional utils
Usage:
import utils from '@celleb/js-utils/utils';
// or
import { utils } from '@celleb/js-utils';
Creates and returns a copy of the input
Example
import { clone } from '@celleb/js-utils/obj';
clone({ id: 3 }); // {id: 3}
Returns true
when the stringify
value of two items are identical.
Example
import { compare } from '@celleb/js-utils/obj';
const obj1 = { one: 1 };
const obj2 = { one: 1 };
obj1 === obj2; // false
compare(obj1, obj2); // true