diff --git a/dist/index.js b/dist/index.js index 596595c..5a1fce5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,146 +1,6 @@ 'use strict'; /* -SEARCHHASH v1.2.7 -~5KB +SEARCHHASH v1.2.8 +~2KB */ -var searchHash = (function() { - // some utility func - function jCompare(obj1, obj2) { - return JSON.stringify(obj1) === JSON.stringify(obj2) && !isRegExp(obj2); - } - - function isString(o) { - return typeof o === 'string' || o instanceof String; - } - - function isRegExp(o) { - return o instanceof RegExp; - } - - function isObj(o) { - var t0 = String(o) !== o, - t1 = o === Object(o), - t2 = typeof o !== 'function', - t3 = {}.toString.call(o).match(/\[object\sObject\]/); - return t0 && t1 && t2 && !!(t3 && t3.length); - } - - function isArr(o) { - var t2 = ({}).toString.call(o).match(/\[object\sArray\]/); - return String(o) !== o && !!(t2 && t2.length); - } - - function isElement(o) { - return ( - o && typeof o === 'object' && // DOM2 - typeof o.nodeType !== 'undefined' && o.nodeType === 1 && - typeof o.nodeName === 'string' - ); - } - - /** - * Main searching function - */ - function digFor(what, rootObj, target, opts) { - if (!isObj(rootObj) && !isArr(rootObj)) throw new Error('BAD PARAM: must search into an object or an array'); - var t, - found = 0, - strOrRx = function(x, y) { - return (isString(x) && isRegExp(y)) ? - x.match(y) : - jCompare(x, y); - }, - matches = { - key: function(k1, k2, key) { - return typeof key === 'function' ? key(k1) : strOrRx(k1, key); - }, - value: function(k1, k2, val) { - return typeof val === 'function' ? val(k2) : strOrRx(k2, val); - }, - keyvalue: function(k1, k2, keyval) { - return ( - (typeof keyval.key === 'function' && keyval.key(k1)) || - strOrRx(k1, keyval.key) - ) && ( - (typeof keyval.value === 'function' && keyval.value(k2)) || - strOrRx(k2, keyval.value) - ); - } - }[what], - res = [], - maybePush = function(objpath, index, trg, obj, level) { - var p = [].concat.call(objpath, [index]), - tmp = matches(index, obj[index], trg), - inRange = opts.min <= level && level <= opts.max, - plen = p.length; - if (inRange && tmp) { - res.push({ - obj: obj, - value: obj[index], - key: p[plen - 1], - parentKey: p[plen - 2], - path: p.join('/'), - getter: function() { - return p.reduce((acc, el) => acc[el], rootObj); - }, - container: p.slice(0, plen - 1).join('/'), - parentContainer: p.slice(0, plen - 2).join('/'), - regexp: tmp, - level: level - }); - found++; - } - dig(obj[index], trg, p, level + 1); - }, - dig = function(o, k, objpath, level) { - if (isElement(o)) { - console.log('ELEMENT'); - return; - } - var i, l; - if (o instanceof Array) { - for (i = 0, l = o.length; i < l; i++) { - maybePush(objpath, i, k, o, level); - if (opts.limit === found) break; - } - } else if (typeof o === 'object') { - for (i in o) { - maybePush(objpath, i, k, o, level); - if (opts.limit === found) break; - } - } - }; - - opts.limit = 'limit' in opts ? ~~(opts.limit) : Infinity; - opts.min = 'min' in opts ? ~~(opts.min) : 0; - opts.max = 'max' in opts ? ~~(opts.max) : Infinity; - if (opts.limit === 0) return res; - opts.min = opts.min < 0 ? 0 : opts.min; - if (opts.max < opts.min) { - t = opts.min; - opts.min = opts.max; - opts.max = t; - } - dig(rootObj, target, [], 0); - if (opts.sorter) return res.sort(opts.sorter); - return res; - } - - return { - forKey: function(o, k, opts) { - return digFor('key', o, k, opts || {}); - }, - forValue: function(o, k, opts) { - return digFor('value', o, k, opts || {}); - }, - forKeyValue: function(o, kv, opts) { - return digFor('keyvalue', o, kv, opts || {}); - } - }; -})(); -/* istanbul ignore next */ -if (typeof exports === 'object' && - typeof module !== 'undefined') { - // eslint-disable-next-line no-undef - module.exports = searchHash; -} \ No newline at end of file +var searchHash=function(){function n(n,t){return JSON.stringify(n)===JSON.stringify(t)&&!e(t)}function t(n){return"string"==typeof n||n instanceof String}function e(n){return n instanceof RegExp}function r(n){var t=String(n)!==n,e=n===Object(n),r="function"!=typeof n,i={}.toString.call(n).match(/\[object\sObject\]/);return t&&e&&r&&!(!i||!i.length)}function i(n){var t={}.toString.call(n).match(/\[object\sArray\]/);return String(n)!==n&&!(!t||!t.length)}function o(n){return n&&"object"==typeof n&&void 0!==n.nodeType&&1===n.nodeType&&"string"==typeof n.nodeName}function u(u,a,c,f){if(!r(a)&&!i(a))throw new Error("BAD PARAM: must search into an object or an array");var l,m=0,y=function(r,i){return t(r)&&e(i)?r.match(i):n(r,i)},s={key:function(n,t,e){return"function"==typeof e?e(n):y(n,e)},value:function(n,t,e){return"function"==typeof e?e(t):y(t,e)},keyvalue:function(n,t,e){return("function"==typeof e.key&&e.key(n)||y(n,e.key))&&("function"==typeof e.value&&e.value(t)||y(t,e.value))}}[u],p=[],g=function(n,t,e,r,i){var o=[].concat.call(n,[t]),u=s(t,r[t],e),c=f.min<=i&&i<=f.max,l=o.length;c&&u&&(p.push({obj:r,value:r[t],key:o[l-1],parentKey:o[l-2],path:o.join("/"),getter:function(){return o.reduce(function(n,t){return n[t]},a)},container:o.slice(0,l-1).join("/"),parentContainer:o.slice(0,l-2).join("/"),regexp:u,level:i}),m++),v(r[t],e,o,i+1)},v=function(n,t,e,r){if(o(n))return void console.log("ELEMENT");var i,u;if(n instanceof Array)for(i=0,u=n.length;i acc[el], rootObj); + return p.reduce(function(acc, el) { return acc[el]; }, rootObj); }, container: p.slice(0, plen - 1).join('/'), parentContainer: p.slice(0, plen - 2).join('/'), diff --git a/yarn.lock b/yarn.lock index 88be15f..50dc74b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1667,7 +1667,7 @@ malta-header-comment@^1.0.5: resolved "https://registry.yarnpkg.com/malta-header-comment/-/malta-header-comment-1.0.10.tgz#05de5cc9a4461cd246f398c71d7802621c3c7c4c" integrity sha512-emrfCi0fbVgAuvqSzWsulcNUH6S2QD4tRPE+XyjNaTXwu583+usfbnncT3B2sPGw+Op78hnhTh/FzMOMAk3naQ== -malta-js-uglify@^1.0.2: +malta-js-uglify@^1.0.5: version "1.0.13" resolved "https://registry.yarnpkg.com/malta-js-uglify/-/malta-js-uglify-1.0.13.tgz#757a6201646437946dc50f2ef17b4c84cd82a071" integrity sha512-bu6D0YgO8O2uUEBCehwdwOcAVY+yXfFk24fDBeFam+Ee8nx5RUw6iXQfSArybkZaj7jEhGLhP2FN6IksClvAZA== @@ -1681,10 +1681,10 @@ malta-mocha@^1.0.11: dependencies: mocha "^8.1.1" -malta@^4.1.27: - version "4.1.27" - resolved "https://registry.yarnpkg.com/malta/-/malta-4.1.27.tgz#09cd7b16a698f8aaa37682c9e284c48a731d0dfc" - integrity sha512-LX1OFH6smY5BJOAozWAE4XBwpbZ/qsempxINijkTIEKrnCDxwnqqVVsCFtLldBExWNWVaj7wiNDxEzZZYW25tg== +malta@^4.1.28: + version "4.1.28" + resolved "https://registry.yarnpkg.com/malta/-/malta-4.1.28.tgz#8256257d107bf482184b3d1aa5298c84764b9497" + integrity sha512-WS2KL+KTPZxjipARQ6tgMJKuO4LPFNzZjJd9s5Ga9GJ6EEvVVZIZcyZf5WNIe8ammSMcxwSPwut2ywpki66bvQ== merge-source-map@^1.1.0: version "1.1.0"