Skip to content

Commit

Permalink
partial
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeghe committed Nov 15, 2020
1 parent 8be70e9 commit 81f2ca3
Show file tree
Hide file tree
Showing 54 changed files with 648,497 additions and 179 deletions.
134 changes: 118 additions & 16 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,118 @@
'use strict';
/*
SEARCHHASH
~1KB
*/
var searchHash=function(){function e(e,t){return n(e)?e===t:JSON.stringify(e)===JSON.stringify(t)}function n(e){
return e&&"object"==typeof e&&null!==e&&(1===e.nodeType||11===e.nodeType)&&"string"==typeof e.nodeName}function t(e){return"string"==typeof e||e instanceof String}function i(e){
return e instanceof RegExp}function o(o,r,u,a){var f,c=+new Date,l=0,m=0,y={key:function(n,o,r){return"function"==typeof r?r(n):t(n)&&i(r)?n.match(r):e(n,r)},value:function(n,o,r){
return"function"==typeof r?r(o):t(o)&&i(r)?o.match(r):e(o,r)},keyvalue:function(n,o,r){
return("function"==typeof r.key&&r.key(n)||(t(n)&&i(r.key)?n.match(r.key):e(n,r.key)))&&("function"==typeof r.value&&r.value(o)||(t(o)&&i(r.value)?o.match(r.value):e(o,r.value)))}}[o],s={
timeElapsed:0,results:[]},p=function(e,n,t,i,o){var r=[].concat.call(e,[n]),u=y(n,i[n],t),f=a.min<=o&&o<=a.max,c=r.length;f&&u&&(s.results.push({obj:i,value:i[n],key:r[c-1],parentKey:r[c-2],
path:r.join("/"),container:r.slice(0,c-1).join("/"),parentContainer:r.slice(0,c-2).join("/"),regexp:u,level:o}),m++),v(i[n],t,r,o+1)},v=function(e,t,i,o){if(!n(e)){var r,u
;if(e instanceof Array)for(r=0,u=e.length;r<u&&(p(i,r,t,e,o),a.limit!==m);r++);else if("object"==typeof e)for(r in e)if(p(i,r,t,e,o),a.limit===m)break}};return a.limit="limit"in a?~~a.limit:1/0,
a.min="min"in a?~~a.min:0,a.max="max"in a?~~a.max:1/0,a.min=a.min<0?0:a.min,a.max<a.min&&(f=a.min,a.min=a.max,a.max=f),v(r,u,[],0),l=+new Date,s.timeElapsed=l-c,s}return{forKey:function(e,n,t){
return o("key",e,n,t||{})},forValue:function(e,n,t){return o("value",e,n,t||{})},forKeyValue:function(e,n,t){return o("keyvalue",e,n,t||{})}}}()
;"object"==typeof exports&&"undefined"!=typeof module&&(module.exports=searchHash);

var searchHash = (function () {
// some utility func
function jCompare (obj1, obj2) {
// if (isElement(obj1)) {
// return obj1 === obj2;
// }
return JSON.stringify(obj1) === JSON.stringify(obj2);
}
// function isElement (o) {
// return o &&
// typeof o === 'object' &&
// o !== null &&
// (o.nodeType === 1 || o.nodeType === 11) &&
// typeof o.nodeName === 'string';
// }
function isString (o) {
return typeof o === 'string' || o instanceof String;
}
function isRegExp (o) {
return o instanceof RegExp;
}

/**
* Main searching function
*/
function digFor (what, obj, target, opts) {
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('/'),
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)) 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(obj, 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 || {});
}
};
})();
typeof exports === 'object' &&
typeof module !== 'undefined' &&
(module.exports = searchHash);
71 changes: 23 additions & 48 deletions source/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@

