Objectivize is an object utility library.
Objectivize will work on any environment that supports ES6 or higher.
To install the Objectivize library in a Node environment, run
npm install objectivize
Tupos can also be run in a browser environment. The easiest way is to use unpkg and include in a <script>
tag:
<script src="https://unpkg.com/browse/objectivize@2.0.0/lib/objectivize.min.js">
This script will add an objectivize
key to the window
object, with the below API exposed through it. Only v2.0.0 or higher are bundled for a browser.
Recurses through an object given a particular path and returns the value found at the end or a fallback value (if one supplied).
A valid path can be a string, symbol, number, or array of any combination of those three types. If the path is a string, it will be split on any .
to generate an array of keys.
-
object
{Object}: The object to traverse -
path
{string | number | symbol | Array<string | number | symbol>}: The path by which to traverse the object -
notFound
{*}: The value returned if the path does not resolve in the object
-
{*}: The value at the resolved path or the
notFound
value
const obj = {
a: {
b: {
c: 1,
d: 2,
},
},
};
get(obj, 'a'); // { b: { c: 1, d: 2 } }
get(obj, 'a.b'); // { c: 1, d: 2 }
get(obj, ['a', 'b', 'd']); // 2
get(obj, 'c'); // undefined
get(obj, 'c', 'fallback'); // 'fallback'
Recurses down an object to set a value at the end of a particular path. If at any point the path becomes non-existent, it will be generated during the traversal. If at any point the path hits a non-traversable point (e.g., a number), it will abort.
-
object
{Object}: The object to traverse -
path
{string | number | symbol | Array<string | number | symbol>}: The path by which to traverse the object -
value
{*}: The value to be set at the resolved path
-
{boolean}:
true
if the set was successful andfalse
otherwise
const obj = {
a: {
b: {
c: 1,
d: 2,
},
},
};
set(obj, 'a.b', [1, 2, 3]); // { a: { b: [ 1, 2, 3 ] } }
set(obj, 'a.d', true); // { a: { b: { c: 1, d: 2 }, d: true } }
set(obj, 'a.b.c.d', 'value'); // { a: { b: { c: 1, d: 2 } } }
Recurses down an object and passes the particular value into a function and replaces it with that function's return value. It returns true
to indicate success and false
otherwise.
-
object
{Object}: The object to traverse -
path
{string | number | symbol | Array<string | number | symbol>}: The path by which to traverse the object -
updateFn
{Function(*): *}: The function called with the value at the resolved path
-
{boolean}:
true
if the update was successful andfalse
otherwise
const obj = {
a: {
b: {
c: 1,
d: 2,
},
},
};
const double = n => n;
update(obj, 'a.b.c', double); // { a: { b: { c: 2, d: 2 } } }
update(obj, 'a.b.c.d', double); // { a: { b: { c: 2, d: 2 } } }
Recurses down an object and deletes the property at the given path.
-
object
{Object}: The object to traverse -
path
{string | number | symbol | Array<string | number | symbol>}: The path by which to traverse the object
-
{boolean}:
true
if the deletion was successful andfalse
otherwise
const obj = {
a: {
b: {
c: 1,
d: 2,
},
},
};
del(obj, 'a.b.c'); // { a: { b: { d: 2 } } }
Recurses down an object and checks to make sure something exists at the end of the path. The value at the end does not matter; only the existence of the final key is checked.
-
object
{Object}: The object to traverse -
path
{string | number | symbol | Array<string | number | symbol>}: The path by which to traverse the object
-
{boolean}:
true
if the path resolves andfalse
otherwise
const obj = {
a: {
b: {
c: 1,
d: 2,
},
},
};
has(obj, 'a.b.c'); // true
has(obj, 'a.b.c.d'); // false
Given a value and a path, generates a series of nested objects that terminate with the supplied value.
-
value
{*}: The value to set at the end of the path -
path
{string | number | symbol | Array<string | number | symbol>}: The path down which the value should be set
- {Object}: A nested object which terminates at the value
generate(1, 'a.b.c'); // { a: { b: { c: 1 } } }
Deeply merges two objects together. The following method is used in case there is a key collision between the two objects being merged:
- if the values of both are objects, continue merging
- if either one of the values are not objects, pass both into the
onCollision
function
By default, merge
will override the left object's value with the value on the object being merged into it (similar to Object.assign
). This can be configured.
The function signature for onCollision
is
(leftObj, rightObj, key) => any
where leftObj
is the current object being merged into (not necessarily the original object passed as the first parameter to merge
), rightObj
is the current object being merged (again, not necessarily the second parameter passed into merge
), and key
is the key these two objects have in common.
-
target
{Object}: The object being merged into -
source
{Object}: The object being merged -
onCollision
{Function(Object, Object, string | symbol): *}: The function used to determing merge conflicts
- {Object}: The result of the merge operation
const left = {
a: {
b: 1,
},
};
const right = {
a: {
b: 2,
c: 3,
},
};
const onCollision = (left, right, key) => [left[key], right[key]];
merge(left, right); // { a: { b: 2, c: 3 } }
merge(left, right, onCollision); // { a: { b: [1, 2], c: 3 } }
Will take an object and flatten it into an object of paths and values as the key/value pairs. Any time the function could recurse farther down an object, it calls a configurable shouldTraverse
parameter to check if it should.
The function signature for shouldTraverse
is
(value, key, obj) => boolean
where value
is the object to potentially traverse into, key
is its respective key, and obj
is the object passed into the destructure
call.
-
object
{Object}: The object to traverse -
shouldTraverse
{Function(*, string | symbol, Object): boolean}: The path down which the value should be set
- {Object}: A flat object of period-separated paths as keys, and their respective terminal values
const obj = {
a: {
b: {
c: 1,
d: 2,
},
},
};
const onlyOneKey = obj => Object.keys(obj).length === 1;
destructure(obj); // { 'a.b.c': 1, 'a.b.d': 2 }
destructure(obj, onlyOneKey); // { 'a.b': { c: 1, d: 2 } }
Checks if one object deeply contains another.
-
object
{Object}: The main objct -
subObject
{Object}: The object to check if it is contained inobject
-
{boolean}:
true
for containment,false
otherwise
const obj1 = {
a: {
b: {
c: 1,
d: 2,
},
},
};
const obj2 = { b: { c: 1 } };
const obj3 = { c: 2, d: 2 };
contains(obj1, obj2); // true
contains(obj1, obj3); // false
Checks if two objects deeply equal each other.
-
object1
{Object}: An object to check for equality withobject2
-
object2
{Object}: An object to check for equality withobject1
-
{boolean}:
true
for equality,false
otherwise
const obj1 = {
a: {
b: {
c: 1,
d: 2,
},
},
};
const obj2 = {
a: {
b: {
c: 1,
d: 2,
},
},
};
const obj3 = {
a: {
b: 5,
},
};
equals(obj1, obj2); // true
equals(obj1, obj3); // false
Returns all own (enumerable and non-enumerable) keys of a specified object.
-
object
{Object}: The object whose keys should be extracted
- {Array<string | symbol>}: Array of all own keys in the object
const obj = {
a: 1,
b: 2,
[Symbol()]: 3,
};
keys(obj); // [ 'a', 'b', Symbol() ]
Returns all own enumerable keys of a specified object.
-
object
{Object}: The object whose keys should be extracted
- {Array}: Array of all own enumerable keys
const obj = {
a: 1,
b: 2,
[Symbol()]: 3,
};
keys.enum(obj); // [ 'a', 'b' ]
Returns all own string (enumerable and non-enumerable) keys of a specified object.
-
object
{Object}: The object whose keys should be extracted
- {Array}: Array of all own string keys
const obj = {
a: 1,
b: 2,
[Symbol()]: 3,
};
keys.names(obj); // [ 'a', 'b' ]
Returns all own enumerable keys of a specified object.
-
object
{Object}: The object whose keys should be extracted
- {Array}: Array of all own symbol keys
const obj = {
a: 1,
b: 2,
[Symbol()]: 3,
};
keys.symbol(obj); // [ Symbol() ]
Returns all values for own (enumerable and non-enumerable) keys of a specified object.
-
object
{Object}: The object whose keys should be extracted
- {Array<*>}: Array of all own values in the object
const obj = {
a: 1,
b: 2,
[Symbol()]: 3,
};
values(obj); // [ 1, 2, 3 ]
Returns all values for own enumerable keys of a specified object.
-
object
{Object}: The object whose keys should be extracted
- {Array<*>}: Array of all own enumerable values
const obj = {
a: 1,
b: 2,
[Symbol()]: 3,
};
values.enum(obj); // [ 1, 2 ]
Returns values for all own string (enumerable and non-enumerable) keys of a specified object.
-
object
{Object}: The object whose keys should be extracted
- {Array<*>}: Array of values for all own string keys
const obj = {
a: 1,
b: 2,
[Symbol()]: 3,
};
values.names(obj); // [ 1, 2 ]
Returns values for all own enumerable keys of a specified object.
-
object
{Object}: The object whose keys should be extracted
- {Array<*>}: Array of values for all own symbol keys
const obj = {
a: 1,
b: 2,
[Symbol()]: 3,
};
values.symbol(obj); // [ 3 ]
Returns all entries for own (enumerable and non-enumerable) keys of a specified object.
-
object
{Object}: The object whose keys should be extracted
- {Array<[string | symbol, *]>}: Array of all own entries in the object
const obj = {
a: 1,
b: 2,
[Symbol()]: 3,
};
entries(obj); // [ [ 'a', 1] , [ 'b', 2 ], [ Symbol(), 3 ] ]
Returns all entries for own enumerable keys of a specified object.
-
object
{Object}: The object whose keys should be extracted
- {Array<[string, *]>}: Array of all own enumerable entries
const obj = {
a: 1,
b: 2,
[Symbol()]: 3,
};
entries.enum(obj); // [ [ 'a', 1 ], [ 'b', 2 ] ]
Returns entries for all own string (enumerable and non-enumerable) keys of a specified object.
-
object
{Object}: The object whose keys should be extracted
- {Array<[string, *]>}: Array of entries for all own string keys
const obj = {
a: 1,
b: 2,
[Symbol()]: 3,
};
entries.names(obj); // [ [ 'a', 1 ], [ 'b', 2 ] ]
Returns entries for all own enumerable keys of a specified object.
-
object
{Object}: The object whose keys should be extracted
- {Array<[symbol, *]>}: Array of entries for all own symbol keys
const obj = {
a: 1,
b: 2,
[Symbol()]: 3,
};
entries.symbol(obj); // [ [ Symbol(), 3 ] ]
Takes an array of [key, value]
tuples and constructs an object out of them. Works identically to Object.fromEntries
.
-
entries
{Array<[string | symbol | number, *]>}: Array of entries for the object
- {Object}: An object built from the key/value pairs
const entries = [
['key1', 1],
['key2', 2],
];
const object = fromEntries(entries); // { key1: 1, key2: 2 }
Takes an object and a function and manipulates its [key, value]
pairs.
The function signature for mapFn
is
(key, value, obj) => [newKey, newValue]
where key
and value
are the current key/value pairs to change, obj
is the original object, and [newKey, newValue]
is a tuple of what the new entry should be.
-
object
{Object}: The object to change -
mapFn
{Function(string | symbol, *, Object): [ string | number | symbol, *]}: The mapping function
- {Object}: The updated object
const obj = {
a: 1,
b: 2,
};
const tag = (key, value, object) => [key + value, value * 2];
map(obj, tag); // { a1: 2, b2: 4 }
Takes an object and a function and manipulates its keys.
The function signature for mapFn
is
(key, value, obj) => newKey
where key
and value
are the current key/value pairs to change, obj
is the original object, and newKey
is the new key to use for that value.
-
object
{Object}: The object to change -
mapFn
{Function(string | symbol, *, Object): string | number | symbol}: The mapping function
- {Object}: The object with updated keys
const obj = {
a: 1,
b: 2,
};
const tag = (key, value, object) => key + key;
mapKeys(obj, tag); // { aa: 1, bb: 2 }
Takes an object and a function and manipulates its values.
The function signature for mapFn
is
(value, key, obj) => newValue
where value
and key
are the current key/value pairs to change, obj
is the original object, and newValue
is the new value to use with that key.
-
object
{Object}: The object to change -
mapFn
{Function(*, string | symbol, Object): *}: The mapping function
- {Object}: The object with updated values
const obj = {
a: 1,
b: 2,
};
const double = (value, key, object) => value * 2;
mapValues(obj, double); // { a: 2, b: 4 }
Given an object and a function, returns the first [key, value]
pair that results in a truthy value.
The function signature for findFn
is:
(key, value, obj) => boolean
where key
and value
are the current key/value pairs to inspect and obj
is the original object.
-
object
{Object}: The object to change -
findFn
{Function(string | symbol, *, Object): boolean}: The finding function
-
{[string | symbol, *]}: The first
[key, value]
tuple for whichfindFn
returned a truthy value
const obj = {
a: 1,
b: 2,
c: 3,
};
const findEvenValue = ([key, value]) => value % 2 === 0;
find(obj, findEvenValue); // [ 'b', 2 ]
Given an object and a function, returns the first key that results in a truthy value.
The function signature for findFn
is the same as find()
.
-
object
{Object}: The object to change -
findFn
{Function(string | symbol, *, Object): boolean}: The finding function
-
{string | symbol}: The first key for which
findFn
returned a truthy value
const obj = {
a: 1,
b: 2,
c: 3,
};
const findEvenValue = ([key, value]) => value % 2 === 0;
findKey(obj, findEvenValue); // 'a'
Given an object and a function, returns the first value that results in a truthy value.
The function signature for findFn
is the same as find()
.
-
object
{Object}: The object to change -
findFn
{Function(string | symbol, *, Object): boolean}: The finding function
-
{*}: The first value for which
findFn
returned a truthy value
const obj = {
a: 1,
b: 2,
c: 3,
};
const findEvenValue = ([key, value]) => value % 2 === 0;
findValue(obj, findEvenValue); // 2
Objectivze is licensed under the MIT license.