Skip to content

Commit 4e9d484

Browse files
committed
feat: options in find
1 parent 1fe540d commit 4e9d484

3 files changed

Lines changed: 99 additions & 11 deletions

File tree

lib/find.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var includes = require('lodash.includes')
4+
35
/**
46
* Finds specific keys in an array
57
*
@@ -10,11 +12,23 @@
1012
* @return {Array} an array of found objects
1113
*/
1214
function find (jsonObject, options, findString) {
15+
var typeCache;
16+
1317
if (typeof options === 'string') {
1418
findString = options;
1519
options = {};
1620
}
1721

22+
options.type = options.type || [];
23+
options.max = options.max || -1;
24+
25+
if (typeof options.type === 'string') {
26+
typeCache = options.type;
27+
28+
options.type = [];
29+
options.type.push(typeCache);
30+
}
31+
1832
return matchJsonKey(jsonObject, options, findString)
1933
}
2034

@@ -41,18 +55,31 @@ function matchJsonKey (jsonObject, options, findString) {
4155
typeOfString = 'array';
4256
}
4357

44-
resultArray.push({
45-
key: findString,
46-
type: typeOfString,
47-
data: value
48-
});
58+
// add if either
59+
// all are allowed => empty array or
60+
// if the specific type is in option.type
61+
if (options.type.length === 0 || includes(options.type, typeOfString)) {
62+
resultArray.push({
63+
key: findString,
64+
type: typeOfString,
65+
data: value
66+
});
67+
68+
if (options.max !== -1 && resultArray.length === options.max) {
69+
return resultArray
70+
}
71+
}
4972
}
5073

5174
// recursive if it is an object
5275
if (Object.prototype.toString.call(value) === '[object Object]') {
5376
var recursiveArray = matchJsonKey(value, options, findString)
5477

5578
resultArray = resultArray.concat(recursiveArray)
79+
80+
if (options.max !== -1 && resultArray.length >= options.max) {
81+
return resultArray
82+
}
5683
}
5784
}
5885

readme.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Check if it is a valid string or object. Just do a `JSON.parse` but with `try -
5151

5252
Alias: `isJson()`
5353

54+
Example:
55+
5456
```js
5557
var json = require('json-extra')
5658

@@ -66,6 +68,8 @@ If you want to change your json string into a path just hit this method.
6668
`base` in an object is always the name of the folder.
6769
`subfolders` create new subfolders
6870

71+
Example:
72+
6973
```js
7074
var json = require('json-extra')
7175

@@ -94,6 +98,8 @@ Read a json file and returns an obj.
9498

9599
Sync: `readToObjSync()`
96100

101+
Example:
102+
97103
```js
98104
var json = require('json-extra')
99105

@@ -116,6 +122,8 @@ Alias: `write()`<br>
116122
Sync: `createSync()`<br>
117123
Sync-Alias: `writeSync()`
118124

125+
Example:
126+
119127
```js
120128
var json = require('json-extra')
121129

@@ -133,6 +141,13 @@ json.create('/any/path/you/want', 'filename.json', '{json: "string or object"}',
133141

134142
Finds a specific key in the json
135143

144+
Options:
145+
146+
- type (array | string): Get specific types. Available options: `array`, `object`, `string`, `boolean` or `number`
147+
- max (boolean): The maximum of keys to find. Default: `-1` alias unlimited
148+
149+
Example:
150+
136151
```js
137152
var json = require('json-extra')
138153
var myJsonObejct = json.readToObj('./package.json')

test/find.spec.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var base = require('../');
44
var expect = require('chai').expect;
55

66
describe('find.js', function () {
7-
it('should return the expected output', function (done) {
7+
it('should return the expected output', function () {
88
var jsonfile = {
99
path: 'a path',
1010
nested: {
@@ -24,11 +24,9 @@ describe('find.js', function () {
2424
expect(foundKeys[0].data).to.be.an('string');
2525
expect(foundKeys[2].data).to.be.an('array');
2626
expect(foundKeys[0].data).to.equal('find this string');
27-
28-
done();
2927
});
3028

31-
it('should check if it is an array or object', function (done) {
29+
it('should check if it is an array or object', function () {
3230
var jsonfile = {
3331
findme: ['test'],
3432
test: {
@@ -43,7 +41,55 @@ describe('find.js', function () {
4341
expect(foundKeys[0].type).to.equal('array');
4442
expect(foundKeys[1].data).to.be.an('object');
4543
expect(foundKeys[1].type).to.equal('object');
44+
});
45+
46+
describe('check its options', function() {
47+
var jsonfile;
48+
49+
beforeEach(function () {
50+
jsonfile = {
51+
path: 'a path',
52+
nested: {
53+
path: 'a second path',
54+
findme: 'find this string',
55+
nested: {
56+
path: 'a third path',
57+
findme: {test: 'find this an object'}
58+
},
59+
},
60+
findme: ['this is an array']
61+
};
62+
});
63+
64+
it('type: should check the type option as string', function () {
65+
var foundKeys = base.find(jsonfile, {
66+
type: 'string'
67+
}, 'findme');
68+
69+
expect(foundKeys.length).to.equal(1);
70+
expect(foundKeys[0].type).to.equal('string');
71+
});
72+
73+
it('type: should check the type option as array', function () {
74+
var foundKeys = base.find(jsonfile, {
75+
type: ['object', 'array']
76+
}, 'findme');
77+
78+
expect(foundKeys.length).to.equal(2);
79+
expect(foundKeys[0].type).to.equal('object');
80+
expect(foundKeys[1].type).to.equal('array');
81+
});
82+
83+
it('max: should check if the maximum is reduced', function () {
84+
var foundKeys = base.find(jsonfile, {
85+
max: 1
86+
}, 'findme');
87+
var foundKeys2 = base.find(jsonfile, {
88+
max: 2
89+
}, 'findme');
4690

47-
done();
91+
expect(foundKeys.length).to.equal(1);
92+
expect(foundKeys2.length).to.equal(2);
93+
});
4894
});
49-
});
95+
});

0 commit comments

Comments
 (0)