This repository has been archived by the owner on May 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove dependency on lodash (#71)
* Added tests for new utilities * Moved source files to a src directory * Add tdd npm script * Removed data files closes 70
- Loading branch information
1 parent
2a8550a
commit c4fce03
Showing
13 changed files
with
163 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |