ObjecTools
Useful easy-to-use utilities for JavaScript objects

Table of contents
- Features
- Installation
- Usage
- Examples
.filter()
.map()
.keys, .values, .entries, .length
.find(), .findIndex(), .findKey(), .findValue()
.findLast(), .findLastIndex(), .findLastKey(), .findLastValue()
.indexOf(), .lastIndexOf(), .indexOfKey()
.some(), .every()
.sort(), .sortByValues()
.flip()
- Chain methods
.transpose()
- Usage with Jest
Features
- Easy to use, w/o
modifying object prototype (
o(obj).filter(...))
- Useful methods that are operable on keys, values and indices (see Examples):
filter()
map(), forEach()
find(), findIndex(), findLast(), findLastIndex()
indexOf(), lastIndexOf(), indexOfKey()
some(), every()
sort()
flip(), transpose()
- and properties:
.length
.keys, .values, .entries
- Provides an easy way to chain methods
- Typed keys and values
- No dependency, based on modern JS features
- Memory and processor efficient
- Returns
Set instead
of Array for .keys (
see: Why does Object.keys() return an Array instead of
a Set?)
Installation
npm i objectools
or:
yarn add objectools
Usage
import o from 'objectools'
o({a: 1, b: 2, c: 3}).filter | map |
...
(
...)
Examples
.filter()
o({a: 1, b: 2, c: 3}).filter((value) => value > 1)
o({a: 1, b: 2, c: 3}).filter((_, key, index) => key < 'c' && index > 0)
.map()
o({a: 1, b: 2, c: 3}).map((value) => value * 2)
o({a: 1, b: 2, c: 3}).map((value, key) => [key.toUpperCase(), value - 1])
.keys, .values, .entries, .length
o({a: 1, b: 2, c: 3}).keys
o({a: 1, b: 2, c: 3}).values
o({a: 1, b: 2, c: 3}).entries
o({a: 1, b: 2, c: 3}).length
.find(), .findIndex(), .findKey(), .findValue()
o({a: 1, b: 2, c: 3}).find((value) => value > 1)
o({a: 1, b: 2, c: 3}).findIndex((value) => value > 1)
o({a: 1, b: 2, c: 3}).findKey((value) => value > 1)
o({a: 1, b: 2, c: 3}).findValue((value) => value > 1)
.findLast(), .findLastIndex(), .findLastKey(), .findLastValue()
o({a: 1, b: 2, c: 3}).findLast((value) => value > 1)
o({a: 1, b: 2, c: 3}).findLastIndex((value) => value > 1)
o({a: 1, b: 2, c: 3}).findLastKey((value) => value > 1)
o({a: 1, b: 2, c: 3}).findLastValue((value) => value > 1)
.indexOf(), .lastIndexOf(), .indexOfKey()
o({a: 3, b: 3}).indexOf(3)
o({a: 3, b: 3}).lastIndexOf(3)
o({a: 3, b: 3}).indexOfKey('b')
.some(), .every()
o({a: 1, b: 2, c: 3}).some((value) => value > 1)
o({a: 1, b: 2, c: 3}).every((value) => value > 1)
.sort(), .sortByValues()
o({b: 1, a: 3, c: 2}).sort()
o({b: 1, a: 3, c: 2}).sortByValues()
.flip()
o({a: 'x', b: 'y'}).flip()
Chain methods
o({b: 1, a: 2, c: 3})
.oFilter((value) => value < 3)
.oMap((value) => value * 2)
.sort()
.transpose()
o({
x: {a: 1, b: 2},
y: {a: 3, b: 4},
z: {a: 5, b: 6},
}).transpose()
Usage with Jest
You may need to add this to your "jest.config.js" file:
export default {
transformIgnorePatterns: [
'/node_modules/(?!(objectools)/)',
}
See: https://stackoverflow.com/a/49676319/5318303/#jest-gives-an-error-syntaxerror-unexpected-token-export