@blakek/make-lookup
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -5,190 +5,7 @@ '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 deep = require('@blakek/deep'); | ||
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; | ||
const type = typeof object; | ||
return type === 'object' || type === 'function'; | ||
} | ||
const 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; | ||
} | ||
const [key, ...keys] = path; // 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; | ||
const value = traverseObject(object, parse(path)); | ||
if (value === NotFound || value === undefined) { | ||
return defaultValue; | ||
} | ||
return value; | ||
} | ||
function makeLookup(inputArray, lookupProperty) { | ||
return inputArray.reduce((lookup, next) => ({ ...lookup, | ||
[get(next, lookupProperty)]: next | ||
[deep.get(next, lookupProperty)]: next | ||
}), {}); | ||
@@ -195,0 +12,0 @@ } |
@@ -1,186 +0,3 @@ | ||
/** | ||
* @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 { get } from '@blakek/deep'; | ||
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; | ||
const type = typeof object; | ||
return type === 'object' || type === 'function'; | ||
} | ||
const 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; | ||
} | ||
const [key, ...keys] = path; // 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; | ||
const value = traverseObject(object, parse(path)); | ||
if (value === NotFound || value === undefined) { | ||
return defaultValue; | ||
} | ||
return value; | ||
} | ||
function makeLookup(inputArray, lookupProperty) { | ||
@@ -187,0 +4,0 @@ return inputArray.reduce((lookup, next) => ({ ...lookup, |
@@ -1,223 +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.makeLookup = mod.exports; | ||
} | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports) { | ||
"use strict"; | ||
_exports.__esModule = true; | ||
_exports.makeLookup = makeLookup; | ||
_exports["default"] = void 0; | ||
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | ||
/** | ||
* @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 makeLookup(inputArray, lookupProperty) { | ||
return inputArray.reduce(function (lookup, next) { | ||
var _extends2; | ||
return _extends(_extends({}, lookup), {}, (_extends2 = {}, _extends2[get(next, lookupProperty)] = next, _extends2)); | ||
}, {}); | ||
} | ||
var _default = makeLookup; | ||
_exports["default"] = _default; | ||
}); | ||
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((r=r||self)["make-lookup"]={})}(this,(function(r){"use strict";var e=function(r,e,t){return r(t={path:e,exports:{},require:function(r,e){return function(){throw new Error("Dynamic requires are not currently supported by @rollup/plugin-commonjs")}(null==e&&t.path)}},t.exports),t.exports}((function(r){function e(){return r.exports=e=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r},e.apply(this,arguments)}r.exports=e})),t={clear:function(){t.results={},t.size=0},results:{},size:0},n=/"[^"]+"|`[^`]+`|'[^']+'|[^.[\]]+/g,u=/^\d+$/i,o=/^"[^"]+"|`[^`]+`|'[^']+'$/,i=function(r,e){for(var t=r.length,n=[],u=0;u<t;u++)n[u]=e(r[u]);return n},f=function(r){var e=function(r){return o.test(r)}(r)?r.slice(1,r.length-1):r;return function(r){return!(!r||!r.length)&&u.test(r)}(e)?+e:e},s=Array.isArray,c=function(r){if("string"==typeof r)return function(r){return t.results[r]||(t.size>500&&t.clear(),t.results[r]=r?i(r.match(n),f):[r],t.size++),t.results[r]}(r);if(s(r))return i(r,f);var e=f(r);return["number"==typeof e?e:""+e]};var l=Symbol("value not found");function a(r,e,t){if(void 0===e)return r;var n=function r(e,t){if(0===t.length)return e;if(!function(r){if(null===r)return!1;var e=typeof r;return"object"===e||"function"===e}(e))return l;var n=t[0],u=t.slice(1);return n in e?r(e[n],u):l}(r,c(e));return n===l||void 0===n?t:n}function p(r,t){return r.reduce((function(r,n){var u;return e(e({},r),{},((u={})[a(n,t)]=n,u))}),{})}r.default=p,r.makeLookup=p,Object.defineProperty(r,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=make-lookup.umd.js.map |
{ | ||
"name": "@blakek/make-lookup", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"main": "dist/make-lookup.cjs.js", | ||
@@ -50,4 +50,6 @@ "module": "dist/make-lookup.esm.js", | ||
"@babel/plugin-proposal-class-properties": "^7.8.3", | ||
"@babel/plugin-transform-runtime": "^7.10.1", | ||
"@babel/preset-env": "^7.9.6", | ||
"@babel/preset-typescript": "^7.9.0", | ||
"@babel/runtime": "^7.10.2", | ||
"@rollup/plugin-babel": "^5.0.2", | ||
@@ -65,2 +67,3 @@ "@rollup/plugin-commonjs": "^12.0.0", | ||
"rollup": "^2.10.9", | ||
"rollup-plugin-terser": "^6.1.0", | ||
"ts-node": "^8.6.2", | ||
@@ -67,0 +70,0 @@ "typescript": "^3.9.3" |
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
8
110725
21
29
1