HelpersJS is a javascript library based on Laravel helpers. It is just based, contains too many functions and also the lack of some.
$ npm i @devesharp/helpers-js
$ yarn add @devesharp/helpers-js
import { Str, Arr } from '@devesharp/helpers-js';
Str.contains('This is my name', 'my name');
// true
Arr.warp(1);
// [1]
The Arr.chunk()
method creates an array chunk:
import { Arr } from '@devesharp/helpers-js';
Arr.chunk([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 2);
// [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10]]
The Arr.range()
method creates an array containing a range of elements.:
import { Arr } from '@devesharp/helpers-js';
Arr.range(1, 5);
// [1,2,3,4,5]
Arr.range(1, 1);
// [1]
The Arr.compareArray()
method compare two arrays:
import { Arr } from '@devesharp/helpers-js';
Arr.range([0, 1], [0, 1]);
// true
Arr.range([], [1]);
// false
The Arr.wrap()
method wraps the given value in an array. If the given value is already an array it will not be changed:
import { Arr } from '@devesharp/helpers-js';
Arr.wrap('any');
// ['any']
Arr.wrap(0);
// [0]
Arr.wrap([true]);
// [true]
The Arr.collapse()
method collapses an array of arrays into a single array:
import { Arr } from '@devesharp/helpers-js';
Arr.wrap([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
The Obj.add()
method adds a given key / value pair to an array if the given key doesn't already exist in the array or is set to null:
import { Obj } from '@devesharp/helpers-js';
Obj.add({ name: 'Desk' }, 'price', 100);
// {name: 'Desk', price: 100}
The Obj.except()
method removes the given key / value pairs from an array:
import { Obj } from '@devesharp/helpers-js';
Obj.except({name: 'Desk', price: 200}, ['price']);
// {name: 'Desk'}
Obj.except({'value': [1,2,3,4]}, ['value.2', 'value.0']);
// {'value': [2,4]}
Obj.except({'products':[{'desk': {'price': 100}},{'desk': {'price': 100}'desk2': {'price': 200}}]},
'products.desk');
// {'products':[{},{'desk2': {'price': 200}}]}
The Obj.pluck()
method retrieves all of the values for a given key from an array:
import { Obj } from '@devesharp/helpers-js';
Obj.pluck([{ id: 1 }, { id: 2 }, { id: 3 }], 'id');
// [1,2,3]
Obj.pluck([{ developer: { id: 1, name: 'Taylor' } }, { developer: { id: 2, name: 'Abigail' } }], 'developer.name'); // ['Taylor', 'Abigail']
The Obj.divide()
method returns two arrays, one containing the keys, and the other containing the values of the given array:
import { Obj } from '@devesharp/helpers-js';
Obj.divide({ key: 'value' });
// [['key'], ['value']]
Obj.divide({ key: 'value', key2: 'value2' });
// [['key', 'key2'], ['value', 'value2']]
The Obj.get()
method retrieves a value from a deeply nested array using "dot" notation:
import { Obj } from '@devesharp/helpers-js';
Obj.get({ products: { desk: { price: 100 } } }, 'products.desk.price');
// 100
Obj.get({ products: { desk: { price: 100 } } }, 'products.desk.price');
// 100
The Obj.get()
method also accepts a default value, which will be returned if the specific key is not found:
import { Obj } from '@devesharp/helpers-js';
Obj.get({}, 'products.desk.price', 200);
// 200
The Obj.has()
method checks whether a given item or items exists in an array using "dot" notation:
import { Obj } from '@devesharp/helpers-js';
Obj.get({ products: { desk: { price: 100 } } }, 'products.desk.price');
// true
Obj.get({ products: { desk: { price: 100 } } }, 'products.desk.price2');
// false
The Obj.only()
method returns only the specified key / value pairs from the given array:
import { Obj } from '@devesharp/helpers-js';
Obj.only({ name: 'Desk', price: 200 }, ['price']);
// {price: 200}
The Obj.set()
method sets a value within a deeply nested array using "dot" notation:
import { Obj } from '@devesharp/helpers-js';
Obj.set({ products: { desk: { price: 100 } } }, 'products.desk.price', 200);
// {'products':{'desk': {'price': 200}}}
The Obj.dot()
method flattens a multi-dimensional array into a single level array that uses "dot" notation to indicate depth:
import { Obj } from '@devesharp/helpers-js';
Obj.dot({ products: { desk: { price: 100, price2: 200 } } });
// ['products.desk.price': 100, 'products.desk.price2': 200]
The Str.after()
method returns everything after the given value in a string:
import { Str } from '@devesharp/helpers-js';
Str.after('This is my name', 'This is my ');
// 'name'
The Str.before()
method returns everything before the given value in a string:
import { Str } from '@devesharp/helpers-js';
Str.before('This is my name', 'my name');
// 'This is '
The Str.camel()
method converts the given string to camelCase:
import { Str } from '@devesharp/helpers-js';
Str.before('This is my name', 'my name');
// 'This is '