helper-fns
JavaScript Utilities
INSTALLAION
npm i helper-fns
yarn add helper-fns
Common JavaScript packages and utilities used across my projects.
isEmpty
Checks if the a value is an empty object/collection, has no enumerable properties or is any type that is not considered a collection.
isEmpty([])
isEmpty({})
isEmpty('')
isEmpty([1, 2])
isEmpty({ a: 1, b: 2 })
isEmpty('text')
isEmpty(123)
isEmpty(true)
orderedToken
Generates token based on user defined format
orderedToken("RU-XXXXX")
pick
Picks the key-value pairs corresponding to the given keys from an object.
pick({ a: 1, b: '2', c: 3 }, ['a', 'c'])
omit
Omits the key-value pairs corresponding to the given keys from an object.
omit({ a: 1, b: '2', c: 3 }, ['b'])
sumOfAnArray
Calculates the sum of two or more numbers/arrays.
sumOfAnArray(1, 2, 3, 4)
pipeFunctions
Performs left-to-right function composition.
const add5 = x => x + 5
const multiply = (x, y) => x * y
const multiplyAndAdd5 = pipeFunctions(multiply, add5)
multiplyAndAdd5(5, 2)
renameKeys
Replaces the names of multiple object keys with the values provided.
const obj = { name: 'Bobo', job: 'Front-End Master', shoeSize: 100 }
renameKeys({ name: 'firstName', job: 'passion' }, obj)
objectArrayToArray
Creates an array of key-value pair arrays from an object.
objectToEntries({ a: 1, b: 2 })
clone
Creates a shallow clone of value.
const objects = [{ a: 1 }, { b: 2 }]
const shallow = clone(objects)
console.log(shallow[0] === objects[0])
difference
Calculates the difference between two arrays, without filtering duplicate values.
difference([1, 2, 3, 3], [1, 2, 4])
union
Returns every element that exists in any of the two arrays at least once.
union([1, 2, 3], [4, 3, 2])
isDate
Checks if gicen string is date
console.log(isDate('not-date'))
console.log(isDate('2019-01-10'))
groupBy
Groups the elements of an array based on the given function.
groupBy([6.1, 4.2, 6.3], Math.floor)
groupBy(['one', 'two', 'three'], 'length')
orderBy
Sorts an array of objects, ordered by properties and orders.
const users = [
{ name: 'fred', age: 48 },
{ name: 'barney', age: 36 },
{ name: 'fred', age: 40 },
]
orderBy(users, ['name', 'age'], ['asc', 'desc'])
orderBy(users, ['name', 'age'])
randomNumber
Generates random number of between a min number and a max number.
console.log(randomNumber(0, 6))
enumToString
Generates random number of between a min number and a max number.
export enum AppRoles {
AUTHOR = 'AUTHOR',
ADMIN = 'ADMIN'
}
console.log(enumToString(AppRoles))
randomString
Generates random string of giben length
console.log(randomString(6))
strAfter
Get string after a substring
strAfter('pineapple', 'pine')
strBefore
Get string before a substring
strBefore('pineapple', 'apple')
isNotEmpty
Checks if the a value is not an empty object/collectiom
isNotEmpty([])
isNotEmpty({})
isNotEmpty('')
isNotEmpty([1, 2])
isNotEmpty({ a: 1, b: 2 })
isNotEmpty('text')
isObject
Checks if the passed value is an object or not.
isObject([1, 2, 3, 4])
isObject([])
isObject(['Hello!'])
isObject({ a: 1 })
isObject({})
isObject(true)
fixedDecimal
Get a number after truncating it from the decimal point. No round off is done
fixedDecimal(3.141525, 3)
generateRandomString
Get a random string of defined length
generateRandomString(6)
slugify
Generate a slug from a string
slugify('i love javascript')
capitalizeEveryWord
capitalizeEveryWord('hello world!')
throttle
window.addEventListener(
'resize',
throttle((evt) => {
console.log(window.innerWidth)
console.log(window.innerHeight)
}, 250),
)
unescapeHTML
unescapeHTML('<a href="#">Me & you</a>')
timeTaken
timeTaken(() => 2 ** 10)
formatDuration
formatDuration(1001)
formatDuration(34325055574)
template
template('Hello, {{name}}!', { name: 'world' })
template('Howdy, {{0}}! {{1}}', ['partner', '🤠'])
template('foo: "{{foo}}"; bar: "{{bar}}";', { foo: 123 })
template(
`
Name: {{name.last}}, {{name.first}}
Location: {{address.city}} ({{address.country}})
Hobbies: {{hobbies.0}}, {{hobbies.1}}, {{hobbies.2}}
`,
{
name: {
first: 'Luke',
last: 'Edwards',
},
address: {
city: 'Los Angeles',
country: 'USA',
},
hobbies: ['eat', 'sleep', 'repeat'],
},
)
encrypt and decrypt
console.log(encrypt('hello','32 bytes hex key','16 bytes hex iv'))
// p5HX3eMlroLYJPhXr2zARg==
console.log(decrypt('p5HX3eMlroLYJPhXr2zARg==','32 bytes hex key','16 bytes hex iv'))
// hello
Contributing
Any types of contributions are welcome. Feel free to send pull requests or create issues.
License
Licensed under The MIT License.