0.2.1 • Published 6 years ago

jl-utils v0.2.1

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

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:

ArgumentTypeRequiredDefaultPoss.
arrayarray []YesN/Aarray []
optionstring " "Nobothboth, left, right, b, l, r
exclusionany, *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:

ArgumentTypeRequiredDefaultPoss.
objectobject {}YesN/Aobject {}
pathstring " "YesN/Akey 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:

ArgumentTypeRequiredDefaultPoss.
objectobject {}YesN/Aobject {}
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:

ArgumentTypeRequiredDefaultPoss.
valueanyYesN/Aany
typestring " ", *array[]YesN/Astring, 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:

ArgumentTypeRequiredDefaultPoss.
numbernumberYesN/Anumber >= 1
jlu.numberToExcelLetter(5) //'E'

excelLetterToNumber(letters)

  • Returns a number representing the Excel column-style letter provided.

Arguments:

ArgumentTypeRequiredDefaultPoss.
lettersstring " "YesN/Astring of letters only
jlu.excelLetterToNumber('aa') //27
0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago