feat: Selective loader that extends all/only existing number methods to complex
Plus a test example where an instance is initialized to have only addition, and then that instance is extended to complex numbers, at which point it has number and complex addition but not negation or subtraction (at all).
This commit is contained in:
parent
29653f25c4
commit
e5ec1083e3
23
complex/extendByComplex.js
Normal file
23
complex/extendByComplex.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import { isTF } from '../poortf.js'
|
||||||
|
import createComplex from './complex.js'
|
||||||
|
|
||||||
|
/* Add all the complex methods for the number methods that are
|
||||||
|
* already in the instance: */
|
||||||
|
export default async function extend(pmath) {
|
||||||
|
for (const item in pmath) {
|
||||||
|
if (isTF(pmath[item])) {
|
||||||
|
// might be a number method, try importing
|
||||||
|
const modulePath = `./${item}.js`
|
||||||
|
try {
|
||||||
|
const { default: create } = await import(modulePath)
|
||||||
|
create(pmath)
|
||||||
|
} catch (err) {
|
||||||
|
// Guess it wasn't a number method; no worries
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
createComplex(pmath)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -25,3 +25,7 @@ export default function poortf (name, imps) {
|
|||||||
|
|
||||||
return fn
|
return fn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isTF(x) {
|
||||||
|
return typeof x === 'function' && 'imps' in x && Array.isArray(x.imps)
|
||||||
|
}
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
import assert from 'assert'
|
import assert from 'assert'
|
||||||
import math from '../picomath.js'
|
import math from '../picomath.js'
|
||||||
import picoInstance from '../picomathInstance.js'
|
import picoInstance from '../picomathInstance.js'
|
||||||
import createNumber from '../number/all.js'
|
import createNumbers from '../number/all.js'
|
||||||
|
import createNumber from '../number/number.js'
|
||||||
|
import createAdd from '../number/add.js'
|
||||||
import createComplex from '../complex/all.js'
|
import createComplex from '../complex/all.js'
|
||||||
|
import extendByComplex from '../complex/extendByComplex.js'
|
||||||
|
|
||||||
describe('Custom instances', () => {
|
describe('Custom instances', () => {
|
||||||
it("doesn't matter what order you assemble", () => {
|
it("doesn't matter what order you assemble", () => {
|
||||||
const bw = picoInstance('backwards')
|
const bw = picoInstance('backwards')
|
||||||
createComplex(bw)
|
createComplex(bw)
|
||||||
createNumber(bw)
|
createNumbers(bw)
|
||||||
|
|
||||||
assert.strictEqual(bw.subtract(16, bw.add(3,4,2)), 7)
|
assert.strictEqual(bw.subtract(16, bw.add(3,4,2)), 7)
|
||||||
assert.strictEqual(bw.negate(bw.number('8')), -8)
|
assert.strictEqual(bw.negate(bw.number('8')), -8)
|
||||||
@ -18,4 +21,19 @@ describe('Custom instances', () => {
|
|||||||
math.complex(11, -4)) // note both instances coexist
|
math.complex(11, -4)) // note both instances coexist
|
||||||
assert.deepStrictEqual(bw.negate(math.complex(3, '8')).im, -8)
|
assert.deepStrictEqual(bw.negate(math.complex(3, '8')).im, -8)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("can selectively import in cute ways", async function () {
|
||||||
|
const cherry = picoInstance('cherry')
|
||||||
|
createNumber(cherry)
|
||||||
|
createAdd(cherry)
|
||||||
|
await extendByComplex(cherry)
|
||||||
|
// Now we have an instance that supports addition for number and complex
|
||||||
|
// and little else:
|
||||||
|
assert.strictEqual(cherry.add(3, 4, 2), 9)
|
||||||
|
assert.deepStrictEqual(
|
||||||
|
cherry.add(cherry.complex(3, 3), 4, cherry.complex(2, 2)),
|
||||||
|
math.complex(9,5))
|
||||||
|
assert.strictEqual('subtract' in cherry, false)
|
||||||
|
assert.strictEqual('negate' in cherry, false)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user