wrkrb
v1.0.9
Published
Worker Bee is a shared utils library
Downloads
1
Readme
Worker Bee
Worker Bee is a collection of commonly used Util methods we find ourselves using across all our projects.
Install
yarn add wrkrb
or
npm install wrkrb --save
Import
import { asCallBack } from 'wrkrb';
or
const { asCallBack } = require('wrkrb');
Methods
These are the methods available and how they are commonly used.
DATE UTILS
formatUTCDateForHTML()
DOCUMENT UTILS
sanitizeFileName(fileName)
HELPER UTILS
asCallBack(promise)
This method de-structures your err and results for ease of use.
const [err, results] = await asCallBack(User.getAll());
if (err) throw err;
return results
pick(obj, [paths])
Creates a new object composed of the picked object properties.
let object = { 'a': 1, 'b': '2', 'c': 3 };
pick(object, ['a', 'c']);
// => { 'a': 1, 'c': 3 }
omit(obj, [paths])
The opposite of pick; this method creates an object and deletes object properties that match the omitList
let object = { 'a': 1, 'b': '2', 'c': 3 };
omit(object, ['a', 'c']);
// => { 'b': '2' }
uniq(array)
Creates a duplicate-free version of an array in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array.
uniq([2, 1, 2, 4, 3, 4]);
// => [2, 1, 4, 3]
trim(string, [chars=whitespace])
Removes leading and trailing whitespace or specified characters from string.
trim(' abc ');
// => 'abc'
trim('-_-abc-_-', '_-');
// => 'abc'
uriBuilder(uri, key, value)
Builds at the end of the uri that has been passed in.
let uri = 'localhost:9000/users/';
uri = uriBuilder(uri, 'search', 'bob');
uri = uriBuilder(uri, 'sort', 'asc');
// => 'localhost:9000/users/?search=bob&sort=asc'
convertTags()
isDeepEqual()
extractYoutubeID(url)
Provided a youtube URL this method will return that video's unique ID.
extractYoutubeID('https://www.youtube.com/watch?v=RGuJga2Gl_k&t=512s');
// => 'RGuJga2Gl_k'
extractYoutubeID('https://www.youtube.com/watch?v=KXJSjte_OAI');
// => 'KXJSjte_OAI'
shadeColor(hex, percent)
Provide a hexcode and percent you'd like to lighten(positive values) or darken(negative values)
shadeColor('005A9C', 10);
// => '#05b90d'
shadeColor('4e0707', -20);
// => '#b35905'
shadeColor('014421', 30);
// => '#1a5501'
NUMBER UTILS
rng(max, min, places)
Random Number Generator for both Integers and Floats with decimal precision.
rng(10)
// => any integer between 10 and 0 including those numbers
rng(10, 5)
// => any integer between 10 and 5 including those numbers
rng(10.1)
// => any float between 10.1 and 0 including those numbers fixed to 2 decimals (default)
rng(10.1, 2.7)
// => any float between 10.1 and 2.7 including those numbers fixed to 2 decimals (default)
rng(10.1, 2.7, 4)
// => any float between 10.1 and 2.7 including those numbers fixed to 4 decimals
rngLength(digits)
Random Number Generator for Integers with a specified length. Return type is string to account for leading zeros.
rngLength(2)
// => '31'
rngLength(4)
// => '8742'
rngLength(10)
// => '0847351238'
STRING UTILS
caseSwap(val, case)
Provide the string you'd like to convert along with the case type you wish to convert to. Types include kebab
, camel
, pascal
, snake
, title
, and sentence
caseSwap('first_name', 'kebab');
// => 'first-name'
caseSwap('lets_see_how_this_goes', 'camel');
// => 'letsSeeHowThisGoes'
caseSwap('there once was a boy', 'pascal');
// => 'ThereOnceWasABoy'
caseSwap('Hello World', 'snake');
// => 'hello_world'
caseSwap('PascalCase', 'title');
// => 'Pascal Case'
caseSwap('once upon a time', 'sentence');
// => 'Once upon a time'
makeEllipsis(val, length = 240)
Provide a string and desired length and this method returns the appropriate length with ellipsis.
let str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
makeEllipsis(str);
// => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis...');
makeEllipsis(str, 3);
// => '...');
makeEllipsis(str, 15);
// => 'Lorem ipsum...');
makeEllipsis(str, 30);
// => 'Lorem ipsum dolor sit amet,...');
makeEllipsis(str, 50);
// => 'Lorem ipsum dolor sit amet, consectetur...');
normalizeCurrency(val, isNegative)
This method takes a currency formatted string and returns the number with decimal.
normalizeCurrency('$9.00');
// => '9.00'
normalizeCurrency('-$9.00');
// => '9.00'
normalizeCurrency('$123,456.00');
// => '123456.00'
formatCurrency(val, [sign])
This method takes any number or string of number and returns a formatted currency string
formatCurrency('9.00');
// => '$9.00'
formatCurrency('-9.00');
// => '-$9.00'
formatCurrency('123456.00');
// => '$123,456.00'
formatCurrency('123456.00', false);
// => '123,456.00'
normalizePhone(val)
This method takes a formatted telephone string and returns the number with no formatting
normalizePhone('123-456');
// => '123456'
normalizePhone('(123) 456-7');
// => '1234567'
normalizePhone('(123) 456-7890', false);
// => '1234567890'
formatPhone(val)
This method takes any number and provides a phone number formatted version.
formatPhone('123456');
// => '123-456'
formatPhone('1234567');
// => '(123) 456-7'
formatPhone('1234567890', false);
// => '(123) 456-7890'
formatPhone('1234567890987', false);
// => '(123) 456-7890'
normalizeSSN(val)
This method takes a formatted SSN string and returns the number with no formatting
normalizeSSN('123-45-6789');
// => '123456789'
formatSSN(val)
This method takes any number and provides a SSN formatted version.
formatSSN('123456789');
// => '123-45-6789'
isSSNValid(val)
This method checks to see if the provided SSN is valid or not.
isSSNValid('856-45-6789');
// => true
isSSNValid('000-45-6789');
// => false
isSSNValid('856-452-6789');
// => false
isSSNValid('856-45-0000');
// => false
normalizeEIN(val)
This method takes a formatted EIN string and returns the number with no formatting
normalizeEIN('12-3456789', false);
// => '123456789'
formatEIN(val)
This method takes any number and provides a EIN formatted version.
formatEIN('123456789', false);
// => '12-3456789'
isEINValid(val)
This method checks to see if the provided EIN is valid or not.
isEINValid('77-4567891');
// => true
isEINValid('78-4567891');
// => false
isEINValid('08-4567891');
// => false
isEINValid('89-4567891');
// => false
stateCodeSwap(val, isDisplay)
This method given a state's 2 digit state code will provide the states full name. This method given a states full name will provide the state's 2 digit code. Pass in a true as the second param to receive capitals.
stateCodeSwap('ms');
// => 'mississippi'
stateCodeSwap('ms', true);
// => 'Mississippi'
stateCodeSwap('mississippi');
// => 'ms'
stateCodeSwap('mississippi', true);
// => 'MS'