A dangerously convienient utility that injects useful functions into the Object prototype, for provide syntax sugar for many common use cases when working with notive Java script objects.
The functions are primarily copies of Object class methods as well as Array prototype methods that are applied to the values of the object.
Some usefull ones for delteing keys and comparing, spliting and joining objects from values are thrown in for good measure.
NOTE: Messing with Object prototypes is rarely a good idea and using this library may unintended consequences in larger application, however there are many situations where the enhanced readability and reducing on code size could make it worth it.
npm i -save objector
Create a new object with function applied to each value of this.
{a: 1}.map(v => v+1) // {a: 2}
let o = { a: 1 }
o.apply(v => v + 1) // { a: 2 }
o // {a: 2}
{a: 1}.flatMap([k,v] => [[k+1, v+1,[k+2, v+2]]) // { a1: 2, a2: 3 }
{a: 1, b: 0}.flatMap([k,v] => v ? [k, v+1 : []) // { a: 2 }
Object.values
{a: 1}.values // [1]
Object.keys
{a: 1}.keys // ['a']
{a: 1}.entries // [[a,1]]
{a: 1, b: 2}.filter(v => v > 1) // {b: 2}
{a: 1, b: 2}.filter((v,k) => k == 'b') // {b: 2}
{a: 1, b: 2}.filter(v => v > 2) // {}
{a: 1, b: 2}.find(v => v > 1) // 'b'
{a: 1, b: 2}.find(v => v > 2) // null
{ a: 1 }.assign({ a: 2, b: 2 }, { c: 3 }) // { a: 1, b: 2, c: 3}
let o = { a: 1 }
o.merge({ a: 2, b: 2 }, { c: 3 }) // { a: 1, b: 2, c: 3}
o // { a: 1, b: 2, c: 3}
let o = { a: 1, b: 2 }
o.patch({ a: 2 }) // { a: 2}, b: 2 }
{ a: 1, b: 2, c: 3}.delete('a','b') // {c: 3}
{a: 1, b: 2}.find(v => v > 1) // true
{a: 1, b: 2}.find(v => v > 2) // false
{a: 1, b: 2}.find(v => v > 0) // true
{a: 1, b: 2}.find(v => v > 1) // false
{a:1}.toString() // '{"a":1}'
{a:1}.json() // '{"a":1}'
let o1 = { a: 1 }
let o2 = o1.clone() // {a: 1}
o1.a = 2
o2 // { a: a }
Return new Object with values concatenated from Array of objects for each value in values array
let o = { a: 1 }
o.join({ a: 2 }, { a: 3 }) // { a: [1,2,3]}
Return Array of objects for each value in values array
{a: [1,2]}.split() // [{ a: 1}, {a:2}]
{a: 1, b: 2}.contains({a: 1}) // true
{a: 1}.contains({a: 1, b: 2}) // false
{a: 1}.within({a: 1, b: 2}) // true
{a: 1, b: 2}.within({a: 1}) // false
{a: 1}.equals({a: 1}) // true
{a: 1}.equals({a: 2}) // false
{a: 1}.size() // 1