Skip to content

Commit

Permalink
feat: add verbose strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Fetz committed May 17, 2018
1 parent c6b1135 commit 8df4a97
Show file tree
Hide file tree
Showing 9 changed files with 1,279 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/bin/json-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const executeAction = (json, cmd) => {
const result = analyze({
json: parsed,
target: cmd.target,
maxDepth: cmd.depth
maxDepth: cmd.depth,
verbose: cmd.verbose
});

console.log(prettyjson.render(result));
Expand All @@ -42,6 +43,7 @@ program
.version(version)
.description('json analyzer')
.usage('[file] [options]')
.option('--verbose', 'verbose')
.option('--depth <n>', 'depth', parseInt)
.option('--target <value>', 'propA.propB[x]');

Expand Down
7 changes: 6 additions & 1 deletion src/lib/json-analyzer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const get = require('lodash.get');
const simpleStrategy = require('./simple');
const verboseStrategy = require('./verbose');

function jsonAnalyzer({ json, target, maxDepth }) {
function jsonAnalyzer({ json, target, maxDepth, verbose = false }) {
const targetNode = target ? get(json, target) : json;

if (verbose) {
return verboseStrategy({ json: targetNode, maxDepth });
}

return simpleStrategy({ json: targetNode });
}

Expand Down
45 changes: 45 additions & 0 deletions src/lib/json-analyzer.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,62 @@
const get = require('lodash.get');
const simpleStrategy = require('./simple');
const verboseStrategy = require('./verbose');
const jsonAnalyzer = require('./json-analyzer');

jest.mock('lodash.get');
jest.mock('./simple');
jest.mock('./verbose');

describe('jsonAnalyzer', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('verboseStrategy', () => {
it('should delegate to verboseStrategy', () => {
jsonAnalyzer({ verbose: true });
expect(verboseStrategy).toHaveBeenCalled();
});

it('should use default json if no target provided', () => {
const expected = Symbol('json');
jsonAnalyzer({ verbose: true, json: expected });
expect(verboseStrategy).toHaveBeenCalledWith(
expect.objectContaining({ json: expected })
);
});

it('should use get if target is provided', () => {
const expected = Symbol('targetNode');
get.mockReturnValueOnce(expected);

const target = Symbol('target');
const json = Symbol('json');
jsonAnalyzer({ verbose: true, json, target });

expect(get).toHaveBeenCalledWith(json, target);
expect(verboseStrategy).toHaveBeenCalledWith(
expect.objectContaining({ json: expected })
);
});

it('should provide maxDepth to verboseStrategy', () => {
const expected = Symbol('maxDepth');

jsonAnalyzer({ verbose: true, maxDepth: expected });
expect(verboseStrategy).toHaveBeenCalledWith(
expect.objectContaining({ maxDepth: expected })
);
});
});

describe('simpleStrategy', () => {
it('should delegate to simpleStrategy', () => {
jsonAnalyzer({ verbose: false });
expect(simpleStrategy).toHaveBeenCalled();
});

it('should delegate to simpleStrategy even if verbose is not provided', () => {
jsonAnalyzer({});
expect(simpleStrategy).toHaveBeenCalled();
});
Expand Down
Loading

0 comments on commit 8df4a97

Please sign in to comment.