Skip to content

Commit

Permalink
removed elapsedTime, test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeghe committed Nov 16, 2020
1 parent 81f2ca3 commit b666094
Show file tree
Hide file tree
Showing 36 changed files with 404 additions and 324,663 deletions.
133 changes: 15 additions & 118 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 14 additions & 11 deletions source/index.js
Expand Up @@ -2,29 +2,32 @@
var searchHash = (function () {
// some utility func
function jCompare (obj1, obj2) {
// if (isElement(obj1)) {
// return obj1 === obj2;
// }
return JSON.stringify(obj1) === JSON.stringify(obj2);
return JSON.stringify(obj1) === JSON.stringify(obj2) && !isRegExp(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;
}
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);
}

/**
* Main searching function
*/
function digFor (what, obj, target, opts) {
if (!isObj(obj) && !isArr(obj)) throw new Error('BAD PARAM: must search into an object or an array');
var t,
found = 0,
strOrRx = function (x, y) {
Expand Down
83 changes: 83 additions & 0 deletions source/test/data.js
@@ -0,0 +1,83 @@
const assert = require('assert'),
sh = require('../dist/index.js'),
countries = require('./data/countries.json'),
licences = require('./data/licenses.json'),
laureate = require('./data/laureate.json'),
big = require('./data/big.json'),
theme = require('./data/theme.json');


describe('Search starts', () => {

describe('Countrynames search', () => {
it('should find 3 elements containing `az`', () => {
const search = sh.forValue(countries, /az/);
assert.strictEqual(3, search.length);
});
it('should find 27 elements containing `al`', () => {
const search = sh.forValue(countries, /al/);
assert.strictEqual(27, search.length);
});
});

describe('Licences search', () => {
it('should find 4 elements containing `MIT`', () => {
const search = sh.forValue(licences, 'MIT');
assert.strictEqual(4, search.length);
});
it('should find 1 elements name:MIT', () => {
const search = sh.forKeyValue(licences, {key:"name", value:"MIT"});
assert.strictEqual(1, search.length);
});
});

describe('Laureate search', () => {
const search = sh.forValue(laureate, 'Albert');
it('should find 6 elements with value `Albert`', () => {
assert.strictEqual(6, search.length);
});
it('one of those has to be `Einstein`', () => {
const Einstein = sh.forValue(search, 'Einstein');
assert.strictEqual(1, Einstein.length);
});
});

describe('Search in a big file', () => {
it('should find 181 companies with a double `l` on the name', () => {
const search = sh.forKeyValue(big, { key: "company", value: /LL/ })
assert.strictEqual(181, search.length);
});
it('should find 3245 companies with an `A` on the name', () => {
const search = sh.forKeyValue(big, { key: "company", value: /A/ })
assert.strictEqual(3245, search.length);
});
it('should find 2109 companies with an `A` in the address', () => {
const search = sh.forKeyValue(big, { key: "address", value: /A/ })
assert.strictEqual(2109, search.length);
});
it('should find 6137 companies with an `A` in the address, caseinsensitive', () => {
const search = sh.forKeyValue(big, { key: "address", value: /A/i })
assert.strictEqual(6137, search.length);
});
});

describe('Search in a deep file using less trivial regexp', () => {
it('should find 13 elements hex3', () => {
const search = sh.forValue(theme, /^#[0-9a-z]{3}$/i);
assert.strictEqual(13, search.length);
});
it('should find 4 elements hex6', () => {
const search = sh.forValue(theme, /^#[0-9a-z]{6}$/i);
assert.strictEqual(4, search.length);
});
it('should find 10 elements rgb', () => {
const search = sh.forValue(theme, /^rgb\(/);
assert.strictEqual(10, search.length);
});
it('should find 12 elements rgba', () => {
const search = sh.forValue(theme, /^rgba\(/);
assert.strictEqual(12, search.length);
});
});

});
File renamed without changes.
File renamed without changes.

0 comments on commit b666094

Please sign in to comment.