Skip to content

Commit 2c3cd3a

Browse files
authored
v2.0.0 (#8)
* Remove getOperands() * Add getVariables()
1 parent f3c58a5 commit 2c3cd3a

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ const valueWithVariable = runArithmetic('2 * four', {
3636
console.log(valueWithVariable); // 8
3737
```
3838

39-
### `getOperands(formulaText: string): string[]`
39+
### `getVariables(formulaText: string): string[]`
4040

4141
Usage example:
4242

4343
```js
44-
import { getOperands } from '@beyondessential/arithmetic';
44+
import { getVariables } from '@beyondessential/arithmetic';
4545

46-
const operands = getOperands('(-a * b) / 3');
47-
console.log(operands); // ['a', 'b', '3']
46+
const variables = getVariables('(-a * b - 1) / (c + 3)');
47+
console.log(variables); // ['a', 'b', 'c']
4848
```

__tests__/symbols.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { getOperands } from '../src/symbols';
8+
import { getVariables } from '../src/symbols';
99

10-
describe('getOperands()', () => {
10+
describe('getVariables()', () => {
1111
const testData = [
12-
['numbers', '1 + 2', ['1', '2']],
1312
['single letters', 'a + b', ['a', 'b']],
1413
['words', 'alpha + beta', ['alpha', 'beta']],
14+
['formula includes numbers', '2 * a + b - 1', ['a', 'b']],
1515
["'u' letter", 'a + u', ['a', 'u']],
1616
["words containing 'u', 'x'", 'user * experience - ux', ['user', 'experience', 'ux']],
1717
['excessive whitespace', ' a + b ', ['a', 'b']],
1818
['no whitespace', 'a+b', ['a', 'b']],
19-
['same operand multiple times', 'a * b + a', ['a', 'b']],
20-
['all operators', '(a + -b) / ((2 * c) - 3 x d)', ['a', 'b', '2', 'c', '3', 'd']],
19+
['same variable multiple times', 'a * b + a', ['a', 'b']],
20+
['all operators', '(a + -b) / ((2 * c) - 3 x d)', ['a', 'b', 'c', 'd']],
2121
];
2222

2323
it.each(testData)('%s', (_, formula, expected) => {
24-
expect(getOperands(formula)).toStrictEqual(expected);
24+
expect(getVariables(formula)).toStrictEqual(expected);
2525
});
2626
});

index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
export function getOperands(formulaText: string): string[];
8+
export function getVariables(formulaText: string): string[];
99
export function runArithmetic(
1010
formulaText: string,
1111
values?: Record<string, string | number>,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@beyondessential/arithmetic",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "Utility to evaluate BODMAS arithmetic formulas",
55
"keywords": [
66
"arithmetic",

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
*/
77

88
export { runArithmetic } from './arithmetic';
9-
export { getOperands } from './symbols';
9+
export { getVariables } from './symbols';

src/symbols.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ export function getPrecedence(operator) {
2525
}
2626
}
2727

28-
export function getOperands(formulaText) {
29-
const operands = formulaText
28+
export function getVariables(formulaText) {
29+
const variables = formulaText
3030
// Replace the alternate multiplication symbol 'x' with a non-alphanumeric character
3131
.replace(/(^|\W)x(\W|$)/, ' ')
3232
.split(/[+-/*() ]/g)
33-
.filter(c => c !== '');
33+
.filter(v => v !== '' && Number.isNaN(Number(v)));
3434

35-
return [...new Set(operands)];
35+
return [...new Set(variables)];
3636
}

0 commit comments

Comments
 (0)