diff --git a/src/boolean/__test__/type.spec.js b/src/boolean/__test__/type.spec.js new file mode 100644 index 0000000..ad294a7 --- /dev/null +++ b/src/boolean/__test__/type.spec.js @@ -0,0 +1,25 @@ +import assert from 'assert' +import math from '#nanomath' +import {Type} from '#core/Type.js' + +const boolean = math.boolean + +describe('boolean type functions', () => { + it('properly converts to boolean', () => { + assert.strictEqual(boolean(false), false) + assert.strictEqual(boolean(true), true) + assert.strictEqual(boolean(0), false) + assert.strictEqual(boolean(-0), false) + assert.strictEqual(boolean(NaN), false) + assert.strictEqual(boolean(Infinity), true) + assert.strictEqual(boolean(1e-30), true) + assert.strictEqual(boolean(undefined), false) + assert.strictEqual(boolean(), false) + assert.strictEqual(boolean(math.types.NumberT), true) + }) + it('converts any type to boolean', () => { + for (const T in math.types) { + if (T instanceof Type) assert(boolean.resolve([T])) + } + }) +}) diff --git a/src/boolean/type.js b/src/boolean/type.js new file mode 100644 index 0000000..169a207 --- /dev/null +++ b/src/boolean/type.js @@ -0,0 +1,14 @@ +import {BooleanT} from './BooleanT.js' +import {onType} from '#core/helpers.js' +import {Returns, Type, TypeOfTypes, Undefined} from '#core/Type.js' +import {NumberT} from '#number/NumberT.js' + +const bool = f => Returns(BooleanT, f) + +export const boolean = onType( + BooleanT, bool(p => p), + NumberT, bool(a => !!a), + TypeOfTypes, bool(() => true), + Undefined, bool(() => false), + [], bool(() => false) +)