Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']
```
12 changes: 6 additions & 6 deletions __tests__/symbols.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string | number>,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@beyondessential/arithmetic",
"version": "1.0.0",
"version": "2.0.0",
"description": "Utility to evaluate BODMAS arithmetic formulas",
"keywords": [
"arithmetic",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
*/

export { runArithmetic } from './arithmetic';
export { getOperands } from './symbols';
export { getVariables } from './symbols';
8 changes: 4 additions & 4 deletions src/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
}