Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Commit

Permalink
fix: remove dependency on lodash (#71)
Browse files Browse the repository at this point in the history
* Added tests for new utilities
* Moved source files to a src directory
* Add tdd npm script
* Removed data files

closes 70
  • Loading branch information
cecilia-sanare authored Oct 22, 2016
1 parent 2a8550a commit c4fce03
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 125 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dry-parser",
"version": "0.0.0-semantically-released.0",
"main": "index.js",
"main": "src/dry-parser.js",
"description": "A simple tool for parsing JavaScript objects to avoid repetition.",
"homepage": "https://github.com/salte-io/dry-parser",
"bugs": "https://github.com/salte-io/dry-parser/issues",
Expand All @@ -16,20 +16,21 @@
"json",
"object"
],
"engines": {
"node": ">= 4.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/salte-io/dry-parser.git"
},
"scripts": {
"lint": "eslint index.js test/**/*.js",
"test": "nyc --reporter=lcov --reporter=text mocha test/**/*.spec.js",
"tdd": "mocha -w test/**/*.spec.js",
"report-coverage": "coveralls < coverage/lcov.info",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"license": "MIT",
"dependencies": {
"lodash": "3.10.1"
},
"devDependencies": {
"chai": "3.5.0",
"coveralls": "2.11.14",
Expand Down
16 changes: 9 additions & 7 deletions index.js → src/dry-parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _ = require('lodash');
var utils = require('./utils.js');
var dry = this;

/**
Expand All @@ -9,13 +9,15 @@ dry.regex = /\{([\w\d.-]+)\}/g;

dry.parse = function(object, baseObject) {
var result = object;
if (_.isArray(object) || _.isObject(object)) {
_.forEach(object, function(value, key) {
result[key] = dry.parse(result[key], baseObject || object);
});
} else if (_.isString(object)) {
if (Array.isArray(object) || typeof object === 'object') {
for (var key in object) {
if (object.hasOwnProperty(key)) {
result[key] = dry.parse(result[key], baseObject || object);
}
}
} else if (typeof object === 'string') {
result = object.replace(dry.regex, function(match, g1) {
var parsedValue = dry.parse(_.get(baseObject, g1), baseObject);
var parsedValue = dry.parse(utils.deepFind(baseObject, g1), baseObject);

if (parsedValue) {
return parsedValue;
Expand Down
20 changes: 20 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var utils = this;

utils.deepFind = function(object, path) {
if (!object) {
return undefined;
}
var paths = path.split('.');
var current = object;

for (var i = 0; i < paths.length; i++) {
if (current[paths[i]] === undefined) {
return undefined;
} else {
current = current[paths[i]];
}
}
return current;
};

module.exports = utils;
14 changes: 0 additions & 14 deletions test/array/data.json

This file was deleted.

14 changes: 0 additions & 14 deletions test/array/expected.json

This file was deleted.

3 changes: 0 additions & 3 deletions test/bi-parse/data.json

This file was deleted.

3 changes: 0 additions & 3 deletions test/bi-parse/expected.json

This file was deleted.

134 changes: 93 additions & 41 deletions test/dry-parser.spec.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,120 @@
var expect = require('chai').expect;
var sinon = require('sinon');
var dry = require('../index');
var dry = require('../src/dry-parser.js');

describe('dryParser', function() {
describe('dry-parser', function() {
var sandbox;
var data;
var expected;

beforeEach(function() {
sandbox = sinon.sandbox.create();
sandbox.stub(console, 'warn');
sandbox.stub(console, 'error');

data = {
single: require('../test/single/data.json'),
biParse: require('../test/bi-parse/data.json'),
array: require('../test/array/data.json'),
retainNonStringTypes: require('../test/retain-non-string-types/data.json'),
invalid: require('../test/invalid/data.json')
};

expected = {
single: require('../test/single/expected.json'),
biParse: require('../test/bi-parse/expected.json'),
array: require('../test/array/expected.json'),
retainNonStringTypes: require('../test/retain-non-string-types/data.json'),
invalid: require('../test/invalid/data.json')
};
});

afterEach(function() {
sandbox.restore();
});

describe('#parse()', function() {
it('should parse bindings of a single object', function() {
var results = dry.parse(data.single);
describe('function(parse)', function() {
describe('single-object', function() {
it('should parse bindings of a single object', function() {
var results = dry.parse({
assets: 'assets',
sass: '{assets}/sass'
});

expect(results).to.deep.equal(expected.single);
});
expect(results).to.deep.equal({
assets: 'assets',
sass: 'assets/sass'
});
});

it('should parse bindings between two objects', function() {
var results = dry.parse(data.biParse, data.single);
it('should parse nested bindings of a single object', function() {
var results = dry.parse({
dir: {
node: 'node_modules'
},
lib: {
angular: '{dir.node}/angular/angular.js'
}
});

expect(results).to.deep.equal(expected.biParse);
});
expect(results).to.deep.equal({
dir: {
node: 'node_modules'
},
lib: {
angular: 'node_modules/angular/angular.js'
}
});
});

it('should parse bindings within an array', function() {
var results = dry.parse(data.array);
it('should support whatever ordering is provided', function() {
var results = dry.parse({
lodash: '{node}/lodash',
object: '{lodash}/object',
node: 'node_modules'
});

expect(results).to.deep.equal(expected.array);
});
expect(results).to.deep.equal({
lodash: 'node_modules/lodash',
object: 'node_modules/lodash/object',
node: 'node_modules'
});
});

it('should parse bindings within an array', function() {
var results = dry.parse({
libraries: [
'angular'
],
someValue: '{libraries.0}'
});

expect(results).to.deep.equal({
libraries: [
'angular'
],
someValue: 'angular'
});
});

it('should retain non-string types', function() {
var results = dry.parse({
bool: true,
int: 1
});

expect(results).to.deep.equal({
bool: true,
int: 1
});
});

it('should retain non-string types', function() {
var results = dry.parse(data.retainNonStringTypes);
it('should log a warning when passed an invalid config', function() {
var results = dry.parse({
invalid: '{dir.invalidKey}/less'
});

expect(results).to.deep.equal(expected.retainNonStringTypes);
expect(results).to.deep.equal({
invalid: '{dir.invalidKey}/less'
});
expect(console.warn.callCount).to.equal(1);
expect(console.warn.calledWith('The following key doesn\'t map to any values; ', 'dir.invalidKey')).to.equal(true);
});
});

it('should log a warning when passed an invalid config', function() {
var results = dry.parse(data.invalid);
describe('bi-parsing', function() {
it('should parse bindings between two objects', function() {
var results = dry.parse({
sass: '{assets}/sass'
}, {
assets: 'assets'
});

expect(results).to.deep.equal(expected.invalid);
sinon.assert.calledOnce(console.warn);
expect(results).to.deep.equal({
sass: 'assets/sass'
});
});
});
});
});
3 changes: 0 additions & 3 deletions test/invalid/data.json

This file was deleted.

4 changes: 0 additions & 4 deletions test/retain-non-string-types/data.json

This file was deleted.

16 changes: 0 additions & 16 deletions test/single/data.json

This file was deleted.

16 changes: 0 additions & 16 deletions test/single/expected.json

This file was deleted.

36 changes: 36 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var expect = require('chai').expect;
var utils = require('../src/utils.js');

describe('utils', function() {
describe('function(deepFind)', function() {
it('should find values from a path', function() {
var results = utils.deepFind({
assets: 'assets'
}, 'assets');

expect(results).to.equal('assets');
});

it('should find values from a deep path', function() {
var results = utils.deepFind({
dir: {
assets: 'assets'
}
}, 'dir.assets');

expect(results).to.equal('assets');
});

it('should be undefined if a path does not exist', function() {
var results = utils.deepFind({}, 'assets');

expect(results).to.equal(undefined);
});

it('should be undefined if the object is falsy', function() {
var results = utils.deepFind(null, 'assets');

expect(results).to.equal(undefined);
});
});
});

0 comments on commit c4fce03

Please sign in to comment.