Comparing version 3.0.0-rc1 to 3.0.0-rc2
180
clues.js
@@ -1,100 +0,106 @@ | ||
var Promise = require('bluebird'), | ||
reArgs = /function.*?\((.*?)\).*/; | ||
(function(self) { | ||
if (typeof module !== 'undefined') { | ||
clues.Promise = require('bluebird'); | ||
module.exports = clues; | ||
} else { | ||
clues.Promise = self.Promise; | ||
self.clues = clues; | ||
} | ||
function matchArgs(fn) { | ||
if (!fn.__args__) { | ||
var match = reArgs.exec(fn.prototype.constructor.toString()); | ||
fn.__args__ = match[1].replace(/\s/g,'') | ||
.split(',') | ||
.filter(function(d) { | ||
return d.length; | ||
}); | ||
var reArgs = /function.*?\((.*?)\).*/; | ||
function matchArgs(fn) { | ||
if (!fn.__args__) { | ||
var match = reArgs.exec(fn.prototype.constructor.toString()); | ||
fn.__args__ = match[1].replace(/\s/g,'') | ||
.split(',') | ||
.filter(function(d) { | ||
return d.length; | ||
}); | ||
} | ||
return fn.__args__; | ||
} | ||
return fn.__args__; | ||
} | ||
function clues(logic,fn,$global,caller,fullref) { | ||
var args,ref; | ||
function clues(logic,fn,$global,caller,fullref) { | ||
var args,ref; | ||
if (!$global) $global = {}; | ||
$global.$root = $global.$root || logic; | ||
if (!$global) $global = {}; | ||
if (typeof fn === 'string') { | ||
ref = fn; | ||
if (typeof fn === 'string') { | ||
ref = fn; | ||
var dot = ref.indexOf('.'); | ||
if (dot > -1) { | ||
var next = ref.slice(0,dot); | ||
return clues(logic,next,$global,caller,fullref) | ||
.then(function(d) { | ||
d.$parent = d.$parent || logic; | ||
logic = d; | ||
ref = ref.slice(dot+1); | ||
fullref = (fullref ? fullref+'.' : '')+next; | ||
return clues(logic,ref,$global,caller,fullref); | ||
}) | ||
.catch(function(e) { | ||
if (logic[ref]) return Promise.fulfilled(logic[ref]); | ||
if (logic.$service && typeof logic.$service === 'function') | ||
return logic[ref] = clues(logic,function() { return logic.$service.call(logic,ref); },$global,caller,(fullref ? fullref+'.' : '')+ref); | ||
else throw e; | ||
}); | ||
var dot = ref.indexOf('.'); | ||
if (dot > -1) { | ||
var next = ref.slice(0,dot); | ||
return clues(logic,next,$global,caller,fullref) | ||
.then(function(d) { | ||
d.$parent = d.$parent || logic; | ||
logic = d; | ||
ref = ref.slice(dot+1); | ||
fullref = (fullref ? fullref+'.' : '')+next; | ||
return clues(logic,ref,$global,caller,fullref); | ||
}) | ||
.catch(function(e) { | ||
if (logic[ref]) return clues.Promise.fulfilled(logic[ref]); | ||
if (logic.$service && typeof logic.$service === 'function') | ||
return logic[ref] = clues(logic,function() { return logic.$service.call(logic,ref); },$global,caller,(fullref ? fullref+'.' : '')+ref); | ||
else throw e; | ||
}); | ||
} | ||
fullref = (fullref ? fullref+'.' : '')+ref; | ||
fn = logic[ref]; | ||
if (fn === undefined) { | ||
if (ref === '$global') return clues.Promise.fulfilled($global); | ||
if ($global[ref]) return clues($global,ref,$global,caller,fullref); | ||
if (logic.$property && typeof logic.$property === 'function') | ||
return logic[ref] = clues(logic,function() { return logic.$property.call(logic,ref); },$global,caller,fullref) ; | ||
return clues.Promise.rejected({ref : ref, message: ref+' not defined', fullref:fullref,caller: caller}); | ||
} | ||
} | ||
fullref = (fullref ? fullref+'.' : '')+ref; | ||
fn = logic[ref]; | ||
if (fn === undefined) { | ||
if ($global[ref]) return clues($global,ref,$global,caller,fullref); | ||
if (logic.$property && typeof logic.$property === 'function') | ||
return logic[ref] = clues(logic,function() { return logic.$property.call(logic,ref); },$global,caller,fullref) ; | ||
return Promise.rejected({ref : ref, message: ref+' not defined', fullref:fullref,caller: caller}); | ||
// Support an array with argument names in front and the function as last element | ||
if (typeof fn === 'object' && fn.length && typeof fn[fn.length-1] == 'function') { | ||
args = fn.slice(0,fn.length-1); | ||
fn = fn[fn.length-1]; | ||
} | ||
} | ||
// If the logic reference is not a function, we simply return the value | ||
if (typeof fn !== 'function') return clues.Promise.fulfilled(fn); | ||
// Support an array with argument names in front and the function as last element | ||
if (typeof fn === 'object' && fn.length && typeof fn[fn.length-1] == 'function') { | ||
args = fn.slice(0,fn.length-1); | ||
fn = fn[fn.length-1]; | ||
} | ||
// If the logic reference is not a function, we simply return the value | ||
if (typeof fn !== 'function') return Promise.fulfilled(fn); | ||
args = (args || matchArgs(fn)) | ||
.map(function(arg) { | ||
var optional,showError; | ||
if (optional = (arg[0] === '_')) arg = arg.slice(1); | ||
if (showError = (arg[0] === '_')) arg = arg.slice(1); | ||
return clues(logic,arg,$global,ref,fullref) | ||
.then(null,function(e) { | ||
if (optional) return (showError) ? e : undefined; | ||
else throw e; | ||
}); | ||
}); | ||
args = (args || matchArgs(fn)) | ||
.map(function(arg) { | ||
var optional,showError; | ||
if (optional = (arg[0] === '_')) arg = arg.slice(1); | ||
if (showError = (arg[0] === '_')) arg = arg.slice(1); | ||
return clues(logic,arg,$global,ref,fullref) | ||
.then(null,function(e) { | ||
if (optional) return (showError) ? e : undefined; | ||
else throw e; | ||
}); | ||
}); | ||
var inputs = clues.Promise.all(args); | ||
if (inputs.cancellable) inputs = inputs.cancellable(); | ||
var inputs = Promise.all(args); | ||
if (inputs.cancellable) inputs = inputs.cancellable(); | ||
var value = inputs | ||
.then(function(args) { | ||
return fn.apply(logic, args); | ||
}) | ||
.then(function(d) { | ||
return typeof d == 'string' ? d : clues(logic,d,$global,caller,fullref); | ||
},function(e) { | ||
if (e.name && e.name == 'CancellationError') | ||
return args.forEach(function(arg) { arg.cancel(); }); | ||
if (typeof e !== 'object') | ||
e = { message : e}; | ||
e.error = true; | ||
e.ref = e.ref || ref; | ||
e.fullref = e.fullref || fullref; | ||
e.caller = e.caller || caller || ''; | ||
throw e; | ||
}); | ||
var value = inputs | ||
.then(function(args) { | ||
return fn.apply(logic, args); | ||
}) | ||
.then(function(d) { | ||
return typeof d == 'string' ? d : clues(logic,d,$global,caller,fullref); | ||
},function(e) { | ||
if (e.name && e.name == 'CancellationError') | ||
return args.forEach(function(arg) { arg.cancel(); }); | ||
if (typeof e !== 'object') | ||
e = { message : e}; | ||
e.error = true; | ||
e.ref = e.ref || ref; | ||
e.fullref = e.fullref || fullref; | ||
e.caller = e.caller || caller || ''; | ||
throw e; | ||
}); | ||
if (ref) logic[ref] = value; | ||
return value; | ||
} | ||
if (ref) logic[ref] = value; | ||
return value; | ||
} | ||
clues.Promise = Promise; | ||
module.exports = clues; | ||
})(this); |
{ | ||
"name": "clues", | ||
"version": "3.0.0-rc1", | ||
"version": "3.0.0-rc2", | ||
"description": "Lightweight logic tree solver using promises.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -22,2 +22,6 @@ var instinct = require("../clues"), | ||
}, | ||
e : function($global) { | ||
$global.test = 4; | ||
return {f:{g:function(test) { return test;}}}; | ||
} | ||
}; | ||
@@ -50,2 +54,9 @@ | ||
}); | ||
it('can be used as object $global',function() { | ||
return instinct(facts,'e.f.g') | ||
.then(function(d) { | ||
assert.equal(d,4); | ||
}); | ||
}); | ||
}); |
@@ -5,4 +5,4 @@ var clues = require('../clues'), | ||
function jsonReplacer(key, value) { | ||
if (typeof value === 'object') { | ||
var tmp = {},key; | ||
if (value && typeof value === 'object') { | ||
var tmp = Array.isArray(value) ? [] : {}; | ||
for (key in value) | ||
@@ -63,17 +63,17 @@ tmp[key] = value[key]; | ||
var data = (select || req.param("fn").split(',')) | ||
.map(function(ref) { | ||
var facts = Object.create(api); | ||
return clues(facts,ref,{res:res,req:req,input:req.body},'__user__') | ||
.catch(stringifyError) | ||
.then(function(d) { | ||
if (d === undefined) d = null; | ||
var txt = {}; | ||
txt[ref] = d; | ||
txt = first+JSON.stringify(txt,jsonReplacer,pretty); | ||
first = ''; | ||
_res.write(txt.slice(1,txt.length-1)+',\t\n'); | ||
if (typeof(res.flush) == 'function') _res.flush(); | ||
}); | ||
}); | ||
var data = (select || req.params["fn"].split(',')) | ||
.map(function(ref) { | ||
var facts = Object.create(api); | ||
return clues(facts,ref,{res:res,req:req,input:req.body,$root:facts},'__user__') | ||
.catch(stringifyError) | ||
.then(function(d) { | ||
if (d === undefined) d = null; | ||
var txt = {}; | ||
txt[ref] = d; | ||
txt = first+JSON.stringify(txt,jsonReplacer,pretty); | ||
first = ''; | ||
_res.write(txt.slice(1,txt.length-1)+',\t\n'); | ||
if (typeof(res.flush) == 'function') _res.flush(); | ||
}); | ||
}); | ||
@@ -93,2 +93,2 @@ req.on('close',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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
50949
1012
0