From e1286adde55218a85dd4eee63222cc3b5463d41e Mon Sep 17 00:00:00 2001 From: fedeghe Date: Fri, 21 May 2021 23:17:59 +0200 Subject: [PATCH] better --- .gitignore | 3 +- dist/index.js | 152 ++++++++++++++++++++++++++++++++++++++++---- source/test/edge.js | 8 +++ 3 files changed, 150 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index bf9f615..38e230a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ /test /.nyc_output .printVersion -.buildNum.json \ No newline at end of file +.buildNum.json +test.js \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 49e33c6..377a396 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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 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; +} \ No newline at end of file diff --git a/source/test/edge.js b/source/test/edge.js index 5da1e52..1875949 100644 --- a/source/test/edge.js +++ b/source/test/edge.js @@ -17,6 +17,14 @@ describe('Search starts', () => { const search = sh.forValue(objs, {}, { min: -1, max: 1 }); assert.strictEqual(1, search.length); }); + + it('should return a working getter', () => { + const search = sh.forValue(objs, "y"); + assert.strictEqual(2, search.length); + assert.strictEqual(search[0].getter(), "y"); + assert.strictEqual(search[1].getter(), "y"); + }); + it('should skip elements', () => { const dom = new JSDOM(`

Hello world

`); const trg = dom.window.document.querySelector("p");