helprjs
A toolkit to help solve challenging tasks with object and arrays. Helprjs is fully documented with live Demos and its really simple to use!
Q&A:
Whilst helprjs is fully tested should you ecounter issues please report them.
Most of the examples here make use of this people object.
const people = [
{ id: 1, firstName: 'John', secondName: 'Smith', age: '61', status: 1 },
{ id: 2, firstName: 'John', secondName: 'West', age: '55', status: true },
{ id: 3, firstName: 'Brenda', secondName: 'Holt', age: '60', status: false },
{ id: 4, firstName: 'Sally', secondName: 'Brampton', age: '33', status: undefined },
{ id: 5, firstName: 'June', secondName: 'Chester', age: '47', status: NaN },
{ id: 6, firstName: 'Jack', secondName: 'Carter', age: '24', status: null },
{ id: 7, firstName: 'Jack', secondName: 'Foster', age: '58', status: 0 },
{ id: 7, firstName: 'Jack', secondName: 'Foster', age: '58', status: 0 },
{ id: 7, firstName: 'Jack', secondName: 'Foster', age: '58', status: 0 },
];
Methods
Methods can be grouped into the following categories:
General : object array manipulation
Array: array only
Boolean : returns true or false
Helpers : deals with values
Numerical : uses numerical data
Vailidation : returns only objects that meet the valid criteria
General
Demo
mergeArraysRemoveDuplicates -merges two arrays and removes all duplicates
Useage:
const array1 = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
const array2 = [{ id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' }];
const mergedArray = mergeArraysRemoveDuplicates(array1, array2);
Demo
deGroup -reverses a grouped object.
deGroup(people, groupObject)
returns 9 objects
Demo
objectToArray -pass in an object
Useage: objectToArray({firstName: 'Mike', lastName: 'Jones', age: 34, registered: true})
Returns ['Mike', 'Jones', 34, true]
Demo
arrayToObject -pass in an array of values
Useage: arrayToObject(["Mike","Jones",34,true])
Returns {0: 'Mike', 1: 'Jones', 2: 34, 3: true}
pass in an array of values and custom keys
Useage: arrayToObject(["Mike","Jones",34,true], ["firstName", "lastName","age","registered"])
Returns {firstName: 'Mike', lastName: 'Jones', age: 34, registered: true}
Demo
addNewProperty -addNewProperty(people, 'registered', true)
adds new key/value to all objects.
Demo
countKeys -countKeys({a:1, b:2, c:3});
// 3
Demo
CSVtoArray -CSVtoArray(csvfile');
// [ {...} {...} ]
Demo
getEven -getEven(people,'age');
returns objects containing only even values
Demo
getObject -getObject(people,2);
returns an object from an array from array key
Demo
getOdd -getOdd(people,'age');
returns objects containing only odd values
Demo
getValues -getValues(people, 'firstName');
// ["John", "John", "Brenda", "Sally", "June", "Jack", "Jack"]
Demo
groupByKey -groupByKey(people, 'firstName')
// {John: Array(2), Brenda: Array(1), Sally: Array(1), June: Array(1), Jack: Array(2)}
Demo
mergeArrays -
mergeArrays(arr1,arr2,key);
returns a single merged array
Demo
randomOrder -randomOrder(people);
returns randomly ordered array
Demo
refine -const result = refine(people, "firstName", "Jack");
// return only objects that match criteria
Demo
removeDuplicates -removeDuplicates(people, 'firstName');
removes all objects containing duplicates values
Demo
removeFalsy -removeFalsy(people, 'status');
removes all objects containing falsy values
Demo
removeValue -removeValue(people,'firstName', 'John');
remove all objects that have this value
Note: 1 and '1' will give a different result, be sure to include quotation marks if targetting a string
Demo
setAllValues -setAllValues(people, 'age', '25');
(sets all values to 25)
Demo
sortByKey -sortByKey(people, 'age', 'desc');
returns object in value order
Note, 3rd paramenter is optional. desc = decending order.
Demo
toArray -toArray(people, 'age');
// ["61","55","60","33","47","24","58"]
Demo
toLower -toLower(people, 'firstName');
returns object with values as lowercase
Demo
toUpper -toUpper(people, 'firstName');
returns object with values as uppercase
Demo
toString -toString(people, 'id');
returns object with values as a string
Demo
toTruncate -toTruncate(people, 'firstName', 3);
returns object with values truncated to numerical value
Demo
toNumber -toNumber(people, 'age');
returns object with values as numbers
Demo
toTrim -toTrim(people, 'firstName');
returns object values with whitespace removed
Array
isEmptyArray
isEmptyArray([1,2]);
false
getAverage
getAverage([1, 2, 3, 4, 4, 4]));
3
Demo
concatArray -pass in an array of arrays
Useage: concatArray([ [1,2],["three", "four"],[5],[6] ])
Returns [1,2,"three","four",5,6]
Demo
populateArray -populateArray(0,20,4)
// [4,8,12,16,20]
Demo
uniqueArray -uniqueArray(["one",1,1,2,3,"two","three","four","one","two"])
// ["one", 1, 2, 3, "two", "three", "four"]
Demo
emptyArray -emptyArray(["one","two","three","four"])
// []
Demo
reverseArray -reverseArray([0,1,2,3,4,5,6])
// [6,5,4,3,2,1,0]
Demo
shuffleArray -shuffleArray([0,1,2,3,4,5,6])
// [4,0,1,6,5,3,2]
Boolean
Demo
isAll -pass in object array, key and function.
const isBelow = (currentValue) => currentValue < 99;
const result = isAll(people, 'age', isBelow)
console.log('result', result)
// true
Demo
isPresent -pass in object array, key and function.
const age = 48;
const isBelow = (currentValue) => currentValue < age;
const result = isPresent(people, "age", isBelow);
console.log('result', result)
// true
Demo
isArray -isArray([1,2,3]);
// true
Demo
isBigint -isBigint(9007199254740991n);
// true
Demo
isBoolean -isBoolean(true);
// true
Demo
isNaN -isNaN(NaN);
// true
Demo
isNull -isNull(null);
// true
Demo
isNumber -isNumber(1);
// true
Demo
isObject -isObject({x:1, y:2});
// true
Demo
isString -isString('abc'});
// true
Demo
isSymbol -isSymbol(Symbol());
// true
Demo
isUndefined -isUndefined(undefined);
// true
Helpers
Demo
percentage -percentage(partial, total)
// percentage(50, 200)
// 25
// calculate percentage of partial against total number
Demo
typeOf -typeOf(value);
// typeOf(1); returns "number"
// typeOf([1,2,3]); returns "array"
// typeOf({x: 1}); returns "object"
Demo
randomId -randomId();
// Random ID generator
// zxrm95d6ug
String
capitalise('hello world')
Returns 'Hello world'
Numerical
Demo
getMaximum -getMaximum(people,'age');
// 61
Demo
getMinimum -getMinimum(people,'age');
// 24
Demo
getTotal -getTotal(people, 'age');
returns sum total
Validation
getValidEmail
getValidEmail( [
{ id: 1, email: 'badEmailDotgmail.com' },
{ id: 2, email: 'test@validEmail.com' },
{ id: 3, email: 'test@badEmail' }
],'email')
returns only objects containing valid email addresses
getValidString
getValidString(people, 'firstName', 5, 99)
//Note: (array, key, minimumLength, maximumLength)
returns only objects containing text within min and max length
Demo
Combination -In this example five methods have been used to generate a desired output.
It is overengineered and an unlikely requirement however it demonstrates
more possibilities when using helprjs.