it's a toolkit of random and useful functions
install:
yarn add @jmellicker/j_
given this string:
let x = 'cat.dog.dolphin.shark'
j_.firstItemOf(x) // => 'cat'
j_.lastItemOf(x) // => 'shark'
j_.nthItemOf(x, 3) // => 'dolphin'
j_.allButFirstItemOf(x) // => 'dog.dolphin.shark'
j_.allButLastItemOf(x) // => 'cat.dog.dolphin'
j_.randomItemOf(x) // => 'dog' or maybe something else!
All above ops will auto-detect ',' '.' '/' & '|' in that order. For a different delimiter, pass it as the last argument:
let x = 'cat#dog#dolphin#shark'
j_.nthItemOf(x, 2, '#') // => 'dolphin'
j_.toTitleCase(tHIs is wEIrD) // => 'This Is Weird"
j_ array ops only operate on flat object keys in an array.
Given this array:
let xmen = [{
name: 'Nightcrawler',
power: 'Teleportation'
}, {
name: 'Cyclops',
power: 'Optic blast'
}, {
name: 'Rogue',
power: 'Absorbing powers'
}, {
name: 'Wolverine',
power: 'Regeneration'
}]
Searches an array of objects for a whole value for a specified key and returns the index of the first matching element
j_.indexFromArray(xmen, 'power', 'Optic blast') // => 1
Searches an array of objects for a whole value for a specified key and returns the first matching array element
j_.queryArrayFirstMatch(xmen, 'power', 'Optic blast') // => { "name": "Cyclops", "power": "Optic blast" }
Searches an array of objects for a whole or partial value for a specified key and returns all matching array elements
j_.queryArrayAllMatches(xmen, 'power', 'po') // => [ { "name": "Nightcrawler", "power": "Teleportation" }, { "name": "Rogue", "power": "Absorbing powers" } ]
Sorts an array of objects by a key
Returns an array of unique keys from an array of objects