js-object-interface
Wrap JS Objects to create a functional Array-like interface for iteration and whatnot.
The goal here is to make it easier to iterate over, and interact w/, complex data structures.
engines: node >= 9.x
Installation
npm i --save js-object-interface
Example
const applyInterface = ; const obj = a: 1 b: 2;var $obj = ; $obj;$obj;$obj; $obj;$obj;$obj; $obj;$obj; $obj;$obj; $obj;$obj; $obj;$obj; $obj;$obj;$obj; $obj;$obj;$obj;
API
API differs from the JS functional Array method API, so pay close attention - the devil is in the details.
.get()
.set()
.remove()
.forEach()
.map()
.filter()
.every()
.some()
.find()
.keys()
.clone()
.assign()
.get()
const obj = a: 1 b: 2 c: d: 3;var $obj = ; $obj // returns {a: 1, b: 2, c: {d: 3}};$obj // returns 1$obj // returns 3
.set()
const obj = a: 1 b: 2 c: d: 3;var $obj = ; $obj // replaces the .src obj w/ {e: 1}. Returns `this`.$obj$obj$obj // adds property "e" as an Object w/ an "f" property === 5
.remove()
const obj = a: 1 b: 2 c: d: 3;var $obj = ; $obj // does nothing$obj // obj === {b:2, c:{d: 3}}$obj // obj === {a: 1, b: 2, c: {}}
.forEach()
const obj = a: 1 b: 2 c: d: 3;var $obj = ; $obj; // returns `this`. // Nested .forEach()$obj; // Async .forEach()console;await $obj;console;// Logs 1, 2, 2, 2, 3
.map()
.map()
does not modify the .src Object - it returns a new Object.
"param2" [wrapResult=false] wraps the result in an interface.
const obj = a: 1 b: 2 c: d: 3;var $obj = ; var result = $obj; // result === {a: 1, b: 2, c: {d: 3}}. // Nested .map()var result = $obj // result === {a: 1, b: 2, c: {d: 3}} // Async .map()console;var result = await $obj;console;// result === {a: 1, b: 2, c: {d: 3}}// Logs 1, 2, 2, 2, 3
.filter()
.filter()
does not modify the .src Object - it returns a new Object.
"param2" [wrapResult=false] wraps the result in an interface.
If all callbacks return false
, null
is returned.
const obj = a: 1 b: 2 c: d: 3;var $obj = ; var result = $obj ; // result === {a: 1, b: 2, c: {d: 3}} // Async .filter()console;var result = await $obj;console;// result === {a: 1, b: 2, c: {d: 3}}// Logs 1, 2, 2, 2, 3
.every()
.every()
iterates over each Object property and if every callback returns true
, true
is returned, if not, false
is returned.
const obj = a: 1 b: 2 c: d: 3;var $obj = ; var result = $obj; // result === true.
.some()
.some()
iterates over each Object property and if at least one callback returns true
, true
is returned, if not, false
is returned.
const obj = a: 1 b: 2 c: d: 3;var $obj = ; var result = $obj; // result === true.
.find()
.find()
iterates over each Object property and returns the first property where the callback returns a "truthy" value.
If no matches are found, undefined
is returned.
const obj = a: 1 b: 2 c: d: 3;var $obj = ; var key = $obj; // key === 'a'
.keys()
Returns Object.keys()
on the .src Object or a nested property.
const obj = a: 1 b: 2 c: d: 3;var $obj = ; $obj; // ['a', 'b', 'c']$obj; // ['d']$obj; // null
.clone()
Returns a recursive (deep) copy of the .src object.
const obj = a: 1 b: 2 c: d: 3;var $obj = ; var obj1 = $obj;var obj1 = $obj; // wraps obj1 in an interface.
.assign()
Returns a new Object (deep assign).
const obj = a: 1 b: 2 c: d: 3;var $obj = ; var obj1 = $obj; // obj1 === {a: 9, b: 2, c: {d: 3}}; // To update the .src Object:obj;
- View src (./lib/index.js) and tests (./tests/main.test.js) for a more detailed understanding of the API.
Custom methods
You can add custom method(s) to your Object.
var obj1 = a: 1;var $obj = ;$obj // result === 1$obj // result === 2
Performance
Now I wouldn't consider the module to be a performance bottleneck, but I should mention that it is not optimized for high-performance applications. View src.
License
MIT