Skip to content

Commit

Permalink
Merge pull request #1532 from blackflux/dev
Browse files Browse the repository at this point in the history
[Gally]: master <- dev
  • Loading branch information
simlu authored Apr 25, 2021
2 parents da0d148 + 69108f3 commit 3abc0af
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 163 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ where:
- `traversedBy`: all needles involved in traversing key.
- `isCircular`: true iff `value` contained in `parents`
- `isLeaf`: true iff `value` can not be traversed
- `depth`: length of `key`
- `getKey`: function that returns `key`
- `getValue`: function that returns `value`
- `getEntry`: function that returns `entry`
Expand All @@ -340,6 +341,7 @@ where:
- `getTraversedBy`: function that returns `traversedBy`
- `getIsCircular`: function that returns `isCircular`
- `getIsLeaf`: function that returns `isLeaf`
- `getDepth`: function that returns `depth`
- `context`: as passed into the search

Notes on Performance:
Expand Down Expand Up @@ -530,12 +532,19 @@ Defaults to `key` when search context is _undefined_ and to `context` otherwise.

Can be explicitly set as:
- `context`: search context is returned
- `key`: matched keys are returned
- `value`: matched values are returned
- `entry`: matched entries are returned
- `property`: matched properties are returned
- `parent`: matched parent are returned
- `parents`: matched parents are returned
- `key`: as passed into `filterFn`
- `value`: as passed into `filterFn`
- `entry`: as passed into `filterFn`
- `property`: as passed into `filterFn`
- `parent`: as passed into `filterFn`
- `parents`: as passed into `filterFn`
- `isMatch`: as passed into `filterFn`
- `matchedBy`: as passed into `filterFn`
- `excludedBy`: as passed into `filterFn`
- `traversedBy`: as passed into `filterFn`
- `isCircular`: as passed into `filterFn`
- `isLeaf`: as passed into `filterFn`
- `depth`: as passed into `filterFn`
- `bool`: returns _true_ iff a match is found
- `count`: returns the match count

Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@
},
"homepage": "https://github.com/blackflux/object-scan#readme",
"devDependencies": {
"@babel/cli": "7.13.10",
"@babel/core": "7.13.10",
"@babel/register": "7.13.8",
"@babel/cli": "7.13.14",
"@babel/core": "7.13.16",
"@babel/register": "7.13.16",
"@blackflux/eslint-plugin-rules": "2.0.3",
"@blackflux/robo-config-plugin": "5.0.0",
"@blackflux/robo-config-plugin": "5.0.3",
"babel-eslint": "10.1.0",
"babel-preset-latest-node": "5.5.1",
"chai": "4.3.4",
"coveralls": "3.1.0",
"diff": "5.0.0",
"diff2html": "3.4.0",
"eslint": "7.22.0",
"diff2html": "3.4.3",
"eslint": "7.24.0",
"eslint-config-airbnb-base": "14.2.1",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-json": "2.1.2",
"eslint-plugin-markdown": "2.0.0",
"eslint-plugin-markdown": "2.0.1",
"eslint-plugin-mocha": "8.1.0",
"joi-strict": "2.0.0",
"js-gardener": "3.0.3",
"json-stringify-pretty-compact": "3.0.0",
"lodash.isequal": "4.5.0",
"mustache": "4.1.0",
"mustache": "4.2.0",
"node-tdd": "3.0.4",
"nyc": "15.1.0",
"object-scan": "13.9.1",
Expand Down
4 changes: 4 additions & 0 deletions src/core/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ module.exports = (haystack_, searches_, ctx) => {
get isLeaf() {
return kwargs.getIsLeaf();
},
getDepth: () => path.length,
get depth() {
return kwargs.getDepth();
},
context: ctx.context
};

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module.exports = (needles, opts = {}) => {
undefined, 'context',
'key', 'value', 'entry',
'property', 'parent', 'parents',
'isMatch', 'matchedBy', 'excludedBy',
'traversedBy', 'isCircular', 'isLeaf', 'depth',
'bool', 'count'
].includes(opts.rtn));
assert(typeof ctx.joined === 'boolean');
Expand Down
40 changes: 27 additions & 13 deletions test/helper.spec/call-signature.spec.js__fixtures/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
'**'
],
useArraySelector: true,
reverse: true,
logs: [
{
cbName: 'breakFn',
Expand All @@ -40,7 +41,8 @@ module.exports = {
'**'
],
isCircular: false,
isLeaf: false
isLeaf: false,
depth: 0
},
{
cbName: 'breakFn',
Expand Down Expand Up @@ -90,7 +92,8 @@ module.exports = {
'**'
],
isCircular: false,
isLeaf: false
isLeaf: false,
depth: 1
},
{
cbName: 'breakFn',
Expand Down Expand Up @@ -146,7 +149,8 @@ module.exports = {
'**'
],
isCircular: false,
isLeaf: false
isLeaf: false,
depth: 2
},
{
cbName: 'breakFn',
Expand Down Expand Up @@ -203,7 +207,8 @@ module.exports = {
'**'
],
isCircular: false,
isLeaf: false
isLeaf: false,
depth: 3
},
{
cbName: 'breakFn',
Expand Down Expand Up @@ -256,7 +261,8 @@ module.exports = {
'**'
],
isCircular: false,
isLeaf: true
isLeaf: true,
depth: 4
},
{
cbName: 'filterFn',
Expand Down Expand Up @@ -309,7 +315,8 @@ module.exports = {
'**'
],
isCircular: false,
isLeaf: true
isLeaf: true,
depth: 4
},
{
cbName: 'filterFn',
Expand Down Expand Up @@ -366,7 +373,8 @@ module.exports = {
'**'
],
isCircular: false,
isLeaf: false
isLeaf: false,
depth: 3
},
{
cbName: 'breakFn',
Expand Down Expand Up @@ -423,7 +431,8 @@ module.exports = {
'**'
],
isCircular: false,
isLeaf: false
isLeaf: false,
depth: 3
},
{
cbName: 'breakFn',
Expand Down Expand Up @@ -476,7 +485,8 @@ module.exports = {
'**'
],
isCircular: false,
isLeaf: true
isLeaf: true,
depth: 4
},
{
cbName: 'filterFn',
Expand Down Expand Up @@ -529,7 +539,8 @@ module.exports = {
'**'
],
isCircular: false,
isLeaf: true
isLeaf: true,
depth: 4
},
{
cbName: 'filterFn',
Expand Down Expand Up @@ -586,7 +597,8 @@ module.exports = {
'**'
],
isCircular: false,
isLeaf: false
isLeaf: false,
depth: 3
},
{
cbName: 'filterFn',
Expand Down Expand Up @@ -642,7 +654,8 @@ module.exports = {
'**'
],
isCircular: false,
isLeaf: false
isLeaf: false,
depth: 2
},
{
cbName: 'filterFn',
Expand Down Expand Up @@ -692,7 +705,8 @@ module.exports = {
'**'
],
isCircular: false,
isLeaf: false
isLeaf: false,
depth: 1
}
],
warning: null,
Expand Down
15 changes: 10 additions & 5 deletions test/helper/call-signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module.exports = ({
objectScan,
haystack,
needles,
useArraySelector = true
useArraySelector = true,
reverse = true
}) => {
const logs = [];
const cb = (cbName) => ({
Expand All @@ -15,7 +16,8 @@ module.exports = ({
excludedBy,
traversedBy,
isCircular,
isLeaf
isLeaf,
depth
}) => {
logs.push({
cbName,
Expand All @@ -28,19 +30,21 @@ module.exports = ({
excludedBy,
traversedBy,
isCircular,
isLeaf
isLeaf,
depth
});
};
const result = objectScan(needles, {
strict: false,
joined: true,
useArraySelector,
reverse,
filterFn: cb('filterFn'),
breakFn: cb('breakFn')
})(haystack);

const startCompile = process.hrtime();
const scanner = objectScan(needles, { strict: false, useArraySelector });
const scanner = objectScan(needles, { strict: false, useArraySelector, reverse });
const diffCompile = process.hrtime(startCompile);
const durationCompile = diffCompile[0] * 1e9 + diffCompile[1];
const startTraverse = process.hrtime();
Expand All @@ -50,14 +54,15 @@ module.exports = ({

let warning = null;
try {
objectScan(needles, { useArraySelector });
objectScan(needles, { useArraySelector, reverse });
} catch (e) {
warning = e.message;
}
return {
haystack,
needles,
useArraySelector,
reverse,
logs,
warning,
duration: {
Expand Down
52 changes: 43 additions & 9 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,19 +572,19 @@ describe('Testing Find', () => {
it('Testing traversedBy on filterFn', () => {
const result = [];
objectScan(['**', ''], {
filterFn: ({ key, isCircular }) => {
result.push([key, isCircular]);
filterFn: ({ key, depth, isCircular }) => {
result.push([key, depth, isCircular]);
},
breakFn: ({ key }) => key.length > 10
})(input);
expect(result).to.deep.equal([
['a.b.a.b.a.b', true],
['a.b.a.b.a', true],
['a.b.a.b', true],
['a.b.a', true],
['a.b', true],
['a', false],
['', false]
['a.b.a.b.a.b', 6, true],
['a.b.a.b.a', 5, true],
['a.b.a.b', 4, true],
['a.b.a', 3, true],
['a.b', 2, true],
['a', 1, false],
['', 0, false]
]);
});
});
Expand Down Expand Up @@ -618,6 +618,40 @@ describe('Testing Find', () => {
});
});

describe('Testing rtn', () => {
const input = { f: { b: { a: null, d: { c: {}, e: {} } }, g: { i: { h: {} } } } };
// eslint-disable-next-line mocha/no-setup-in-describe
input.f.g = input;
const pattern = ['*.b.a', '!f.g', '*.g'];

// eslint-disable-next-line mocha/no-setup-in-describe
Object.entries({
context: undefined,
key: ['f.g', 'f.b.a'],
value: [input, null],
entry: [['f.g', input], ['f.b.a', null]],
property: ['g', 'a'],
// eslint-disable-next-line mocha/no-setup-in-describe
parent: [input.f, input.f.b],
// eslint-disable-next-line mocha/no-setup-in-describe
parents: [[input.f, input], [input.f.b, input.f, input]],
isMatch: [true, true],
matchedBy: [['*.g'], ['*.b.a']],
excludedBy: [['!f.g'], []],
traversedBy: [['*.g', '!f.g'], ['*.b.a']],
isCircular: [true, false],
isLeaf: [false, true],
depth: [2, 3],
bool: true,
count: 2
}).forEach(([rtn, expected]) => {
it(`Testing ${rtn}`, () => {
const r = objectScan(pattern, { rtn })(input);
expect(r).to.deep.equal(expected);
});
});
});

describe('Testing Fn needle', () => {
const input = [{ parent: { child: 'value' } }];
const pattern = ['[*].*.child', '[*].parent'];
Expand Down
Loading

0 comments on commit 3abc0af

Please sign in to comment.