0.2.2
JavaScript Utilities, see GitHub repo
Install the package using npm:
npm install @pure-function/jsutils --save
or yarn
yarn add @pure-function/jsutils
Array Methods
Replace (part of) an array with items from another array.
(Array)
The array to manipulate.
(Number)
Start index of the part of the array to be replaced, it should
be an integer >= 0.
(Number?)
An optional end index of the part of the array to be replaced, To
remove items, an end index should be >= startIndex value, alternatively
a new array can be inserted by setting end index to null.
(Array
= []
)
An optional array to add in the place of the removed part. This
is optional and defaults to an empty array.
Search for an element in an Array.
(Array)
The array to be searched.
(any)
The element to search for.
(Number
= 0
)
The index to start searching from. It should be an integer >= 0.
(Function?)
A custom equality function to use for the search.
int
:
Element search Index. If element is not found, it returns -1.
ArrayUtils.indexOf(['a', 'ab', 'abc'], 'ab');
// => 1
ArrayUtils.indexOf([[1, 2], null, [1, 3], undefined, 'a'], [1, 3], 0,
(a, b) => ((a === b) || (a && b && a.toString() === b.toString())));
// => 2
Basic JavaScript Language dev utilities
Execute javascript function with given arguments. If the execution fails, return a default value.
(Function)
JavaScript function.
(any)
This can be any value.
(...any)
The arguments to pass to the function.
any
:
function output or default value.
let value = 2
LangUtils.scriptExecute(x => x * x, 0, value) //=> 4
Create a shallow clone object (uses Lodash clone method).
(any)
Accepts any value.
any
:
cloned object.
Create a deep clone object (uses Lodash cloneDeep method).
(any)
Accepts any value.
any
:
deep cloned object.
Performs a deep comparison between two values to determine if they are equivalent (uses Lodash isEqual method).
Note: This method supports comparing arrays, array buffers, booleans,
date objects, error objects, maps, numbers, Object
objects, regexes,
sets, strings, symbols, and typed arrays. Object
objects are compared
by their own, not inherited, enumerable properties. Functions and DOM
nodes are not supported.
(any)
The value to compare.
(any)
The other value to compare.
Boolean
:
true
if the values are equivalent, else
false
.
var object = { 'user': 'fred' };
var other = { 'user': 'fred' };
LangUtils.isEqual(object, other);
// => true
object === other;
// => false
Get a value from a JSON object at a given path.
(Object)
JSON object
any
:
value at given path
LangUtils.query({'a': ['b', 'c']}, 'a[1]');
//=> 'c'
LangUtils.query({'a': ['b', 'c']}, ['a', 1])
//=> 'c'
Node env related dev utilities
Array related dev utilities
Object related dev utilities
Get a value from an object for a given key, if the key is not present return default value.
(Object)
Plain object.
(String)
Key value.
(any
= null
)
Value to be returned if the key is not present in the object.
any
:
Value for the given key from the object, or default value.
Verify if all elements of a given array are plain objects.
Boolean
:
true if array is an array of objects, otherwise false.
ObjectUtils.isPlainObjectArray([{h: 'h'}, [{k: 'k'}]]);
// => false
ObjectUtils.isPlainObjectArray([{h: 'h'}, [{k: 'k'}]], true);
// => true
Merge one object into another.
(Object)
Original object on which merge operation is going to be performed.
(Object)
Object to be merged.
(Boolean
= false
)
Perform a recursive merge.
(Boolean
= false
)
Overide parameters in original object with parameters from
to-be-merged object. By default override is true.
(Boolean
= false
)
Consider keys with null values. By default null values will be
merged.
(Boolean
= false
)
Extend arrays. If true arrays will be extended, if false (default)
arrays will be overridden based on the index.
let toDict = {a: '1', b: {c: '1', e: null}, f: [{g: '1'}] };
let fromDict = {b: {c: '2', d: '2', e: '2'}, f: [{g: '2'}] };
ObjectUtils.merge(toDict, fromDict);
// => toDict is updated as {a: '1', b: {c: '2', d: '2', e: '2'}, f: [{g: '2'}]}
let toDict = {a: '1', b: {c: '1', e: null}, f: [{g: '1'}] };
let fromDict = {b: {c: '2', d: '2', e: '2'}, f: [{g: '2'}] };
ObjectUtils.merge(toDict, fromDict, true, true, true, true);
// => toDict is updated as {a: '1', b: {c: '1', d: '2', e: '2'}, f: [{g: '1'}, {g: '2'}]}
let toDict = {a: '1', b: {c: '1', e: null}, f: [{g: '1'}] };
let fromDict = {b: {c: '2', d: '2', e: '2'}, f: [{g: '2'}] };
ObjectUtils.merge(toDict, fromDict, true, true, false, true);
// => toDict is updated as {a: '1', b: {c: '1', d: '2', e: null}, f: [{g: '1'}, {g: '2'}]}
Promise related dev utilities
String related dev utilities
Creates a new interpolated string with a string template and a data object.
(String)
String template to be interpolated.
(Object)
Data object to use in interpolation.
String
:
Interpolated String.
StringUtils.interpolate('${a} ${b}', {a: 5, b: 3});
// => '5 3'