feat: Initial core of picomath

Implements a totally simplistic "poortf" mutable typed function and
  a picomath instance generator, as well as the very beginnings of a
  number type and one generic function and a default full picomath instance.

  Also provides some tests which serve as usage examples.
This commit is contained in:
Glen Whitney 2022-03-25 00:49:03 -07:00
parent 36cc91ca95
commit 536656bfe8
14 changed files with 1627 additions and 2 deletions

15
test/_picomath.js Normal file
View file

@ -0,0 +1,15 @@
import assert from 'assert'
import math from '../picomath.js'
describe('The default full picomath instance "math"', () => {
it('performs basic arithmetic operations', () => {
assert.strictEqual(math.subtract(16, math.add(3,4,2)), 7)
assert.strictEqual(math.negate(math.number('8')), -8)
})
it('can be extended', () => {
math('add', [args => typeof args[0] === 'string',
(...addends) => addends.reduce((x,y) => x+y, '')])
assert.strictEqual(math.add('Kilroy',' is here'), 'Kilroy is here')
})
})

13
test/_picomathInstance.js Normal file
View file

@ -0,0 +1,13 @@
import assert from 'assert'
import picomathInstance from '../picomathInstance.js'
describe('picomath core', () => {
it('creates an instance that can define TFs', () => {
const pmath = picomathInstance('pmath')
pmath('add', [() => true, (a,b) => a+b])
assert.strictEqual(pmath.add(2,2), 4)
assert.strictEqual(pmath.add('Kilroy', 17), 'Kilroy17')
assert.strictEqual(pmath.add(1), NaN)
// I guess + never throws!
})
})

41
test/_poortf.js Normal file
View file

@ -0,0 +1,41 @@
import assert from 'assert'
import poortf from '../poortf.js'
describe('poortf', () => {
const slate = poortf('slate')
it('creates an empty tf', () => {
assert.throws(() => slate('empty'), TypeError)
assert.throws(() => slate('empty'), /slate.*empty/)
})
const add = poortf('add', [
[args => Array.from(args).every(a => typeof a === 'number'),
(a, b) => a+b],
[args => Array.from(args).every(a => typeof a === 'boolean'),
(p, q) => p || q]
])
it('creates a tf with initial behaviors', () => {
assert.strictEqual(add(2,2), 4)
assert.strictEqual(add(true, false), true)
assert.throws(() => add('kilroy'), TypeError)
})
it('extends an empty tf', () => {
slate.addImps([
[args => typeof args[0] === 'string', s => s + ' wuz here'],
[args => typeof args[0] === 'number', () => 'I am not a number']
])
assert.strictEqual(slate('Kilroy', 'was here'), 'Kilroy wuz here')
assert.strictEqual(slate(2, 'was here'), 'I am not a number')
assert.throws(() => slate(['Ha!']), TypeError)
})
it('extends a tf with other behaviors', () => {
add.addImps([args => typeof args[0] === 'string', (s,x) => s + x]),
assert.strictEqual(add('Kilroy', 23), 'Kilroy23')
assert.throws(() => add(['Ha!'], 'gotcha'), TypeError)
})
})