Package Exports
- objectools
- objectools/dist/main.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (objectools) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ObjecTools
Useful easy-to-use utilities to for javascript objects
Features
- Easy to use, w/o
modifying object prototype (
o(obj).filter(...)) - Useful methods that are operable on keys, values and indices (see Example usages):
filter()map(),forEach()find(),findIndex(),findLast(),findLastIndex()indexOf(),lastIndexOf(),indexOfKey()some(),every()sort()flip(),transpose()
- and properties:
.length.keys,.values,.entries
- Provide an easy way to chain methods (see Example usages)
- Typed keys and values
- No dependency, based on modern JS features
- Memory and processor efficient
- Returns
Setinstead ofArrayfor.keys( see: Why doesObject.keys()return anArrayinstead of aSet?)
Installation
npm i objectoolsor:
yarn add objectoolsExample usages:
import o from 'objectools'
const obj = {a: 1, b: 2, c: 3}
o(obj).filter((value) => value > 1) // {b: 2, c: 3}
o(obj).filter((_, key, index) => key < 'c' && index > 0) // {b: 2}
o(obj).map((value) => value * 2) // {a: 2, b: 4, c: 6}
o(obj).map((value, key) => [key.toUpperCase(), value - 1]) // {A: 0, B: 1, C: 2}
o(obj).keys // Set {'a', 'b', 'c'} // Type: `Set<'a' | 'b' | 'c'>`
o(obj).values // [1, 2, 3] // Type: `number[]`
o(obj).entries // [['a', 1], ['b', 2], ['c', 3]] // Type: ['a' | 'b' | 'c', number][]
o(obj).length // 3
o(obj).find((value) => value > 1) // ['b', 2]
o(obj).findIndex((value) => value > 1) // 1
o(obj).findLast((value) => value > 1) // ['c', 3]
o(obj).findLastIndex((value) => value > 1) // 2
o({a: 3, b: 3}).indexOf(3) // 0
o({a: 3, b: 3}).lastIndexOf(3) // 1
o({a: 3, b: 3}).indexOfKey('b') // 1
o(obj).some((value) => value > 1) // true
o(obj).every((value) => value > 1) // false
o({a: 'x', b: 'y'}).flip() // {x: 'a', y: 'b'}
// Chain methods:
o({b: 1, a: 2, c: 3})
.oFilter((value) => value < 3)
.oMap((value) => value * 2)
.sort()
// --> {a: 4, b: 2}
o({
x: {a: 1, b: 2},
y: {a: 3, b: 4},
z: {a: 5, b: 6},
}).transpose()
// Returns:
// {
// a: {x: 1, y: 3, z: 5},
// b: {x: 2, y: 4, z: 6},
// }