Skip to content

Commit

Permalink
fix: support type number in checkAndEvaluateMath
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Apr 27, 2023
1 parent f9119ce commit d37eebc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-phones-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tokens-studio/sd-transforms': patch
---

Support values of type number for checkAndEvaluateMath utility.
4 changes: 2 additions & 2 deletions src/checkAndEvaluateMath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ function parseAndReduce(expr: string): string {
return `${Number.parseFloat(evaluated.toFixed(3))}${unit ?? (hasPx ? 'px' : '')}`;
}

export function checkAndEvaluateMath(expr: string | undefined): string | undefined {
export function checkAndEvaluateMath(expr: string | number | undefined): string | undefined {
if (expr === undefined) {
return expr;
}
const exprs = splitMultiIntoSingleValues(expr);
const exprs = splitMultiIntoSingleValues(`${expr}`);
const reducedExprs = exprs.map(_expr => parseAndReduce(_expr));
return reducedExprs.join(' ');
}
4 changes: 4 additions & 0 deletions test/spec/checkAndEvaluateMath.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ describe('check and evaluate math', () => {
expect(checkAndEvaluateMath('(15 + 20 - 17 * 8 / 3) * 7px')).to.equal('-72.333px');
});

it('supports expression of type number', () => {
expect(checkAndEvaluateMath(10)).to.equal('10');
});

it('can evaluate math expressions where more than one token has a unit, in case of px', () => {
expect(checkAndEvaluateMath('4px * 7px')).to.equal('28px');
expect(checkAndEvaluateMath('4 * 7px * 8px')).to.equal('224px');
Expand Down

0 comments on commit d37eebc

Please sign in to comment.