Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "percento", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Simple node package to mixin json variables", | ||
@@ -5,0 +5,0 @@ "main": "src/jsonresolver.js", |
@@ -6,12 +6,12 @@ var _ = require('lodash'); | ||
var prevString, | ||
value, | ||
value, | ||
keys; | ||
while (jsonString !== prevString) { | ||
prevString = jsonString; | ||
keys = jsonString.match(/%([\w\d-_\.]+)%/g); | ||
if (!keys) continue; | ||
keys.forEach(function(key) { | ||
@@ -25,23 +25,36 @@ key = key.match(/%([\w\d-_\.]+)%/)[1]; | ||
return jsonString; | ||
} | ||
function resolve ( json, ctx ) { | ||
var jsonString = JSON.stringify(json || { }); | ||
jsonString = ctx ? | ||
resolveWithContext(jsonString, ctx) : | ||
resolveWithContext(jsonString, json); | ||
return JSON.parse(jsonString); | ||
} | ||
function JSONResolver() { | ||
return { | ||
chain: function() { | ||
return { | ||
lastAns: undefined, | ||
resolve: function ( json, ctx ) { | ||
this.lastAns = resolve(this.lastAns || json, ctx || json); | ||
return this; | ||
}, | ||
value: function() { | ||
return this.lastAns || { }; | ||
} | ||
}; | ||
}, | ||
resolve: resolve | ||
resolve: function ( json, ctx ) { | ||
var jsonString = JSON.stringify(json); | ||
}; | ||
jsonString = ctx ? | ||
resolveWithContext(jsonString, ctx) : | ||
resolveWithContext(jsonString, json); | ||
return JSON.parse(jsonString); | ||
} | ||
}; | ||
} | ||
module.exports = JSONResolver; |
var jsonResolver = require('../src/jsonResolver'); | ||
describe('JSON variable resolver', function () { | ||
it('should provide a resolve function', function () { | ||
it('should provide a resolve function', function () { | ||
expect(jsonResolver().resolve).toBeDefined(); | ||
}); | ||
}); | ||
@@ -17,3 +17,3 @@ it('should return json that its given', function () { | ||
ctx = { 'a':'b' }; | ||
expect(jsonResolver().resolve(json, ctx)).toEqual(json); | ||
@@ -36,3 +36,3 @@ }); | ||
ctx = { 'name':'baz %surname%', 'surname': 'luhrmann' }; | ||
expect(jsonResolver().resolve(json, ctx)).toEqual(expected); | ||
@@ -45,3 +45,3 @@ }); | ||
ctx = { 'surname': 'luhrmann', 'name':'baz %surname%' }; | ||
expect(jsonResolver().resolve(json, ctx)).toEqual(expected); | ||
@@ -54,3 +54,3 @@ }); | ||
ctx = { }; | ||
expect(jsonResolver().resolve(json, ctx)).toEqual(expected); | ||
@@ -63,3 +63,3 @@ }); | ||
ctx = { baz: 'luhrmann' }; | ||
expect(jsonResolver().resolve(json, ctx)).toEqual(expected); | ||
@@ -112,5 +112,107 @@ }); | ||
}); | ||
}); | ||
describe('when not given JSON as ctx', function() { | ||
it('should ignore the ctx, returning the input JSON', function() { | ||
var expected = {'input': '%steinbeck%'}, | ||
json = {'input': '%steinbeck%'}, | ||
ctx = 7; | ||
expect(jsonResolver().resolve(json, ctx)).toEqual(expected); | ||
}); | ||
}); | ||
describe('when not given JSON as json', function() { | ||
describe('when given a valid ctx', function() { | ||
it('will return the first input', function() { | ||
var expected = 'Romeo + Juliet', | ||
json = 'Romeo + Juliet', | ||
ctx = { 'a':'bar' }; | ||
expect(jsonResolver().resolve(json, ctx)).toEqual(expected); | ||
}); | ||
}); | ||
describe('when not given a valid ctx', function() { | ||
it('will return the first input', function() { | ||
var expected = 'Romeo + Juliet', | ||
json = 'Romeo + Juliet', | ||
ctx = 'The Great Gatsby'; | ||
expect(jsonResolver().resolve(json, ctx)).toEqual(expected); | ||
}); | ||
}); | ||
describe('when json is falsy', function() { | ||
describe('when undefined', function() { | ||
it('will return an empty object', function() { | ||
var expected = { }, | ||
json = undefined, | ||
ctx = { 'name':'baz %surname%' }; | ||
expect(jsonResolver().resolve(json, ctx)).toEqual(expected); | ||
}); | ||
}); | ||
describe('when false', function() { | ||
it('will return an empty object', function() { | ||
var expected = { }, | ||
json = false, | ||
ctx = { 'name':'baz %surname%' }; | ||
expect(jsonResolver().resolve(json, ctx)).toEqual(expected); | ||
}); | ||
}); | ||
describe('when null', function() { | ||
it('will return an empty object', function() { | ||
var expected = { }, | ||
json = null, | ||
ctx = { 'name':'baz %surname%' }; | ||
expect(jsonResolver().resolve(json, ctx)).toEqual(expected); | ||
}); | ||
}); | ||
describe('when 0', function() { | ||
it('will return an empty object', function() { | ||
var expected = { }, | ||
json = 0, | ||
ctx = { 'name':'baz %surname%' }; | ||
expect(jsonResolver().resolve(json, ctx)).toEqual(expected); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('chaining', function() { | ||
describe('chain a single resolve', function () { | ||
it('should resolve normally, returning after calling value()', function () { | ||
var expected = { 'foo':'bar','baz':'luhrmann' }, | ||
json = { 'foo':'%a%','baz':'luhrmann' }, | ||
ctx = { 'a':'bar' }; | ||
expect(jsonResolver().chain().resolve(json, ctx).value()).toEqual(expected); | ||
}); | ||
}); | ||
describe('chain a multiple chain resolve', function () { | ||
it('should resolve normally, returning after calling value()', function () { | ||
var expected = { 'foo':'bang','baz':'luhrmann' }, | ||
json = { 'foo':'%a%','baz':'luhrmann' }, | ||
ctx = { 'a':'%bar%' }, | ||
ctx2 = { 'bar': 'bang'}; | ||
expect( | ||
jsonResolver() | ||
.chain() | ||
.resolve(json, ctx) | ||
.resolve(ctx2) | ||
.value() | ||
).toEqual(expected); | ||
}); | ||
}); | ||
}); |
12973
237