refactor: Simpler merging mechanism
Merging of Pocomath modules is eased by allowing one PocomathInstance to be merged into another. That allows types, for example, to be exported as a PocomathInstance (so there is no need for a special identifier convention for types; they can be directly added with an installType method). Also, larger modules can just be exported as an instance, since there is more flexibility and more checking in merging PocomathInstances than raw modules.
This commit is contained in:
parent
58fa661a2d
commit
d9d7af961f
12 changed files with 146 additions and 89 deletions
|
@ -18,14 +18,14 @@ describe('The default full pocomath instance "math"', () => {
|
|||
})
|
||||
|
||||
it('can be extended', () => {
|
||||
math.installType('stringK', {
|
||||
test: s => typeof s === 'string' && s.charAt(0) === 'K',
|
||||
before: ['string']
|
||||
})
|
||||
math.install({
|
||||
add: {
|
||||
'...stringK': () => addends => addends.reduce((x,y) => x+y, '')
|
||||
},
|
||||
Type_stringK: {
|
||||
test: s => typeof s === 'string' && s.charAt(0) === 'K',
|
||||
before: ['string']
|
||||
}
|
||||
})
|
||||
assert.strictEqual(math.add('Kilroy','K is here'), 'KilroyK is here')
|
||||
})
|
||||
|
|
|
@ -23,7 +23,7 @@ describe('A custom instance', () => {
|
|||
|
||||
it("can be assembled in any order", () => {
|
||||
bw.install(numbers)
|
||||
bw.install({Type_string: {test: s => typeof s === 'string'}})
|
||||
bw.installType('string', {test: s => typeof s === 'string'})
|
||||
assert.strictEqual(bw.subtract(16, bw.add(3,4,2)), 7)
|
||||
assert.strictEqual(bw.negate('8'), -8)
|
||||
assert.deepStrictEqual(bw.add(bw.complex(1,3), 1), {re: 2, im: 3})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue