From 340dbd436ee16c635d27595a1c663c118b1aef3e Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Thu, 11 Aug 2022 23:51:47 -0700 Subject: [PATCH] feat(PocomathInstance): Add .returnTypeOf method --- src/core/PocomathInstance.mjs | 12 +++++++++++- test/_pocomath.mjs | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/core/PocomathInstance.mjs b/src/core/PocomathInstance.mjs index 9f9520b..f8d9c5a 100644 --- a/src/core/PocomathInstance.mjs +++ b/src/core/PocomathInstance.mjs @@ -2,7 +2,7 @@ import typed from 'typed-function' import {makeChain} from './Chain.mjs' import {dependencyExtractor, generateTypeExtractor} from './extractors.mjs' -import R_ from './returns.mjs' +import {R_, returnTypeOf} from './returns.mjs' import {typeListOfSignature, typesOfSignature, subsetOfKeys} from './utils.mjs' const anySpec = {} // fixed dummy specification of 'any' type @@ -191,6 +191,16 @@ export default class PocomathInstance { return result } + /* Determine the return type of an operation given an input signature */ + returnTypeOf(operation, signature) { + if (typeof operation === 'string') { + operation = this[operation] + } + if (!(this._typed.isTypedFunction(operation))) return 'any' + const details = this._typed.findSignature(operation, signature) + return returnTypeOf(details.fn) + } + /* Return a chain object for this instance with a given value: */ chain(value) { return makeChain(value, this, this._chainRepository) diff --git a/test/_pocomath.mjs b/test/_pocomath.mjs index 38df18b..9e52a25 100644 --- a/test/_pocomath.mjs +++ b/test/_pocomath.mjs @@ -20,6 +20,10 @@ describe('The default full pocomath instance "math"', () => { assert.strictEqual(math.typeOf({re: 6.28, im: 2.72}), 'Complex') }) + it('can determine the return types of operations', () => { + assert.strictEqual(math.returnTypeOf('negate', 'number'), 'number') + }) + it('can subtract numbers', () => { assert.strictEqual(math.subtract(12, 5), 7) //assert.strictEqual(math.subtract(3n, 1.5), 1.5)