diff --git a/README.md b/README.md index ff2d5e7..5bf9ae2 100644 --- a/README.md +++ b/README.md @@ -36,13 +36,13 @@ const valueWithVariable = runArithmetic('2 * four', { console.log(valueWithVariable); // 8 ``` -### `getOperands(formulaText: string): string[]` +### `getVariables(formulaText: string): string[]` Usage example: ```js -import { getOperands } from '@beyondessential/arithmetic'; +import { getVariables } from '@beyondessential/arithmetic'; -const operands = getOperands('(-a * b) / 3'); -console.log(operands); // ['a', 'b', '3'] +const variables = getVariables('(-a * b - 1) / (c + 3)'); +console.log(variables); // ['a', 'b', 'c'] ``` diff --git a/__tests__/symbols.test.js b/__tests__/symbols.test.js index bfc6233..6189278 100644 --- a/__tests__/symbols.test.js +++ b/__tests__/symbols.test.js @@ -5,22 +5,22 @@ * LICENSE file in the root directory of this source tree. */ -import { getOperands } from '../src/symbols'; +import { getVariables } from '../src/symbols'; -describe('getOperands()', () => { +describe('getVariables()', () => { const testData = [ - ['numbers', '1 + 2', ['1', '2']], ['single letters', 'a + b', ['a', 'b']], ['words', 'alpha + beta', ['alpha', 'beta']], + ['formula includes numbers', '2 * a + b - 1', ['a', 'b']], ["'u' letter", 'a + u', ['a', 'u']], ["words containing 'u', 'x'", 'user * experience - ux', ['user', 'experience', 'ux']], ['excessive whitespace', ' a + b ', ['a', 'b']], ['no whitespace', 'a+b', ['a', 'b']], - ['same operand multiple times', 'a * b + a', ['a', 'b']], - ['all operators', '(a + -b) / ((2 * c) - 3 x d)', ['a', 'b', '2', 'c', '3', 'd']], + ['same variable multiple times', 'a * b + a', ['a', 'b']], + ['all operators', '(a + -b) / ((2 * c) - 3 x d)', ['a', 'b', 'c', 'd']], ]; it.each(testData)('%s', (_, formula, expected) => { - expect(getOperands(formula)).toStrictEqual(expected); + expect(getVariables(formula)).toStrictEqual(expected); }); }); diff --git a/index.d.ts b/index.d.ts index 49541a7..9b455ca 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -export function getOperands(formulaText: string): string[]; +export function getVariables(formulaText: string): string[]; export function runArithmetic( formulaText: string, values?: Record, diff --git a/package.json b/package.json index fdf8755..9bb8cb2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@beyondessential/arithmetic", - "version": "1.0.0", + "version": "2.0.0", "description": "Utility to evaluate BODMAS arithmetic formulas", "keywords": [ "arithmetic", diff --git a/src/index.js b/src/index.js index d3e5fca..c01399e 100644 --- a/src/index.js +++ b/src/index.js @@ -6,4 +6,4 @@ */ export { runArithmetic } from './arithmetic'; -export { getOperands } from './symbols'; +export { getVariables } from './symbols'; diff --git a/src/symbols.js b/src/symbols.js index db9204a..d2b79b5 100644 --- a/src/symbols.js +++ b/src/symbols.js @@ -25,12 +25,12 @@ export function getPrecedence(operator) { } } -export function getOperands(formulaText) { - const operands = formulaText +export function getVariables(formulaText) { + const variables = formulaText // Replace the alternate multiplication symbol 'x' with a non-alphanumeric character .replace(/(^|\W)x(\W|$)/, ' ') .split(/[+-/*() ]/g) - .filter(c => c !== ''); + .filter(v => v !== '' && Number.isNaN(Number(v))); - return [...new Set(operands)]; + return [...new Set(variables)]; }