Comparing version 3.5.38 to 3.5.39
@@ -98,3 +98,3 @@ (function(self) { | ||
// If the logic reference is not a function, we simply return the value | ||
if (typeof fn !== 'function' || (ref && ref[0] === '$')) { | ||
if (typeof fn !== 'function' || ((ref && ref[0] === '$') && fn.name !== '$prep')) { | ||
// If the value is a promise we wait for it to resolve to inspect the result | ||
@@ -115,3 +115,3 @@ if (fn && typeof fn.then === 'function') | ||
if (fn.name === '$external' || (args[0] === '$external' && args.length === 1)) return logic[ref] = clues.Promise.resolve({$external: fn.bind(logic)}); | ||
if (fn.name === '$service') return fn; | ||
@@ -118,0 +118,0 @@ args = args.map(function(arg) { |
{ | ||
"name": "clues", | ||
"version": "3.5.38", | ||
"version": "3.5.39", | ||
"description": "Lightweight logic tree solver using promises.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -312,5 +312,22 @@ [![NPM Version][npm-image]][npm-url] | ||
It is worth noting that this functionality only applies to functions. If an object has a $ prefix, then any functions inside that object will be crunched by `clues` as usual. | ||
It is worth noting that this functionality only applies to functions. If an object has a $ prefix, then any functions inside that object will be crunched by `clues` as usual. | ||
If you would like to have a service method that is partially fed by the rest of the `facts`, you can name your function `$prep`. If you want to make a `$prep` for your service, you must eventually return a `$service` method. For example: | ||
```js | ||
var Cabinet = { | ||
something: () => 5, | ||
complicated: () => 6, | ||
$adder: function $prep(something, complicated) { | ||
let work = something + complicated; | ||
return function $service(number) { | ||
return work * number; | ||
} | ||
} | ||
}; | ||
``` | ||
### $property - lazily create children by missing reference | ||
@@ -317,0 +334,0 @@ If a particular property can not be found in a given object, clues will try to locate a `$property` function. If that function exists, it is executed with the missing property name as the first argument and the missing value is set to be the function outcome. |
@@ -9,3 +9,11 @@ const clues = require('../clues'); | ||
b : 11, | ||
counter: {count: 0}, | ||
$logic_service : a => a, | ||
$with_prep : function $prep(a,b,counter) { | ||
counter.count++; | ||
let c = a + b; | ||
return function $service(d) { | ||
return c + d; | ||
} | ||
}, | ||
top : a => ({ $nested_service : b => a + b }) | ||
@@ -35,2 +43,18 @@ }; | ||
t.test('with prep', async t => { | ||
const d = await clues(Object.create(Logic), '$with_prep', Object.create(global)); | ||
t.same(typeof d, 'function','returns a function'); | ||
t.same(d(5), 26); | ||
}); | ||
t.test('with prep - solved only once', async t => { | ||
let logic = Object.create(Logic); | ||
logic.counter = {count: 0}; | ||
const d1 = await clues(logic, '$with_prep', Object.create(global)); | ||
const d2 = await clues(logic, '$with_prep', Object.create(global)); | ||
t.same(typeof d1, 'function','returns a function'); | ||
t.same(d1(5), 26); | ||
t.same(logic.counter.count, 1); | ||
}); | ||
}); |
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
98966
1727
649