feat: Add all remaining features of original Picomath PoC #5

Merged
glen merged 2 commits from original_poc into main 2022-07-19 19:37:53 +00:00
2 changed files with 35 additions and 0 deletions
Showing only changes of commit 92485678f1 - Show all commits

View 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
}
}
}

View File

@ -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)
})
})