🧱 immunity
DEPRECATED in favor of hex. See https://github.com/eserozvataf/hex for details.
Immunity is a set of helper methods in order to construct a new version of data without mutating/updating existing data structure in-place.
Compared to its alternatives, immunity only provides utility functions instead of delivering new data types/structures as a solution.
Additionally, as a library, Immunity is completely tree-shaking-friendly. Your favorite module bundler can easily inline the functionality you need with no extra configuration, instead of bundling the whole Immunity package.
Execute npm install immunity
or yarn add immunity
to install immunity and its dependencies into your project directory.
appends new item(s) to an array or a generator.
import appendToArray from 'immunity/appendToArray';
const source = [ 'a', 'b' ];
const newOne = appendToArray(source, 'c');
// output: Result: ['a','b','c']
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
appends new item(s) to an object.
import appendToObject from 'immunity/appendToObject';
const source = { a: 1, b: 2 };
const newOne = appendToObject(source, { c: 3 });
// output: Result: {'a':1,'b':2,'c':3}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
copies an instance with its constructor.
import copy from 'immunity/copy';
class dummy {}
const source = new dummy();
const newOne = copy(source);
// output: Result: class dummy {}
console.log('Result:', newOne.constructor);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
skips first n items from an array or a generator.
import dropFromArray from 'immunity/dropFromArray';
const source = [ 'a', 'b', 'c' ];
const newOne = dropFromArray(source, 1);
// output: Result: ['b','c']
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
skips first n items from an object.
import dropFromObject from 'immunity/dropFromObject';
const source = { a: 1, b: 2, c: 3 };
const newOne = dropFromObject(source, 1);
// output: Result: {'b':2,'c':3}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
returns matching items from an array or a generator.
import filterArray from 'immunity/filterArray';
const source = [ 1, 2, 3, 4, 5 ];
const newOne = filterArray(source, x => x <= 3);
// output: Result: [1,2,3]
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
returns matching items from an object.
import filterObject from 'immunity/filterObject';
const source = { a: 1, b: 2, c: 3, d: 4, e: 5 };
const newOne = filterObject(source, x => x <= 3);
// output: Result: {'a':1,'b':2,'c':3}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
creates a new array with the results of calling a provided function on every element in the calling array.
import mapArray from 'immunity/mapArray';
const source = [ 1, 2, 3, 4, 5 ];
const newOne = mapArray(source, x => x - 1);
// output: Result: [0,1,2,3,4]
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
creates a new object with the results of calling a provided function on every element in the calling object.
import mapObject from 'immunity/mapObject';
const source = { a: 1, b: 2, c: 3, d: 4, e: 5 };
const newOne = mapObject(source, (value, key) => ({ [key]: value - 1 }));
// output: Result: {'a':0,'b':1,'c':2,'d':3,'e':4}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
merges two or more arrays into one.
import mergeArrays from 'immunity/mergeArrays';
const source1 = [ 1, 2, 3 ];
const source2 = [ 4, 5 ];
const newOne = mergeArrays(source1, source2);
// output: Result: [1,2,3,4,5]
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
merges two or more objects into one.
import mergeObjects from 'immunity/mergeObjects';
const source1 = { a: 1, b: 2, c: 3 };
const source2 = { d: 4, e: 5 };
const newOne = mergeObjects(source1, source2);
// output: Result: {'a':1,'b':2,'c':3,'d':4,'e':5}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
returns matching and not matching items from an array or a generator.
import pickFromArray from 'immunity/pickFromArray';
const source = [ 1, 2, 3, 4, 5 ];
const newOne = pickFromArray(source, [ 2, 3, 6 ]);
// output: Result: {'items':[2,3],'rest':[1,4,5]}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
returns matching and not matching items from an object.
import pickFromObject from 'immunity/pickFromObject';
const source = { a: 1, b: 2, c: 3, d: 4, e: 5 };
const newOne = pickFromObject(source, [ 'b', 'c', 'f' ]);
// output: Result: {'items':{'b':2,'c':3},'rest':{'a':1,'d':4,'e':5}}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
prepends new item(s) to an array or a generator.
import prependToArray from 'immunity/prependToArray';
const source = [ 'b', 'c' ];
const newOne = prependToArray(source, 'a');
// output: Result: ['a','b','c']
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
prepends new item(s) to an object.
import prependToObject from 'immunity/prependToObject';
const source = { b: 2, c: 3 };
const newOne = prependToObject(source, { a: 1 });
// output: Result: {'a':1,'b':2,'c':3}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
removes first matching item from an array or a generator.
import removeFirstMatchFromArray from 'immunity/removeFirstMatchFromArray';
const source = [ 1, 5, 2, 3, 4, 5 ];
const newOne = removeFirstMatchFromArray(source, x => x === 5);
// output: Result: [1,2,3,4,5]
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
removes first matching item from an object.
import removeFirstMatchFromObject from 'immunity/removeFirstMatchFromObject';
const source = { a: 1, f: 5, b: 2, c: 3, d: 4, e: 5 };
const newOne = removeFirstMatchFromObject(source, x => x === 5);
// output: Result: {'a':1,'b':2,'c':3,'d':4,'e':5}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
removes specified item(s) from an array or a generator.
import removeFromArray from 'immunity/removeFromArray';
const source = [ 1, 2, 3, 4, 5 ];
const newOne = removeFromArray(source, 2, 3);
// output: Result: [1,4,5]
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
removes items with specified key(s) from an object.
import removeKeyFromObject from 'immunity/removeKeyFromObject';
const source = { a: 1, b: 2, c: 3, d: 4, e: 5 };
const newOne = removeKeyFromObject(source, 'b', 'c');
// output: Result: {'a':1,'d':4,'e':5}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
removes items with specified value(s) from an object or a generator.
import removeValueFromObject from 'immunity/removeValueFromObject';
const source = { a: 1, b: 2, c: 3, d: 4, e: 5 };
const newOne = removeValueFromObject(source, 2, 3);
// output: Result: {'a':1,'d':4,'e':5}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
reverses an array or a generator content.
import reverseArray from 'immunity/reverseArray';
const source = [ 1, 2, 3, 4, 5 ];
const newOne = reverseArray(source);
// output: Result: [5,4,3,2,1]
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
reverses an object content.
import reverseObject from 'immunity/reverseObject';
const source = { a: 1, b: 2, c: 3, d: 4, e: 5 };
const newOne = reverseObject(source);
// output: Result: {'e':5,'d':4,'c':3,'b':2,'a':1}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
splits an array or a generator content from specified index.
import splitArray from 'immunity/splitArray';
const source = [ 1, 2, 3, 4, 5 ];
const newOne = splitArray(source, 3);
// output: Result: {'items':[1,2,3],'rest':[4,5]}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
splits an array or a generator content from specified last index.
import splitLastArray from 'immunity/splitLastArray';
const source = [ 1, 2, 3, 4, 5 ];
const newOne = splitLastArray(source, 2);
// output: Result: {'items':[4,5],'rest':[1,2,3]}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
splits an object content from specified index.
import splitObject from 'immunity/splitObject';
const source = { a: 1, b: 2, c: 3, d: 4, e: 5 };
const newOne = splitObject(source, 3);
// output: Result: {'items':{'a':1,'b':2,'c':3},'rest':{'d':4,'e':5}}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
splits an object content from specified last index.
import splitLastObject from 'immunity/splitLastObject';
const source = { a: 1, b: 2, c: 3, d: 4, e: 5 };
const newOne = splitLastObject(source, 2);
// output: Result: {'items':{'d':4,'e':5},'rest':{'a':1,'b':2,'c':3}}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
takes first n items from an array or a generator.
import takeFromArray from 'immunity/takeFromArray';
const source = [ 'a', 'b', 'c' ];
const newOne = takeFromArray(source, 2);
// output: Result: ['a','b']
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
takes first n items from an object.
import takeFromObject from 'immunity/takeFromObject';
const source = { a: 1, b: 2, c: 3 };
const newOne = takeFromObject(source, 2);
// output: Result: {'a':1,'b':2}
console.log(`Result: ${JSON.stringify(newOne)}`);
// output: Is Same: false
console.log(`Is Same: ${source === newOne}`);
See GitHub Projects for more.
- node.js (https://nodejs.org/)
Apache 2.0, for further details, please see LICENSE file
See contributors.md
It is publicly open for any contribution. Bugfixes, new features and extra modules are welcome.
- To contribute to code: Fork the repo, push your changes to your fork, and submit a pull request.
- To report a bug: If something does not work, please report it using GitHub Issues.