From 4e9d484488e5721d6d0bd27ec027b5753e931182 Mon Sep 17 00:00:00 2001 From: JPeer264 Date: Wed, 30 Nov 2016 19:05:34 +0100 Subject: [PATCH] feat: options in find --- lib/find.js | 37 ++++++++++++++++++++++++++---- readme.md | 15 ++++++++++++ test/find.spec.js | 58 ++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 99 insertions(+), 11 deletions(-) diff --git a/lib/find.js b/lib/find.js index a5deecf..9d637c7 100644 --- a/lib/find.js +++ b/lib/find.js @@ -1,5 +1,7 @@ 'use strict'; +var includes = require('lodash.includes') + /** * Finds specific keys in an array * @@ -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) } @@ -41,11 +55,20 @@ 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 @@ -53,6 +76,10 @@ function matchJsonKey (jsonObject, options, findString) { var recursiveArray = matchJsonKey(value, options, findString) resultArray = resultArray.concat(recursiveArray) + + if (options.max !== -1 && resultArray.length >= options.max) { + return resultArray + } } } diff --git a/readme.md b/readme.md index 5111503..ab3f762 100644 --- a/readme.md +++ b/readme.md @@ -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') @@ -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') @@ -94,6 +98,8 @@ Read a json file and returns an obj. Sync: `readToObjSync()` +Example: + ```js var json = require('json-extra') @@ -116,6 +122,8 @@ Alias: `write()`
Sync: `createSync()`
Sync-Alias: `writeSync()` +Example: + ```js var json = require('json-extra') @@ -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') diff --git a/test/find.spec.js b/test/find.spec.js index 848993e..bbbc6e7 100644 --- a/test/find.spec.js +++ b/test/find.spec.js @@ -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: { @@ -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: { @@ -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); + }); }); -}); \ No newline at end of file +});