0.2.1 • Published 6 years ago
jl-utils v0.2.1
jl-utils
A bag of tricks for useful JavaScript operations
Install/Initialize:
npm i jl-utils
const jlu = require('jl-utils')
Table of Contents:
Array
trimFalsys(array, option, exclusion)
- Trims an array of falsy values until it reaches the first truthy value, exclusion value, or the array is empty, whichever comes first.
Arguments:
Argument | Type | Required | Default | Poss. |
---|---|---|---|---|
array | array [] | Yes | N/A | array [] |
option | string " " | No | both | both, left, right, b, l, r |
exclusion | any, *array [] | No | **!!no exc!!** | any, *array [] |
const arr = [0, false, 'string', 1, 2, 'null', undefined]
jlu.trimFalsys(arr) //['string', 1, 2, 'null']
jlu.trimFalsys(arr, 'left') //['string', 1, 2, 'null', undefined]
jlu.trimFalsys(arr, null, false) //[false, 'string', 1, 2, 'null']
jlu.trimFalsys(arr, null, [false, undefined]) //[false, 'string', 1, 2, 'null', undefined]
Object
findEntry(object, path)
- Attempts to find the entry to the key path given. If the entry is not found, or there is an error thrown, returns undefined. Note - you can dot-notate array indices as well.
Arguments:
Argument | Type | Required | Default | Poss. |
---|---|---|---|---|
object | object {} | Yes | N/A | object {} |
path | string " " | Yes | N/A | key path in dot notation |
const obj = {
a: 1,
b: {
c: 2,
d: ['a', 'b', 'c'],
e: {
f: 4
}
}
}
jlu.findEntry(obj, 'b.d.2') //'c'
jlu.findEntry(obj, 'b.e.g') //undefined
objDeepCopy(object)
- Copies an object in its entirety and produces a unique object disconnected from the original. Note - this method is not able to copy keyed methods.
Arguments:
Argument | Type | Required | Default | Poss. |
---|---|---|---|---|
object | object {} | Yes | N/A | object {} |
const obj1 = { a: 1, b: 2, c: 3, d: () => {}}
const obj2 = jlu.objDeepCopy(obj1)
obj2.b = 10
console.log(obj1) //{ a: 1, b: 2, c: 3, d: () => {}}
console.log(obj2) //{ a: 1, b: 10, c: 3}
Type Validation
type(value, type)
- Returns a boolean should the value match any type provided. Types provided can be a list within an array, and should any of those types be matched with the value, returns true, otherwise false.
Arguments:
Argument | Type | Required | Default | Poss. |
---|---|---|---|---|
value | any | Yes | N/A | any |
type | string " ", *array[] | Yes | N/A | string, number, array, object, true object, boolean, function, date |
const val1 = 'hello world'
const val2 = []
const val3 = {}
jlu.type(val1, 'string') //true
jlu-type(val2, ['object', 'boolean']) //true
jlu-type(val2, 'true object') //false
jlu-type(val3, 'true object') //true
Miscellaneous
numberToExcelLetter(number)
- Returns an Excel column-style letter based on the number provided.
Arguments:
Argument | Type | Required | Default | Poss. |
---|---|---|---|---|
number | number | Yes | N/A | number >= 1 |
jlu.numberToExcelLetter(5) //'E'
excelLetterToNumber(letters)
- Returns a number representing the Excel column-style letter provided.
Arguments:
Argument | Type | Required | Default | Poss. |
---|---|---|---|---|
letters | string " " | Yes | N/A | string of letters only |
jlu.excelLetterToNumber('aa') //27