@blakek/deep
Advanced tools
Comparing version 1.0.3 to 2.0.0
@@ -5,149 +5,5 @@ 'use strict'; | ||
/** | ||
* @constant {Object} CACHE | ||
* | ||
* @property {function} clear clear the cache results | ||
* @property {Object} results the map of path => array results | ||
* @property {number} size the size of the cache | ||
*/ | ||
var CACHE = { | ||
clear: function clear() { | ||
CACHE.results = {}; | ||
CACHE.size = 0; | ||
}, | ||
results: {}, | ||
size: 0 | ||
}; | ||
/** | ||
* @constant {RegExp} DOTTY_WITH_BRACKETS_SYNTAX | ||
*/ | ||
var curry = require('@blakek/curry'); | ||
var pathington = require('pathington'); | ||
var DOTTY_WITH_BRACKETS_SYNTAX = /"[^"]+"|`[^`]+`|'[^']+'|[^.[\]]+/g; | ||
/** | ||
* @constant {number} MAX_CACHE_SIZE | ||
*/ | ||
var MAX_CACHE_SIZE = 500; | ||
/** | ||
* @constant {RegExp} NUMBER | ||
*/ | ||
var NUMBER = /^\d+$/i; | ||
/** | ||
* @constant {RegExp} QUOTED_KEY | ||
*/ | ||
var QUOTED_KEY = /^"[^"]+"|`[^`]+`|'[^']+'$/; | ||
// constants | ||
/** | ||
* @function isNumericKey | ||
* | ||
* @description | ||
* is the key passed a numeric string | ||
* | ||
* @param {string} key the key to test | ||
* @returns {boolean} is the key passed a numeric string | ||
*/ | ||
var isNumericKey = function isNumericKey(key) { | ||
return !!(key && key.length) && NUMBER.test(key); | ||
}; | ||
/** | ||
* @function isQuotedKey | ||
* | ||
* @description | ||
* is the key passed a quoted key | ||
* | ||
* @param {string} key the key to test | ||
* @returns {boolean} is the key a quoted key | ||
*/ | ||
var isQuotedKey = function isQuotedKey(key) { | ||
return QUOTED_KEY.test(key); | ||
}; | ||
/** | ||
* @function map | ||
* | ||
* @description | ||
* map the array to a new array based on fn | ||
* | ||
* @param {Array<*>} array the array to map | ||
* @param {function} fn the function to call with each iteration value | ||
* @returns {Array<*>} the mapped array | ||
*/ | ||
var map = function map(array, fn) { | ||
var length = array.length; | ||
var mapped = []; | ||
for (var index = 0; index < length; index++) { | ||
mapped[index] = fn(array[index]); | ||
} | ||
return mapped; | ||
}; | ||
/** | ||
* @function getNormalizedParseKey | ||
* | ||
* @description | ||
* get the key as a number if parseable, or as a quoted string if applicable | ||
* | ||
* @param {string} key the key to try to parse | ||
* @returns {number|string} the parsed key | ||
*/ | ||
var getNormalizedParseKey = function getNormalizedParseKey(key) { | ||
var cleanKey = isQuotedKey(key) ? key.slice(1, key.length - 1) : key; | ||
return isNumericKey(cleanKey) ? +cleanKey : cleanKey; | ||
}; | ||
/** | ||
* @function parsePath | ||
* | ||
* @description | ||
* parse the path, memoizing the results | ||
* | ||
* @param {string} path the path to parse | ||
* @returns {Array<number|string>} the parsed path | ||
*/ | ||
var parseStringPath = function parseStringPath(path) { | ||
if (CACHE.results[path]) { | ||
return CACHE.results[path]; | ||
} | ||
if (CACHE.size > MAX_CACHE_SIZE) { | ||
CACHE.clear(); | ||
} | ||
CACHE.results[path] = path ? map(path.match(DOTTY_WITH_BRACKETS_SYNTAX), getNormalizedParseKey) : [path]; | ||
CACHE.size++; | ||
return CACHE.results[path]; | ||
}; | ||
// constants | ||
var isArray = Array.isArray; | ||
/** | ||
* @function parse | ||
* | ||
* @description | ||
* the path parsed into a valid array of keys / indices | ||
* | ||
* @param {Array<number|string>|number|string} path the path to parse | ||
* @returns {Array<number|string>} the parsed path | ||
*/ | ||
var parse = function parse(path) { | ||
if (typeof path === 'string') { | ||
return parseStringPath(path); | ||
} | ||
if (isArray(path)) { | ||
return map(path, getNormalizedParseKey); | ||
} | ||
var normalizedParseKey = getNormalizedParseKey(path); | ||
return [typeof normalizedParseKey === 'number' ? normalizedParseKey : "" + normalizedParseKey]; | ||
}; | ||
function isObject(object) { | ||
@@ -159,3 +15,3 @@ if (object === null) return false; | ||
const NotFound = Symbol('value not found'); | ||
const NotFound = Symbol('curriable placeholder'); | ||
function traverseObject(object, path) { | ||
@@ -181,5 +37,6 @@ // If the path has been exhausted, return the current object | ||
} | ||
function get(object, path, defaultValue) { | ||
function _getOr(defaultValue, path, object) { | ||
if (path === undefined) return object; | ||
const value = traverseObject(object, parse(path)); | ||
const value = traverseObject(object, pathington.parse(path)); | ||
@@ -192,12 +49,16 @@ if (value === NotFound || value === undefined) { | ||
} | ||
function has(object, path) { | ||
const value = traverseObject(object, parse(path)); | ||
const _get = (path, object) => _getOr(undefined, path, object); | ||
function _has(path, object) { | ||
const value = traverseObject(object, pathington.parse(path)); | ||
return value !== NotFound; | ||
} | ||
function remove(object, path) { | ||
function _remove(path, object) { | ||
if (path === undefined) return object; | ||
const parsedPath = parse(path); | ||
const parsedPath = pathington.parse(path); | ||
const referencePath = parsedPath.slice(0, -1); | ||
const finalPath = parsedPath[parsedPath.length - 1]; | ||
const reference = traverseObject(object, parse(referencePath)); | ||
const reference = traverseObject(object, pathington.parse(referencePath)); | ||
if (!reference) return object; | ||
@@ -207,4 +68,5 @@ delete reference[finalPath]; | ||
} | ||
function set(object, path, value) { | ||
const parsedPath = parse(path); | ||
function _set(value, path, object) { | ||
const parsedPath = pathington.parse(path); | ||
let reference = object; | ||
@@ -226,3 +88,10 @@ parsedPath.forEach((key, index) => { | ||
const get = curry.curry(_get); | ||
const getOr = curry.curry(_getOr); | ||
const has = curry.curry(_has); | ||
const remove = curry.curry(_remove); | ||
const set = curry.curry(_set); | ||
exports.get = get; | ||
exports.getOr = getOr; | ||
exports.has = has; | ||
@@ -229,0 +98,0 @@ exports.remove = remove; |
export declare type Path = Array<number | string> | string; | ||
export declare function traverseObject(object: any, path: string[]): any; | ||
export declare function get(object: any, path?: Path, defaultValue?: any): any; | ||
export declare function has(object: any, path: Path): boolean; | ||
export declare function remove(object: any, path: Path): any; | ||
export declare function set(object: any, path: Path, value: any): any; | ||
export declare const get: import("@blakek/curry").Curry2<import("pathington").Path, any, any>; | ||
export declare const getOr: import("@blakek/curry").Curry3<any, import("pathington").Path, any, any>; | ||
export declare const has: import("@blakek/curry").Curry2<import("pathington").Path, any, boolean>; | ||
export declare const remove: import("@blakek/curry").Curry2<import("pathington").Path, any, any>; | ||
export declare const set: import("@blakek/curry").Curry3<any, import("pathington").Path, any, any>; |
@@ -1,148 +0,4 @@ | ||
/** | ||
* @constant {Object} CACHE | ||
* | ||
* @property {function} clear clear the cache results | ||
* @property {Object} results the map of path => array results | ||
* @property {number} size the size of the cache | ||
*/ | ||
var CACHE = { | ||
clear: function clear() { | ||
CACHE.results = {}; | ||
CACHE.size = 0; | ||
}, | ||
results: {}, | ||
size: 0 | ||
}; | ||
/** | ||
* @constant {RegExp} DOTTY_WITH_BRACKETS_SYNTAX | ||
*/ | ||
import { curry } from '@blakek/curry'; | ||
import { parse } from 'pathington'; | ||
var DOTTY_WITH_BRACKETS_SYNTAX = /"[^"]+"|`[^`]+`|'[^']+'|[^.[\]]+/g; | ||
/** | ||
* @constant {number} MAX_CACHE_SIZE | ||
*/ | ||
var MAX_CACHE_SIZE = 500; | ||
/** | ||
* @constant {RegExp} NUMBER | ||
*/ | ||
var NUMBER = /^\d+$/i; | ||
/** | ||
* @constant {RegExp} QUOTED_KEY | ||
*/ | ||
var QUOTED_KEY = /^"[^"]+"|`[^`]+`|'[^']+'$/; | ||
// constants | ||
/** | ||
* @function isNumericKey | ||
* | ||
* @description | ||
* is the key passed a numeric string | ||
* | ||
* @param {string} key the key to test | ||
* @returns {boolean} is the key passed a numeric string | ||
*/ | ||
var isNumericKey = function isNumericKey(key) { | ||
return !!(key && key.length) && NUMBER.test(key); | ||
}; | ||
/** | ||
* @function isQuotedKey | ||
* | ||
* @description | ||
* is the key passed a quoted key | ||
* | ||
* @param {string} key the key to test | ||
* @returns {boolean} is the key a quoted key | ||
*/ | ||
var isQuotedKey = function isQuotedKey(key) { | ||
return QUOTED_KEY.test(key); | ||
}; | ||
/** | ||
* @function map | ||
* | ||
* @description | ||
* map the array to a new array based on fn | ||
* | ||
* @param {Array<*>} array the array to map | ||
* @param {function} fn the function to call with each iteration value | ||
* @returns {Array<*>} the mapped array | ||
*/ | ||
var map = function map(array, fn) { | ||
var length = array.length; | ||
var mapped = []; | ||
for (var index = 0; index < length; index++) { | ||
mapped[index] = fn(array[index]); | ||
} | ||
return mapped; | ||
}; | ||
/** | ||
* @function getNormalizedParseKey | ||
* | ||
* @description | ||
* get the key as a number if parseable, or as a quoted string if applicable | ||
* | ||
* @param {string} key the key to try to parse | ||
* @returns {number|string} the parsed key | ||
*/ | ||
var getNormalizedParseKey = function getNormalizedParseKey(key) { | ||
var cleanKey = isQuotedKey(key) ? key.slice(1, key.length - 1) : key; | ||
return isNumericKey(cleanKey) ? +cleanKey : cleanKey; | ||
}; | ||
/** | ||
* @function parsePath | ||
* | ||
* @description | ||
* parse the path, memoizing the results | ||
* | ||
* @param {string} path the path to parse | ||
* @returns {Array<number|string>} the parsed path | ||
*/ | ||
var parseStringPath = function parseStringPath(path) { | ||
if (CACHE.results[path]) { | ||
return CACHE.results[path]; | ||
} | ||
if (CACHE.size > MAX_CACHE_SIZE) { | ||
CACHE.clear(); | ||
} | ||
CACHE.results[path] = path ? map(path.match(DOTTY_WITH_BRACKETS_SYNTAX), getNormalizedParseKey) : [path]; | ||
CACHE.size++; | ||
return CACHE.results[path]; | ||
}; | ||
// constants | ||
var isArray = Array.isArray; | ||
/** | ||
* @function parse | ||
* | ||
* @description | ||
* the path parsed into a valid array of keys / indices | ||
* | ||
* @param {Array<number|string>|number|string} path the path to parse | ||
* @returns {Array<number|string>} the parsed path | ||
*/ | ||
var parse = function parse(path) { | ||
if (typeof path === 'string') { | ||
return parseStringPath(path); | ||
} | ||
if (isArray(path)) { | ||
return map(path, getNormalizedParseKey); | ||
} | ||
var normalizedParseKey = getNormalizedParseKey(path); | ||
return [typeof normalizedParseKey === 'number' ? normalizedParseKey : "" + normalizedParseKey]; | ||
}; | ||
function isObject(object) { | ||
@@ -154,3 +10,3 @@ if (object === null) return false; | ||
const NotFound = Symbol('value not found'); | ||
const NotFound = Symbol('curriable placeholder'); | ||
function traverseObject(object, path) { | ||
@@ -176,3 +32,4 @@ // If the path has been exhausted, return the current object | ||
} | ||
function get(object, path, defaultValue) { | ||
function _getOr(defaultValue, path, object) { | ||
if (path === undefined) return object; | ||
@@ -187,7 +44,11 @@ const value = traverseObject(object, parse(path)); | ||
} | ||
function has(object, path) { | ||
const _get = (path, object) => _getOr(undefined, path, object); | ||
function _has(path, object) { | ||
const value = traverseObject(object, parse(path)); | ||
return value !== NotFound; | ||
} | ||
function remove(object, path) { | ||
function _remove(path, object) { | ||
if (path === undefined) return object; | ||
@@ -202,3 +63,4 @@ const parsedPath = parse(path); | ||
} | ||
function set(object, path, value) { | ||
function _set(value, path, object) { | ||
const parsedPath = parse(path); | ||
@@ -221,2 +83,8 @@ let reference = object; | ||
export { get, has, remove, set, traverseObject }; | ||
const get = curry(_get); | ||
const getOr = curry(_getOr); | ||
const has = curry(_has); | ||
const remove = curry(_remove); | ||
const set = curry(_set); | ||
export { get, getOr, has, remove, set, traverseObject }; |
@@ -1,247 +0,2 @@ | ||
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports"], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(exports); | ||
} else { | ||
var mod = { | ||
exports: {} | ||
}; | ||
factory(mod.exports); | ||
global.deep = mod.exports; | ||
} | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports) { | ||
"use strict"; | ||
_exports.__esModule = true; | ||
_exports.get = get; | ||
_exports.has = has; | ||
_exports.remove = remove; | ||
_exports.set = set; | ||
_exports.traverseObject = traverseObject; | ||
/** | ||
* @constant {Object} CACHE | ||
* | ||
* @property {function} clear clear the cache results | ||
* @property {Object} results the map of path => array results | ||
* @property {number} size the size of the cache | ||
*/ | ||
var CACHE = { | ||
clear: function clear() { | ||
CACHE.results = {}; | ||
CACHE.size = 0; | ||
}, | ||
results: {}, | ||
size: 0 | ||
}; | ||
/** | ||
* @constant {RegExp} DOTTY_WITH_BRACKETS_SYNTAX | ||
*/ | ||
var DOTTY_WITH_BRACKETS_SYNTAX = /"[^"]+"|`[^`]+`|'[^']+'|[^.[\]]+/g; | ||
/** | ||
* @constant {number} MAX_CACHE_SIZE | ||
*/ | ||
var MAX_CACHE_SIZE = 500; | ||
/** | ||
* @constant {RegExp} NUMBER | ||
*/ | ||
var NUMBER = /^\d+$/i; | ||
/** | ||
* @constant {RegExp} QUOTED_KEY | ||
*/ | ||
var QUOTED_KEY = /^"[^"]+"|`[^`]+`|'[^']+'$/; // constants | ||
/** | ||
* @function isNumericKey | ||
* | ||
* @description | ||
* is the key passed a numeric string | ||
* | ||
* @param {string} key the key to test | ||
* @returns {boolean} is the key passed a numeric string | ||
*/ | ||
var isNumericKey = function isNumericKey(key) { | ||
return !!(key && key.length) && NUMBER.test(key); | ||
}; | ||
/** | ||
* @function isQuotedKey | ||
* | ||
* @description | ||
* is the key passed a quoted key | ||
* | ||
* @param {string} key the key to test | ||
* @returns {boolean} is the key a quoted key | ||
*/ | ||
var isQuotedKey = function isQuotedKey(key) { | ||
return QUOTED_KEY.test(key); | ||
}; | ||
/** | ||
* @function map | ||
* | ||
* @description | ||
* map the array to a new array based on fn | ||
* | ||
* @param {Array<*>} array the array to map | ||
* @param {function} fn the function to call with each iteration value | ||
* @returns {Array<*>} the mapped array | ||
*/ | ||
var map = function map(array, fn) { | ||
var length = array.length; | ||
var mapped = []; | ||
for (var index = 0; index < length; index++) { | ||
mapped[index] = fn(array[index]); | ||
} | ||
return mapped; | ||
}; | ||
/** | ||
* @function getNormalizedParseKey | ||
* | ||
* @description | ||
* get the key as a number if parseable, or as a quoted string if applicable | ||
* | ||
* @param {string} key the key to try to parse | ||
* @returns {number|string} the parsed key | ||
*/ | ||
var getNormalizedParseKey = function getNormalizedParseKey(key) { | ||
var cleanKey = isQuotedKey(key) ? key.slice(1, key.length - 1) : key; | ||
return isNumericKey(cleanKey) ? +cleanKey : cleanKey; | ||
}; | ||
/** | ||
* @function parsePath | ||
* | ||
* @description | ||
* parse the path, memoizing the results | ||
* | ||
* @param {string} path the path to parse | ||
* @returns {Array<number|string>} the parsed path | ||
*/ | ||
var parseStringPath = function parseStringPath(path) { | ||
if (CACHE.results[path]) { | ||
return CACHE.results[path]; | ||
} | ||
if (CACHE.size > MAX_CACHE_SIZE) { | ||
CACHE.clear(); | ||
} | ||
CACHE.results[path] = path ? map(path.match(DOTTY_WITH_BRACKETS_SYNTAX), getNormalizedParseKey) : [path]; | ||
CACHE.size++; | ||
return CACHE.results[path]; | ||
}; // constants | ||
var isArray = Array.isArray; | ||
/** | ||
* @function parse | ||
* | ||
* @description | ||
* the path parsed into a valid array of keys / indices | ||
* | ||
* @param {Array<number|string>|number|string} path the path to parse | ||
* @returns {Array<number|string>} the parsed path | ||
*/ | ||
var parse = function parse(path) { | ||
if (typeof path === 'string') { | ||
return parseStringPath(path); | ||
} | ||
if (isArray(path)) { | ||
return map(path, getNormalizedParseKey); | ||
} | ||
var normalizedParseKey = getNormalizedParseKey(path); | ||
return [typeof normalizedParseKey === 'number' ? normalizedParseKey : "" + normalizedParseKey]; | ||
}; | ||
function isObject(object) { | ||
if (object === null) return false; | ||
var type = typeof object; | ||
return type === 'object' || type === 'function'; | ||
} | ||
var NotFound = Symbol('value not found'); | ||
function traverseObject(object, path) { | ||
// If the path has been exhausted, return the current object | ||
if (path.length === 0) { | ||
return object; | ||
} // If the value could not be found, return `defaultValue` | ||
if (!isObject(object)) { | ||
return NotFound; | ||
} | ||
var key = path[0], | ||
keys = path.slice(1); // Search deeper in the object | ||
if (key in object) { | ||
return traverseObject(object[key], keys); | ||
} // The key was not found in the object. | ||
return NotFound; | ||
} | ||
function get(object, path, defaultValue) { | ||
if (path === undefined) return object; | ||
var value = traverseObject(object, parse(path)); | ||
if (value === NotFound || value === undefined) { | ||
return defaultValue; | ||
} | ||
return value; | ||
} | ||
function has(object, path) { | ||
var value = traverseObject(object, parse(path)); | ||
return value !== NotFound; | ||
} | ||
function remove(object, path) { | ||
if (path === undefined) return object; | ||
var parsedPath = parse(path); | ||
var referencePath = parsedPath.slice(0, -1); | ||
var finalPath = parsedPath[parsedPath.length - 1]; | ||
var reference = traverseObject(object, parse(referencePath)); | ||
if (!reference) return object; | ||
delete reference[finalPath]; | ||
return object; | ||
} | ||
function set(object, path, value) { | ||
var parsedPath = parse(path); | ||
var reference = object; | ||
parsedPath.forEach(function (key, index) { | ||
if (index === parsedPath.length - 1) { | ||
reference[key] = value; | ||
return; | ||
} | ||
if (!isObject(reference[key])) { | ||
reference[key] = {}; | ||
} | ||
reference = reference[key]; | ||
}); | ||
return object; | ||
} | ||
}); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e=e||self).deep={})}(this,(function(e){"use strict";function r(e,r){return void 0===r&&(r=e.length),function n(){for(var t=arguments.length,u=new Array(t),i=0;i<t;i++)u[i]=arguments[i];return u.length>=r?e.apply(void 0,u.slice(0,r)):function(){for(var e=arguments.length,r=new Array(e),t=0;t<e;t++)r[t]=arguments[t];return n.apply(void 0,u.concat(r))}}}var n={clear:function(){n.results={},n.size=0},results:{},size:0},t=/"[^"]+"|`[^`]+`|'[^']+'|[^.[\]]+/g,u=/^\d+$/i,i=/^"[^"]+"|`[^`]+`|'[^']+'$/,o=function(e,r){for(var n=e.length,t=[],u=0;u<n;u++)t[u]=r(e[u]);return t},f=function(e){var r=function(e){return i.test(e)}(e)?e.slice(1,e.length-1):e;return function(e){return!(!e||!e.length)&&u.test(e)}(r)?+r:r},c=Array.isArray,l=function(e){if("string"==typeof e)return function(e){return n.results[e]||(n.size>500&&n.clear(),n.results[e]=e?o(e.match(t),f):[e],n.size++),n.results[e]}(e);if(c(e))return o(e,f);var r=f(e);return["number"==typeof r?r:""+r]};function s(e){if(null===e)return!1;var r=typeof e;return"object"===r||"function"===r}var a=Symbol("curriable placeholder");function v(e,r){if(0===r.length)return e;if(!s(e))return a;var n=r[0],t=r.slice(1);return n in e?v(e[n],t):a}function d(e,r,n){if(void 0===r)return n;var t=v(n,l(r));return t===a||void 0===t?e:t}var p=r((function(e,r){return d(void 0,e,r)})),h=r(d),g=r((function(e,r){return v(r,l(e))!==a})),y=r((function(e,r){if(void 0===e)return r;var n=l(e),t=n.slice(0,-1),u=n[n.length-1],i=v(r,l(t));return i?(delete i[u],r):r})),b=r((function(e,r,n){var t=l(r),u=n;return t.forEach((function(r,n){n!==t.length-1?(s(u[r])||(u[r]={}),u=u[r]):u[r]=e})),n}));e.get=p,e.getOr=h,e.has=g,e.remove=y,e.set=b,e.traverseObject=v,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "@blakek/deep", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"main": "dist/index.cjs.js", | ||
@@ -49,25 +49,34 @@ "module": "dist/index.esm.js", | ||
}, | ||
"browserslist": [ | ||
">0.2%", | ||
"not dead", | ||
"not op_mini all" | ||
], | ||
"dependencies": { | ||
"@blakek/curry": "^2.0.2", | ||
"pathington": "^1.1.7" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.9.6", | ||
"@babel/plugin-proposal-class-properties": "^7.8.3", | ||
"@babel/preset-env": "^7.9.6", | ||
"@babel/preset-typescript": "^7.9.0", | ||
"@rollup/plugin-babel": "^5.0.2", | ||
"@rollup/plugin-commonjs": "^12.0.0", | ||
"@rollup/plugin-node-resolve": "^8.0.0", | ||
"@typescript-eslint/eslint-plugin": "^2.23.0", | ||
"@typescript-eslint/parser": "^2.23.0", | ||
"@babel/core": "^7.10.2", | ||
"@babel/plugin-proposal-class-properties": "^7.10.1", | ||
"@babel/plugin-transform-runtime": "^7.10.1", | ||
"@babel/preset-env": "^7.10.2", | ||
"@babel/preset-typescript": "^7.10.1", | ||
"@rollup/plugin-babel": "^5.0.3", | ||
"@rollup/plugin-commonjs": "^13.0.0", | ||
"@rollup/plugin-node-resolve": "^8.0.1", | ||
"@typescript-eslint/eslint-plugin": "^3.2.0", | ||
"@typescript-eslint/parser": "^3.2.0", | ||
"amper-scripts": "^1.0.0-0", | ||
"ava": "^3.5.0", | ||
"nodemon": "^2.0.2", | ||
"ava": "^3.9.0", | ||
"nodemon": "^2.0.4", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^1.19.1", | ||
"prettier": "^2.0.5", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.10.9", | ||
"ts-node": "^8.6.2", | ||
"typescript": "^3.9.3" | ||
"rollup": "^2.16.1", | ||
"rollup-plugin-terser": "^6.1.0", | ||
"ts-node": "^8.10.2", | ||
"typescript": "^3.9.5" | ||
}, | ||
"peerDependencies": {}, | ||
"scripts": { | ||
@@ -74,0 +83,0 @@ "build": "run-s build:clean build:types build:js", |
106
README.md
@@ -28,3 +28,3 @@ # deep | ||
```js | ||
import { get, has, remove, set } from '@blakek/deep'; | ||
import { get, getOr, has, remove, set } from '@blakek/deep'; | ||
@@ -42,18 +42,19 @@ const user = { | ||
// Get a property value | ||
get(user, 'sites.github.username'); // 'blakek' | ||
get(user, 'this.does.not.exist'); // undefined | ||
get(user, 'sites.facebook.username', 'no-account'); // 'no-account' | ||
get(user, 'roles.0'); // 'alert:create' | ||
get('sites.github.username', user); //» 'blakek' | ||
// Arguments can be partially applied | ||
const githubUsername = get('sites.github.username'); | ||
githubUsername(user); //» 'blakek' | ||
// Get a property value with a fallback other than `undefined` | ||
getOr('no-account', 'sites.facebook.username', user); //» 'no-account' | ||
// Test for a property value | ||
has(user, 'sites.github'); // true | ||
has(user, 'sites.twitter'); // false | ||
has('sites.github', user); //» true | ||
// Remove a property value | ||
remove({ a: 42, b: 123 }, 'a'); // { b: 123 } | ||
remove({ a: 42 }, 'nothing.exists.here'); // { a: 42 } | ||
remove('a', { a: 42, b: 123 }); //» { b: 123 } | ||
// Set a property value | ||
set({ a: 42 }, 'a', 123); // { a: 123 } | ||
set({ a: 42 }, 'a.b.c', 123); // { a: { b: { c: 123 } } } | ||
set(123, 'a.b.c', { a: 42 }); //» { a: { b: { c: 123 } } } | ||
``` | ||
@@ -63,8 +64,11 @@ | ||
For all these, `Path` can be a dot-notation string or array of path parts. | ||
For all these: | ||
- `path` can be either a dot-notation string or array of path parts | ||
- arguments can be partially applied | ||
### `get` | ||
```ts | ||
function get(object: any, path?: Path, defaultValue?: any): any; | ||
function get(path: Path, object: any): any; | ||
``` | ||
@@ -77,14 +81,51 @@ | ||
id: 'abf87de', | ||
roles: ['alert:create', 'alert:read'] | ||
roles: ['alert:create', 'alert:read'], | ||
sites: { | ||
github: { | ||
username: 'blakek' | ||
} | ||
} | ||
}; | ||
get(user, 'roles.0'); // 'alert:create' | ||
get(user, ['roles', 1]); // 'alert:read' | ||
get(user, 'does.not.exist', 'fallback'); // 'fallback' | ||
get('id', user); //» 'abf87de' | ||
get('roles.0', user); //» 'alert:create' | ||
get('roles[0]', user); //» 'alert:create' | ||
get(['roles', 1], user); //» 'alert:read' | ||
get('sites.github.username', user); //» 'blakek' | ||
const getID = get('id'); | ||
getID(user); //» 'abf87de' | ||
``` | ||
### `getOr` | ||
```ts | ||
function getOr(defaultValue: any, path: Path, object: any): any; | ||
``` | ||
Like `get`, gets a value from an object. Will return a fallback other than | ||
`undefined` if the value was not found equal to `undefined`. | ||
```js | ||
const user = { | ||
id: 'abf87de', | ||
roles: ['alert:create', 'alert:read'], | ||
sites: { | ||
github: { | ||
username: 'blakek' | ||
} | ||
} | ||
}; | ||
getOr('/images/placeholder.png', 'sites.github.image', user); //» '/images/placeholder.png' | ||
const getRoles = getOr([], 'roles'); | ||
getRoles(user); //» ['alert:create', 'alert:read'] | ||
getRoles({}); //» [] | ||
``` | ||
### `has` | ||
```ts | ||
function has(object: any, path: Path): boolean; | ||
function has(path: Path, object: any): boolean; | ||
``` | ||
@@ -105,9 +146,8 @@ | ||
has(product, 'attributes.materials'); // true | ||
has(product, ['avability', 'sizes']); // false | ||
has(product, 'attributes.isCool'); // true (property exists but is undefined) | ||
has('attributes.materials', product); //» true | ||
has(['avability', 'sizes'], product); //» false | ||
has('attributes.isCool', product); //» true; property exists but is undefined | ||
// `get()` should be used if you want to ensure a value is not `null` or | ||
// `undefined` | ||
get(product, 'attributes.isCool', false); // false | ||
// `get()` should be used if you want to ensure a value is not `undefined` | ||
getOr(false, 'attributes.isCool', product); //» false | ||
``` | ||
@@ -118,3 +158,3 @@ | ||
```ts | ||
function remove(object: any, path: Path): any; | ||
function remove(path: Path, object: any): any; | ||
``` | ||
@@ -130,4 +170,5 @@ | ||
remove(user, 'password'); // { username: 'blakek' } | ||
remove(user, 'property.does.not.exist'); // { username: 'blakek' } | ||
remove('password', user); //» { username: 'blakek' } | ||
remove('property.does.not.exist', user); | ||
//» { username: 'blakek' } (same object from previous line) | ||
``` | ||
@@ -138,3 +179,3 @@ | ||
```ts | ||
function set(object: any, path: Path, value: any): any; | ||
function set(value: any, path: Path, object: any): any; | ||
``` | ||
@@ -151,8 +192,9 @@ | ||
set(user, 'profile.bgColor', 'tomato'); // { profile: { bgColor: 'tomato' } | ||
set('tomato', 'profile.bgColor', user); //» { profile: { bgColor: 'tomato' } } | ||
set(user, 'profile.bgImage', '/images/user.png'); | ||
// { profile: { bgColor: 'tomato', bgImage: '/images/user.png' } } | ||
set('/images/user.png', 'profile.bgImage', user); | ||
//» { profile: { bgColor: 'tomato', bgImage: '/images/user.png' } } | ||
set(user, 'profile', null); // { profile: null } | ||
const logout = set(null, 'profile'); | ||
logout(user); //» { profile: null } | ||
``` | ||
@@ -159,0 +201,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
129251
9
220
2
20
156
1
+ Added@blakek/curry@^2.0.2
+ Added@blakek/curry@2.0.2(transitive)