Skip to content

JasonMejane/ts-manip

Repository files navigation

ts-manip

Manipulate arrays and objects efficiently.

Release   ts-manip on npm   Package size   Licence Dependencies   Issues  
Node.js CI   Coverage  

Install

In terminal, run:

npm i ts-manip

Usage

Import

import * as manip from 'ts-manip';

Methods can also be imported separatly:

import { flattenDeep } from 'ts-manip/flattenDeep';
OR
import { flattenDeep } from 'ts-manip';

Available functions

  • additiveMergeDeep: Performe a deep additive merge on values of an object, on a (key, value) basis
  • filter: Filter elements of an array
  • filteredMap: Map an array to another, thanks to a tranform function, filtering the elements to keep thanks to a predicate function
  • findFirst: Find the first matching element in an array
  • findLast: Find the last matching element in an array
  • flattenDeep: Flatten deeply an array (or up to a given depth), and can transform elements thanks to an optional tranform function
  • forEach: Iterates over elements of an array and invoke a function for each element
  • forEachAsync: Iterates over elements of an array and invoke an asynchronous function for each element
  • groupBy: Group elements of an array, thanks to an iteratee
  • map: Map an array to another, thanks to a tranform function
  • numericDiff: Return the differences of numeric values between 2 objects of the same type
  • reduce: Reduce an array to a new single value, thanks to a tranform function.
  • splitIntoMultiple: Split an array into an array of arrays of length <= to a given max length

Examples

const mapped = manip.map([1, 2, 3], (element) => { return element + 5; });
// mapped = [6, 7, 8];

const filtered = manip.filter(
	[{ id: 'aaa', type: 'a' }, { id: 'bbb', type: 'b' }, { id: 'ccc', type: 'a' }],
	(element) => { return element.type === 'a'; }
);
// filtered = [{ id: 'aaa', type: 'a' }, { id: 'ccc', type: 'a' }];

const difference = manip.numericDiff({ A: 10, B: -20 }, { A: 5, B: 30 });
// difference = { A: -5, B: 50 };

const result = manip.additiveMergeDeep(
	{ A: 5, B: 'part1', C: true, D: [1, 3], E: { val: 10 }, F: 25 },
	{ A: 10, B: '-part2', C: false, D: [5, 6], E: { val: 2, score: 1 } }
);
// result = { A: 15, B: 'part1-part2', C: false, D: [1, 3, 5, 6], E: { val: 12, score: 1 }, F: 25 };

Contribute

Please feel free to suggest features or bug fix through Git issues. Pull Requests for that are also more than welcome.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published