@dxworks/utils v3.0.0
@dxworks/utils
npm i @dxworks/utils
DX Works' PWA Utility functions and helpers
usage example: import { exportName } from '@dxworks/utils'
Modules
- arrayIsEmpty
- callAll
- camelCaseToDash
- compose
- dashToCamelCase
- escape
- unescape
- get
- hashString
- pipe
- removeWhiteSpace
- _throw
- trueTypeOf
- ueid
arrayIsEmpty()
This function can be used to check whether an array is empty. It will return true if the array argument passed to it is empty and false if it is not.
example
const occupiedArray = [1, 2, 3];
arrayIsEmpty(occupiedArray) // => false
const emptyArray = [];
arrayIsEmpty(emptyArray) // => true
callAll()
This curried function allow you to call each function passed to it with the same arguments. This function is useful when you want to apply the results of DOM event to multiple functions.
example
const handleClick = e => callAll(firstFunc, secondFunc)(e)
button.addEventListener('click', handleClick);
camelCaseToDash()
This function takes a camelCasedString as an argument and returns a dash-cased-version of it.
example
camelCaseToDash('RandomMPark') // => 'random-m-park'
compose()
This curried function to takes multiple functions and composes them right to left with result from the previous function.
example
const minus8 = num => num - 8;
const add10 = num => num + 10;
const multiply10 = num => num * 10;
compose(add2, multiply)(4, 10); // => 42
dashToCamelCase()
This function takes a dash-cased-string as an argument and returns a camelCasedVersion of it.
example
dashToCamelCase('some-Thing-need') // => 'someThingNeed'
escape()
This module is a fork of html-escaper.
example
escape('&<>\'"') // => '&<>'"');
escape('<>\'"&') // => '<>'"&');
unescape()
example
unescape('&<>'"') // => '&<>\'"');
unescape('<>'"&') // => '<>\'"&');
get()
This utility function allows the you to safely retrieve deeply nested values from an object.
example
st('Given deeply nested object and path, Should safely return value', t => {
const obj = {
a: {
b: {
c: 'hello',
d: [1, 2, 3, 'd'],
}
}
};
const getD = get('a', 'b', 'd', 3);
getHello(obj) // => 'hello'
const getHello = get('a', 'b', 'c');
getD(obj) // => 'd'
const getNull = get('a', 'b', 'e');
getNull(obj) // => null
hashString()
This utility function takes a string and return a hashed number. It uses a djb2 hashing function. If it receives and empty string it return null.
example
hashString('test') // => 2090756197
hashString('') // => null
pipe()
This curried function to takes multiple functions and pipes them left to right with result from the previous function.
example
const add2 = num => num + 2;
const multiply = (num1, num2) => num1 * num2;
pipe(multiply, add2)(4, 10); // => 42
removeWhiteSpace()
Use this function to remove white space from a string.
example
removeWhiteSpace(' some random script '); // => 'somerandomscript'
_throw()
We at DX Works tend to enjoy using short-circuit evaluation and nested ternaries in our code. This function allows us to avoid unnecessary if statements when we want to throw an error on some condition.
example
condtion && _throw('You messed up');
trueTypeOf()
Forked from the following article this function takes an object and returns a lower cased string informing you of it's true type.
example
trueTypeOf([]) //=> 'array'
trueTypeOf(new Date()) //=> 'date'
trueTypeOf(function () {}) //=> 'function'
trueTypeOf(/test/i) //=> 'regexp'
trueTypeOf(null) //=> 'null'
trueTypeOf(new Set()) //=> 'set'
ueid()
This function is useful when you need a temporary unique identifier on the client side of a PWA. Simply invoke it and receive back a unique enough ID.
example
Scenario global.date.now() === 1546088160029
ueid() // => 'JQ9GYZA5'
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago