semantic ops for Javascript data objects and strings
Just barely getting started. Play with j_ here.
given this string:
x = 'cat.dog.dolphin.shark'
firstItemOf(x) // => 'cat'
lastItemOf(x) // => 'shark'
nthItemOf(x, 3) // => 'dolphin'
allButFirstItemOf(x) // => 'dog.dolphin.shark'
allButLastItemOf(x) // => 'cat.dog.dolphin'
All string ops will auto-detect a period or comma delimiter. If you are using a different delimiter, pass it as the last argument:
x = 'cat|dog|dolphin|shark'
nthItemOf(x, 2, '|') // => 'dog'
Given this array:
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
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
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
queryArrayAllMatches(xmen, 'power', 'po') // => [ { "name": "Nightcrawler", "power": "Teleportation" }, { "name": "Rogue", "power": "Absorbing powers" } ]
Returns an array of unique keys from an array of objects