feat: Switch to function-based specification of dependencies
Allows dependencies to be economically expressed and used. For example, see the new definition of subtract. Credit for the basic idea goes to James Drew, see https://stackoverflow.com/a/41525264 Resolves #21.
This commit is contained in:
parent
d72c443616
commit
9fb3aa2959
13 changed files with 104 additions and 102 deletions
|
@ -19,7 +19,7 @@ describe('The default full pocomath instance "math"', () => {
|
|||
|
||||
it('can be extended', () => {
|
||||
math.install({'add': {
|
||||
'...string': [[], addends => addends.reduce((x,y) => x+y, '')]
|
||||
'...string': () => addends => addends.reduce((x,y) => x+y, '')
|
||||
}})
|
||||
assert.strictEqual(math.add('Kilroy',' is here'), 'Kilroy is here')
|
||||
})
|
||||
|
|
|
@ -4,7 +4,7 @@ import PocomathInstance from '../../src/core/PocomathInstance.mjs'
|
|||
const pi = new PocomathInstance('dummy')
|
||||
describe('PocomathInstance', () => {
|
||||
it('creates an instance that can define typed-functions', () => {
|
||||
pi.install({add: {'any,any': [[], (a,b) => a+b]}})
|
||||
pi.install({add: {'any,any': () => (a,b) => a+b}})
|
||||
assert.strictEqual(pi.add(2,2), 4)
|
||||
assert.strictEqual(pi.add('Kilroy', 17), 'Kilroy17')
|
||||
assert.strictEqual(pi.add(1, undefined), NaN)
|
||||
|
|
22
test/core/_dependencyExtractor.mjs
Normal file
22
test/core/_dependencyExtractor.mjs
Normal file
|
@ -0,0 +1,22 @@
|
|||
import assert from 'assert'
|
||||
import dependencyExtractor from '../../src/core/dependencyExtractor.mjs'
|
||||
|
||||
describe('dependencyExtractor', () => {
|
||||
it('will record the keys of a destructuring function', () => {
|
||||
const myfunc = ({a, 'b(x)': b, c: alias}) => 0
|
||||
const params = new Set()
|
||||
myfunc(dependencyExtractor(params))
|
||||
assert.ok(params.has('a'))
|
||||
assert.ok(params.has('b(x)'))
|
||||
assert.ok(params.has('c'))
|
||||
assert.ok(params.size === 3)
|
||||
})
|
||||
|
||||
it('does not pick up anything from a regular function', () => {
|
||||
const myfunc = arg => 0
|
||||
const params = new Set()
|
||||
myfunc(dependencyExtractor(params))
|
||||
assert.ok(params.size === 0)
|
||||
})
|
||||
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue