refactor: Make generics more strict via templates
This commit also adds a built-in `typeOf` function to every PocomathInstance. It also adds a notation (prefix '!') for "eager" templeates that instantiate themselves for all types immediately upon definition.
This commit is contained in:
parent
fd3d6b2eb3
commit
36e7b750ce
23 changed files with 233 additions and 44 deletions
|
@ -1,4 +1,5 @@
|
|||
import assert from 'assert'
|
||||
import PocomathInstance from '../src/core/PocomathInstance.mjs'
|
||||
import math from '../src/pocomath.mjs'
|
||||
|
||||
describe('The default full pocomath instance "math"', () => {
|
||||
|
@ -37,16 +38,17 @@ describe('The default full pocomath instance "math"', () => {
|
|||
})
|
||||
|
||||
it('can be extended', () => {
|
||||
math.installType('stringK', {
|
||||
const stretch = PocomathInstance.merge(math) // clone to not pollute math
|
||||
stretch.installType('stringK', {
|
||||
test: s => typeof s === 'string' && s.charAt(0) === 'K',
|
||||
before: ['string']
|
||||
})
|
||||
math.install({
|
||||
stretch.install({
|
||||
add: {
|
||||
'...stringK': () => addends => addends.reduce((x,y) => x+y, '')
|
||||
},
|
||||
})
|
||||
assert.strictEqual(math.add('Kilroy','K is here'), 'KilroyK is here')
|
||||
assert.strictEqual(stretch.add('Kilroy','K is here'), 'KilroyK is here')
|
||||
})
|
||||
|
||||
it('handles complex numbers', () => {
|
||||
|
|
|
@ -38,6 +38,13 @@ describe('complex', () => {
|
|||
math.complex(ms.negate(ms.sqrt(0.5)), ms.sqrt(0.5)))
|
||||
})
|
||||
|
||||
it('checks for equality', () => {
|
||||
assert.ok(math.equal(math.complex(3,0), 3))
|
||||
assert.ok(math.equal(math.complex(3,2), math.complex(3, 2)))
|
||||
assert.ok(!(math.equal(math.complex(45n, 3n), math.complex(45n, -3n))))
|
||||
assert.ok(!(math.equal(math.complex(45n, 3n), 45n)))
|
||||
})
|
||||
|
||||
it('computes gcd', () => {
|
||||
assert.deepStrictEqual(
|
||||
math.gcd(math.complex(53n, 56n), math.complex(47n, -13n)),
|
||||
|
|
|
@ -30,4 +30,11 @@ describe('number', () => {
|
|||
it('computes gcd', () => {
|
||||
assert.strictEqual(math.gcd(15, 35), 5)
|
||||
})
|
||||
|
||||
it('compares numbers', () => {
|
||||
assert.ok(math.smaller(12,13.5))
|
||||
assert.ok(math.equal(Infinity, Infinity))
|
||||
assert.ok(math.largerEq(12.5, math.divide(25,2)))
|
||||
})
|
||||
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue