Comparing version 3.5.13 to 3.5.14
{ | ||
"name": "clues", | ||
"version": "3.5.13", | ||
"version": "3.5.14", | ||
"description": "Lightweight logic tree solver using promises.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -17,3 +17,4 @@ var clues = require('../clues'), | ||
}, | ||
userid : ['input.userid',String] | ||
userid : ['input.userid',String], | ||
never_traversed : {} | ||
}; | ||
@@ -29,3 +30,7 @@ | ||
return { desc: 'this is an injected response', userid: userid}; | ||
} | ||
}, | ||
'impossible.to.reach' : 42, | ||
'possible' : {}, | ||
'possible.value': 42, | ||
'never_traversed.value' : 11 | ||
},$global); | ||
@@ -41,2 +46,25 @@ }; | ||
}); | ||
it('allows sequential injections',function() { | ||
return clues(injected,'possible.value',{}) | ||
.then(function(d) { | ||
assert.equal(d,42); | ||
}); | ||
}); | ||
it('ignores base paths that do not exists',function() { | ||
return clues(injected,'impossible.to.reach',{}) | ||
.then(function() { | ||
throw 'SHOULD_ERROR'; | ||
},function(e) { | ||
assert.equal(e.message,'impossible not defined'); | ||
}); | ||
}); | ||
it('does not execute injected paths that are not traversed',function() { | ||
return clues({},injected) | ||
.then(function(injected) { | ||
assert(typeof injected.never_traversed === 'function'); | ||
}); | ||
}); | ||
}); |
@@ -7,15 +7,28 @@ // This function sets given properties (each key is a full path) in a provided base object | ||
function inject(obj, prop, $global) { | ||
var keys = Object.keys(prop); | ||
if (!$global) | ||
throw new Error('$global not supplied to inject'); | ||
if (typeof prop !== 'object' || typeof obj !== 'object') | ||
throw new Error('Invalid properties passed to inject'); | ||
var keys = []; | ||
for (var key in prop) | ||
keys.push(key); | ||
return clues.Promise.mapSeries(keys, function(key) { | ||
var path = key.split('.'), | ||
base = path.length === 1 ? obj : path.slice(0, path.length - 1).join('.'), | ||
base = path.slice(0, path.length - 1), | ||
item = path[path.length - 1], | ||
value = prop[key]; | ||
return clues(obj, base, $global, 'inject') | ||
.then(function(base) { | ||
var original = base[item]; | ||
var o = obj; | ||
function nextLevel() { | ||
var next = base.shift(); | ||
var original = o[next]; | ||
if (!next) { | ||
original = o[item]; | ||
if (original !== undefined) | ||
base['original_'+item] = function private() { | ||
o['original_'+item] = function private() { | ||
return original; | ||
@@ -25,8 +38,19 @@ }; | ||
value = clues.Promise.reject(value); | ||
if (base[item] && base[item]._fulfill && base[item].isPending()) | ||
base[item]._fulfill(value); | ||
if (o[item] && o[item]._fulfill && o[item].isPending()) | ||
o[item]._fulfill(value); | ||
else | ||
base[item] = value; | ||
}) | ||
.catch(Object); | ||
o[item] = value; | ||
} else { | ||
if (original !== undefined) | ||
o[next] = function() { | ||
return clues(o,original,$global,'set','set') | ||
.then(d => { | ||
o = d; | ||
return clues.Promise.try(o ? nextLevel : Object).then( () => d); | ||
}); | ||
}; | ||
} | ||
} | ||
return nextLevel(); | ||
}) | ||
@@ -33,0 +57,0 @@ .then(function() { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
90320
1762