feat: Add all remaining features of original Picomath PoC #5
19
complex/extendToComplex.mjs
Normal file
19
complex/extendToComplex.mjs
Normal file
@ -0,0 +1,19 @@
|
||||
import './Complex.mjs'
|
||||
import * as complex from './complex.mjs'
|
||||
|
||||
/* Add all the complex implementations for functions already
|
||||
in the instance:
|
||||
*/
|
||||
|
||||
export default async function extendToComplex(pmath) {
|
||||
pmath.install(complex)
|
||||
for (const name in pmath._imps) {
|
||||
const modulePath = `./${name}.mjs`
|
||||
try {
|
||||
const mod = await import(modulePath)
|
||||
pmath.install(mod)
|
||||
} catch (err) {
|
||||
// Guess it wasn't a method available in complex; no worries
|
||||
}
|
||||
}
|
||||
}
|
@ -3,9 +3,11 @@ import math from '../pocomath.mjs'
|
||||
import typed from 'typed-function'
|
||||
import PocomathInstance from '../PocomathInstance.mjs'
|
||||
import * as numbers from '../number/all.mjs'
|
||||
import * as numberAdd from '../number/add.mjs'
|
||||
import * as complex from '../complex/all.mjs'
|
||||
import * as complexAdd from '../complex/add.mjs'
|
||||
import * as complexNegate from '../complex/negate.mjs'
|
||||
import extendToComplex from '../complex/extendToComplex.mjs'
|
||||
|
||||
const bw = new PocomathInstance('backwards')
|
||||
describe('A custom instance', () => {
|
||||
@ -38,4 +40,18 @@ describe('A custom instance', () => {
|
||||
assert.deepStrictEqual(
|
||||
pm.subtract({re:5, im:0}, {re:10, im:1}), {re:-5, im: -1})
|
||||
})
|
||||
|
||||
it("can selectively import in cute ways", async function () {
|
||||
const cherry = new PocomathInstance('cherry')
|
||||
cherry.install(numberAdd)
|
||||
await extendToComplex(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