var searchHash = (function () {
var ERRS = {
BAD1: {
type: 'BAD_PARAMS',
message: 'Either a Literal Object either an array should be passed as second parameter'
}
};

// some utility func
function jCompare (obj1, obj2) {
if (isElement(obj1)) {
return obj1 === obj2;
}
// if (isElement(obj1)) {
// return obj1 === obj2;
// }
return JSON.stringify(obj1) === JSON.stringify(obj2);
}
function isElement (o) {
return o &&
typeof o === 'object' &&
o !== null &&
(o.nodeType === 1 || o.nodeType === 11) &&
typeof o.nodeName === 'string';
}
// function isElement (o) {
// return o &&
// typeof o === 'object' &&
// o !== null &&
// (o.nodeType === 1 || o.nodeType === 11) &&
// typeof o.nodeName === 'string';
// }
function isString (o) {
return typeof o === 'string' || o instanceof String;
}
Expand All @@ -33,53 +26,37 @@ var searchHash = (function () {
*/
function digFor (what, obj, target, opts) {
var t,
sTime = +new Date(),
eTime = 0,
found = 0,
strOrRx = function (x, y) {
return (isString(x) && isRegExp(y))
? x.match(y)
: jCompare(x, y);
},
matches = {
key: function (k1, k2, key) {
if (typeof key === 'function') {
return key(k1);
}
return (isString(k1) && isRegExp(key))
? k1.match(key)
: jCompare(k1, key);
return typeof key === 'function' ? key(k1) : strOrRx(k1, key);
},
value: function (k1, k2, val) {
if (typeof val === 'function') {
return val(k2);
}
return (isString(k2) && isRegExp(val))
? k2.match(val)
: jCompare(k2, val);
return typeof val === 'function' ? val(k2) : strOrRx(k2, val);
},
keyvalue: function (k1, k2, keyval) {
return (
(typeof keyval.key === 'function' && keyval.key(k1)) ||
((isString(k1) && isRegExp(keyval.key))
? k1.match(keyval.key)
: jCompare(k1, keyval.key)
)
strOrRx(k1, keyval.key)
) && (
(typeof keyval.value === 'function' && keyval.value(k2)) ||
((isString(k2) && isRegExp(keyval.value))
? k2.match(keyval.value)
: jCompare(k2, keyval.value)
)
strOrRx(k2, keyval.value)
);
}
}[what],
res = {
timeElapsed: 0,
results: []
},
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.results.push({
res.push({
obj: obj,
value: obj[index],
key: p[plen - 1],
Expand All @@ -95,7 +72,7 @@ var searchHash = (function () {
dig(obj[index], trg, p, level + 1);
},
dig = function (o, k, objpath, level) {
if (isElement(o)) return;
// if (isElement(o)) return;
var i, l;
if (o instanceof Array) {
for (i = 0, l = o.length; i < l; i++) {
Expand All @@ -113,16 +90,14 @@ var searchHash = (function () {
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(obj, target, [], 0);
eTime = +new Date();
res.timeElapsed = eTime - sTime;
return res;
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
81 changes: 33 additions & 48 deletions source/test/data/deeper.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,41 @@
[
{
"name": "Federico",
"info": {
"hobbies": [
{
"name": "ede"
}
]
}
},{
"name": "Gabriele",
"info": {
"hobbies": [
{
"name": "ede"
}
]
}
},
"one",
{
"a": "ONE",
"b": {
"a1": "ONE",
"b1": {
"a2": "one",
"b2": {
"a3": "one"
},
"x": [
"ONE",
"two",
"tone",
"one"
],
"a": {
"b": {
"c": {
"d": {
"e": {
"f": {
"g": {
"h": "oNE",
"t" : "twone"
},
"two" :"two"
"a": "it's an a",
"a1": "it's an a1",
"_a1": "it's an _a1",
"b": {},
"c": null,
"d": {
"a": {
"n": null,
"b": {
"c": {
"d": {
"e": {
"s": null,
"f": {
"g": {
"h": [
{
"a": 1,
"w": null,
"z": {
"a": false
}
},
{
"1a": "name"
}
]
}
}
}
},
"name": "y"
}
}
}
},
"x": "name"
},
"name": "x"
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions source/test/forkey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const assert = require('assert'),
sh = require('../dist/index.js'),
objs = require('./data/deeper.json');

describe('Search starts', () => {
describe('search for key as string', () => {
it('should find 4 elements', () => {
const search = sh.forKey(objs, 'a');
assert.strictEqual(4, search.length);
});

it('should find 1 elements, using limit 1', () => {
const search = sh.forKey(objs, 'a', {limit:1});
assert.strictEqual(1, search.length);
});

it('should find 0 elements, with limit 0 (edge case)', () => {
const search = sh.forKey(objs, 'a', {limit:0});
assert.strictEqual(0, search.length);
});

it('should find 0 elements on first level; arrays has no keys indeed :)', () => {
const search = sh.forKey(objs, 'a', {max: 0});
assert.strictEqual(0, search.length);
});
});



});

0 comments on commit 81f2ca3

Please sign in to comment.