feat: implicit convert BooleanT to NumberT
All checks were successful
/ test (pull_request) Successful in 19s
All checks were successful
/ test (pull_request) Successful in 19s
Also adds implicit conversion configuration option to Type constructor, and institutes a numbers-only bundle, and supports argument matching with implicit conversions. Still need to test that a behavior that invokes implicit conversion ends up with the conversion operation as a dependency (and so regenerates itself if the conversion changes).
This commit is contained in:
parent
5bee93dbb3
commit
bfc64f3789
14 changed files with 95 additions and 34 deletions
|
|
@ -2,5 +2,5 @@ import {Type} from '#core/Type.js'
|
|||
import {BooleanT} from '#boolean/BooleanT.js'
|
||||
|
||||
export const NumberT = new Type(n => typeof n === 'number', {
|
||||
from: [BooleanT]
|
||||
from: [[BooleanT, math => math.number.resolve([BooleanT])]],
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import assert from 'assert'
|
||||
import {NumberT} from '../NumberT.js'
|
||||
import {BooleanT} from '#boolean/BooleanT.js'
|
||||
import math from '#nanomath'
|
||||
|
||||
describe('NumberT Type', () => {
|
||||
it('correctly recognizes numbers', () => {
|
||||
|
|
@ -8,4 +10,10 @@ describe('NumberT Type', () => {
|
|||
assert(NumberT.test(Infinity))
|
||||
assert(!NumberT.test("3"))
|
||||
})
|
||||
|
||||
it('can convert from BooleanT to NumberT', () => {
|
||||
const cnvBtoN = NumberT.from.get(BooleanT)(math)
|
||||
assert.strictEqual(cnvBtoN(true), 1)
|
||||
assert.strictEqual(cnvBtoN(false), 0)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -4,5 +4,7 @@ import math from '#nanomath'
|
|||
describe('number type operations', () => {
|
||||
it('converts to number', () => {
|
||||
assert.strictEqual(math.number(2.637), 2.637)
|
||||
assert.strictEqual(math.number(true), 1)
|
||||
assert.strictEqual(math.number(false), 0)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue