Skip to content

Commit

Permalink
feat: options in find
Browse files Browse the repository at this point in the history
  • Loading branch information
JPeer264 committed Nov 30, 2016
1 parent 1fe540d commit 4e9d484
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 11 deletions.
37 changes: 32 additions & 5 deletions lib/find.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var includes = require('lodash.includes')

/**
* Finds specific keys in an array
*
Expand All @@ -10,11 +12,23 @@
* @return {Array} an array of found objects
*/
function find (jsonObject, options, findString) {
var typeCache;

if (typeof options === 'string') {
findString = options;
options = {};
}

options.type = options.type || [];
options.max = options.max || -1;

if (typeof options.type === 'string') {
typeCache = options.type;

options.type = [];
options.type.push(typeCache);
}

return matchJsonKey(jsonObject, options, findString)
}

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

resultArray.push({
key: findString,
type: typeOfString,
data: value
});
// add if either
// all are allowed => empty array or
// if the specific type is in option.type
if (options.type.length === 0 || includes(options.type, typeOfString)) {
resultArray.push({
key: findString,
type: typeOfString,
data: value
});

if (options.max !== -1 && resultArray.length === options.max) {
return resultArray
}
}
}

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

resultArray = resultArray.concat(recursiveArray)

if (options.max !== -1 && resultArray.length >= options.max) {
return resultArray
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Check if it is a valid string or object. Just do a `JSON.parse` but with `try -

Alias: `isJson()`

Example:

```js
var json = require('json-extra')

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

Example:

```js
var json = require('json-extra')

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

Sync: `readToObjSync()`

Example:

```js
var json = require('json-extra')

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

Example:

```js
var json = require('json-extra')

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

Finds a specific key in the json

Options:

- type (array | string): Get specific types. Available options: `array`, `object`, `string`, `boolean` or `number`
- max (boolean): The maximum of keys to find. Default: `-1` alias unlimited

Example:

```js
var json = require('json-extra')
var myJsonObejct = json.readToObj('./package.json')
Expand Down
58 changes: 52 additions & 6 deletions test/find.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var base = require('../');
var expect = require('chai').expect;

describe('find.js', function () {
it('should return the expected output', function (done) {
it('should return the expected output', function () {
var jsonfile = {
path: 'a path',
nested: {
Expand All @@ -24,11 +24,9 @@ describe('find.js', function () {
expect(foundKeys[0].data).to.be.an('string');
expect(foundKeys[2].data).to.be.an('array');
expect(foundKeys[0].data).to.equal('find this string');

done();
});

it('should check if it is an array or object', function (done) {
it('should check if it is an array or object', function () {
var jsonfile = {
findme: ['test'],
test: {
Expand All @@ -43,7 +41,55 @@ describe('find.js', function () {
expect(foundKeys[0].type).to.equal('array');
expect(foundKeys[1].data).to.be.an('object');
expect(foundKeys[1].type).to.equal('object');
});

describe('check its options', function() {
var jsonfile;

beforeEach(function () {
jsonfile = {
path: 'a path',
nested: {
path: 'a second path',
findme: 'find this string',
nested: {
path: 'a third path',
findme: {test: 'find this an object'}
},
},
findme: ['this is an array']
};
});

it('type: should check the type option as string', function () {
var foundKeys = base.find(jsonfile, {
type: 'string'
}, 'findme');

expect(foundKeys.length).to.equal(1);
expect(foundKeys[0].type).to.equal('string');
});

it('type: should check the type option as array', function () {
var foundKeys = base.find(jsonfile, {
type: ['object', 'array']
}, 'findme');

expect(foundKeys.length).to.equal(2);
expect(foundKeys[0].type).to.equal('object');
expect(foundKeys[1].type).to.equal('array');
});

it('max: should check if the maximum is reduced', function () {
var foundKeys = base.find(jsonfile, {
max: 1
}, 'findme');
var foundKeys2 = base.find(jsonfile, {
max: 2
}, 'findme');

done();
expect(foundKeys.length).to.equal(1);
expect(foundKeys2.length).to.equal(2);
});
});
});
});

0 comments on commit 4e9d484

Please sign in to comment.