@aofl/object-utils
Advanced tools
Comparing version 3.0.0-alpha.11 to 3.0.0-beta.1
/** | ||
* Exports deepAssign and deepFreeze | ||
* Exports deepAssign, deepFreeze, get, has, set, defaults, recurseObjectByPath | ||
* | ||
* @module aofl-js/object-utils-package | ||
* @version 1.0.0 | ||
* @module @aofl/object-utils | ||
* @version 3.0.0 | ||
* @since 1.0.0 | ||
* @author Arian Khosravi <arian.khosravi@aofl.com> | ||
@@ -7,0 +8,0 @@ */ |
@@ -0,10 +1,27 @@ | ||
/** | ||
* @summary core | ||
* @version 3.0.0 | ||
* @since 3.0.0 | ||
* @author Arian Khosravi<arian.khosravi@aofl.com> | ||
*/ | ||
/** | ||
* Abstracts away the recursion function of traversing an object's nested | ||
* properties by a given path. | ||
* | ||
* @memberof module:@aofl/object-utils | ||
* | ||
* @param {Object} obj | ||
* @param {String} path dot notatino | ||
* @param {Function} op operation to perform as object is recursed by path. | ||
*/ | ||
const recurseObjectByPath = (obj, path, op) => { | ||
const pathParts = path === ''? '': path.split('.'); | ||
const recurse = (pathParts, source) => { | ||
const recurse = (argPathParts, source) => { | ||
let key = ''; | ||
let subPath = []; | ||
if (pathParts.length) { | ||
key = pathParts[0]; | ||
subPath = pathParts.slice(1); | ||
if (argPathParts.length) { | ||
key = argPathParts[0]; | ||
subPath = argPathParts.slice(1); | ||
} | ||
@@ -11,0 +28,0 @@ return op(key, subPath, source, recurse); |
@@ -0,1 +1,7 @@ | ||
/** | ||
* @summary deep-assign | ||
* @version 3.0.0 | ||
* @since 1.0.0 | ||
* @author Arian Khosravi<arian.khosravi@aofl.com> | ||
*/ | ||
import {recurseObjectByPath} from './core'; | ||
@@ -6,44 +12,41 @@ | ||
* | ||
* @memberof module:@aofl/object-utils | ||
* @example | ||
* let user = { | ||
* name: 'Alan', | ||
* account: { | ||
* active: true, | ||
* products: { | ||
* 1: true, | ||
* 2: true, | ||
* 3: true | ||
* } | ||
* }, | ||
* preferences: { | ||
* locale: 'en-US' | ||
* } | ||
* }; | ||
* | ||
* deepAssign(user, 'account.products', { | ||
* 2: false | ||
* }); | ||
* // { // new ref | ||
* // name: 'Alan', | ||
* // account: { // new ref | ||
* // active: true, | ||
* // products: { // new ref | ||
* // 1: true, | ||
* // 2: false, | ||
* // 3: true | ||
* // } | ||
* // }, | ||
* // preferences: { // same ref | ||
* // locale: 'en-US' | ||
* // } | ||
* // }; | ||
* | ||
* @version 1.0.0 | ||
* @author Arian Khosravi <arian.khosravi@aofl.com> | ||
* @memberof module:aofl-js/object-utils-package | ||
* @param {Object} left left source | ||
* @param {String} path path to target | ||
* @param {Object} right right source | ||
* @return {Object} | ||
*/ | ||
* name: 'Alan', | ||
* account: { | ||
* active: true, | ||
* products: { | ||
* '1': true, | ||
* '2': true, | ||
* '3': true | ||
* } | ||
* }, | ||
* preferences: { | ||
* locale: 'en-US' | ||
* } | ||
* }; | ||
* | ||
* deepAssign(user, 'account.products', { | ||
* 2: false | ||
* }); | ||
* { // new ref | ||
* name: 'Alan', | ||
* account: { // new ref | ||
* active: true, | ||
* products: { // new ref | ||
* '1': true, | ||
* '2': false, | ||
* '3': true | ||
* } | ||
* }, | ||
* preferences: { // same ref | ||
* locale: 'en-US' | ||
* } | ||
* }; | ||
* @param {Object} left left source | ||
* @param {String} path path to target | ||
* @param {Object} right right source | ||
* @return {Object} | ||
*/ | ||
const deepAssign = (left, path, right) => { | ||
@@ -50,0 +53,0 @@ return recurseObjectByPath(left, path, (key, pathParts, source, recurse) => { |
/** | ||
* @summary deep-freez | ||
* @version 3.0.0 | ||
* @since 1.0.0 | ||
* @author Arian Khosravi<arian.khosravi@aofl.com> | ||
*/ | ||
/** | ||
* Recursively calls Object.freeze on objects properties | ||
* | ||
* @version 1.0.0 | ||
* @author Arian Khosravi <arian.khosravi@aofl.com> | ||
* @memberof @module:aofl/object-utils | ||
* | ||
* @memberof module:aofl-js/object-utils-package | ||
* @param {Object} source | ||
@@ -9,0 +14,0 @@ * @return {Object} |
@@ -1,2 +0,19 @@ | ||
const defaults = (target, defaults) => { | ||
/** | ||
* @summary defaults | ||
* @version 3.0.0 | ||
* @since 1.0.0 | ||
* @author Arian Khosravi<arian.khosravi@aofl.com> | ||
*/ | ||
/** | ||
* Assigns missing defaultOptions onto the target object. | ||
* | ||
* @memberof module:@aofl/object-utils | ||
* | ||
* @param {Object} target | ||
* @param {Object} defaultOptions | ||
* | ||
* @return {Object} | ||
*/ | ||
const defaults = (target, defaultOptions) => { | ||
const recurse = (t, d) => { | ||
@@ -15,5 +32,5 @@ for (const key in d) { | ||
recurse(target, defaults); | ||
recurse(target, defaultOptions); | ||
}; | ||
export {defaults}; |
@@ -0,3 +1,18 @@ | ||
/** | ||
* @summary get | ||
* @version 3.0.0 | ||
* @since 1.0.0 | ||
* @author Arian Khosravi<arian.khosravi@aofl.com> | ||
*/ | ||
import {recurseObjectByPath} from './core'; | ||
/** | ||
* Returns nested property of an object based on path. | ||
* | ||
* @memberof module:@aofl/object-utils | ||
* | ||
* @param {Object} obj | ||
* @param {String} path dot notation | ||
*/ | ||
const get = (obj, path) => { | ||
@@ -4,0 +19,0 @@ return recurseObjectByPath(obj, path, (key, pathParts, source, recurse) => { |
@@ -0,3 +1,18 @@ | ||
/** | ||
* @summary has | ||
* @version 3.0.0 | ||
* @since 1.0.0 | ||
* @author Arian Khosravi<arian.khosravi@aofl.com> | ||
*/ | ||
import {recurseObjectByPath} from './core'; | ||
/** | ||
* Checks if an object has a nested property defined by path. | ||
* | ||
* @memberof module:@aofl/object-utils | ||
* | ||
* @param {Object} obj | ||
* @param {String} path dot notation | ||
* @retrun {Boolean} | ||
*/ | ||
const has = (obj, path) => { | ||
@@ -4,0 +19,0 @@ return recurseObjectByPath(obj, path, (key, pathParts, source, recurse) => { |
@@ -0,3 +1,17 @@ | ||
/** | ||
* @summary set | ||
* @version 3.0.0 | ||
* @since 1.0.0 | ||
* @author Arian Khosravi<arian.khosravi@aofl.com> | ||
*/ | ||
import {recurseObjectByPath} from './core'; | ||
/** | ||
* Sets the value of a nested member of an object. | ||
* | ||
* @memberof module:@aofl/object-utils | ||
* @param {Object} obj | ||
* @param {String} path | ||
* @param {*} val | ||
*/ | ||
const set = (obj, path, val) => { | ||
@@ -4,0 +18,0 @@ return recurseObjectByPath(obj, path, (key, pathParts, source, recurse) => { |
{ | ||
"name": "@aofl/object-utils", | ||
"version": "3.0.0-alpha.11", | ||
"version": "3.0.0-beta.1", | ||
"description": "A small collection of Object utility functions designed to have a small footprint (20b gz) and be performant", | ||
@@ -25,4 +25,3 @@ "main": "index.js", | ||
], | ||
"license": "MIT", | ||
"gitHead": "ecdfe7e68c95cd3ce9eca2c86626275d1d4559fe" | ||
"license": "MIT" | ||
} |
11444
333
12