refactor: prepare for boolean functions
All checks were successful
/ test (pull_request) Successful in 19s

* Defines a BooleanT type
  * adds options to the Type constructor, so far just to allow conversions
    from other types
  * renames Number type to NumberT
  * records the name of types, and puts the name in the string representation
  * defines a conversion fron BooleanT to NumberT, specified to be automatic
  * stub code for automatic conversions, not yet complete
  * BooleanT not yet added to nanomath

Checked that the new facilities do not disrupt the prior behavior on numbers.
This commit is contained in:
Glen Whitney 2025-04-10 13:57:05 -07:00
parent 14011984a0
commit 5bee93dbb3
16 changed files with 123 additions and 62 deletions

View file

@ -1,3 +0,0 @@
import {Type} from '#core/Type.js'
export const Number = new Type(n => typeof n === 'number')

6
src/number/NumberT.js Normal file
View file

@ -0,0 +1,6 @@
import {Type} from '#core/Type.js'
import {BooleanT} from '#boolean/BooleanT.js'
export const NumberT = new Type(n => typeof n === 'number', {
from: [BooleanT]
})

View file

@ -1,11 +0,0 @@
import assert from 'assert'
import {Number} from '../Number.js'
describe('Number Type', () => {
it('correctly recognizes numbers', () => {
assert(Number.test(3))
assert(Number.test(NaN))
assert(Number.test(Infinity))
assert(!Number.test("3"))
})
})

View file

@ -0,0 +1,11 @@
import assert from 'assert'
import {NumberT} from '../NumberT.js'
describe('NumberT Type', () => {
it('correctly recognizes numbers', () => {
assert(NumberT.test(3))
assert(NumberT.test(NaN))
assert(NumberT.test(Infinity))
assert(!NumberT.test("3"))
})
})

View file

@ -1,4 +1,4 @@
export * as typeDefinition from './Number.js'
export * as typeDefinition from './NumberT.js'
export * as arithmetic from './arithmetic.js'
export * as type from './type.js'
export * as utils from './utils.js'

View file

@ -1,7 +1,7 @@
import {Number} from './Number.js'
import {NumberT} from './NumberT.js'
import {onType} from '#core/helpers.js'
import {Returns} from '#core/Type.js'
export const plain = f => onType(
Array(f.length).fill(Number), Returns(Number, f))
Array(f.length).fill(NumberT), Returns(NumberT, f))

View file

@ -1,4 +1,7 @@
import {plain} from './helpers.js'
import {BooleanT} from '#boolean/BooleanT.js'
import {Returns} from '#core/Type.js'
import {NumberT} from '#number/NumberT.js'
// Not much to do so far when there is only one type
export const number = plain(a => a)
number.also(BooleanT, Returns(NumberT, a => a ? 1 : 0))