-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
150 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
/test | ||
/.nyc_output | ||
.printVersion | ||
.buildNum.json | ||
.buildNum.json | ||
test.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,144 @@ | ||
'use strict'; | ||
/* | ||
SEARCHHASH v1.2.2 | ||
~2KB | ||
~4KB | ||
*/ | ||
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 i(n){var t=String(n)!==n,e=n===Object(n),i="function"!=typeof n,r={}.toString.call(n).match(/\[object\sObject\]/);return t&&e&&i&&!(!r||!r.length)}function r(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,f,c){if(!i(a)&&!r(a))throw new Error("BAD PARAM: must search into an object or an array");var l,m=0,y=function(i,r){return t(i)&&e(r)?i.match(r):n(i,r)},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=[],v=function(n,t,e,i,r){ | ||
var o=[].concat.call(n,[t]),u=s(t,i[t],e),a=c.min<=r&&r<=c.max,f=o.length;a&&u&&(p.push({obj:i,value:i[t],key:o[f-1],parentKey:o[f-2],path:o.join("/"),container:o.slice(0,f-1).join("/"), | ||
parentContainer:o.slice(0,f-2).join("/"),regexp:u,level:r}),m++),g(i[t],e,o,r+1)},g=function(n,t,e,i){if(o(n))return void console.log("ELEMENT");var r,u;if(n instanceof Array)for(r=0, | ||
u=n.length;r<u&&(v(e,r,t,n,i),c.limit!==m);r++);else if("object"==typeof n)for(r in n)if(v(e,r,t,n,i),c.limit===m)break};return c.limit="limit"in c?~~c.limit:1/0,c.min="min"in c?~~c.min:0, | ||
c.max="max"in c?~~c.max:1/0,0===c.limit?p:(c.min=c.min<0?0:c.min,c.max<c.min&&(l=c.min,c.min=c.max,c.max=l),g(a,f,[],0),p)}return{forKey:function(n,t,e){return u("key",n,t,e||{})}, | ||
forValue:function(n,t,e){return u("value",n,t,e||{})},forKeyValue:function(n,t,e){return u("keyvalue",n,t,e||{})}}}();"object"==typeof exports&&"undefined"!=typeof module&&(module.exports=searchHash); | ||
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); | ||
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 || {}); | ||
} | ||
}; | ||
})(); | ||
if (typeof exports === 'object' && | ||
typeof module !== 'undefined') { | ||
// eslint-disable-next-line no-undef | ||
module.exports = searchHash; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters