Skip to content

Commit

Permalink
fix: eval math for non-numerical val wrapped in spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Jul 26, 2023
1 parent c36a1ae commit 0032d8d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/pretty-ghosts-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tokens-studio/sd-transforms': patch
---

Hotfix for evaluate math of values that have non-numericals wrapped in spaces.
4 changes: 3 additions & 1 deletion src/checkAndEvaluateMath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ function parseAndReduce(expr: string): string {
return expr;
}
// Put back the px unit if needed and if reduced doesn't come with one
return `${Number.parseFloat(evaluated.toFixed(3))}${unit ?? (hasPx ? 'px' : '')}`;
return `${typeof evaluated !== 'string' ? Number.parseFloat(evaluated.toFixed(3)) : evaluated}${
unit ?? (hasPx ? 'px' : '')
}`;
}

export function checkAndEvaluateMath(expr: string | number | undefined): string | undefined {
Expand Down
4 changes: 4 additions & 0 deletions test/spec/checkAndEvaluateMath.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ describe('check and evaluate math', () => {
`800 italic 16px/1 'Arial Black'`,
);
});

it('supports values that contain spaces and strings, e.g. a date format', () => {
expect(checkAndEvaluateMath(`dd/MM/yyyy 'om' HH:mm`)).to.equal(`dd/MM/yyyy om HH:mm`);
});
});

0 comments on commit 0032d8d

Please sign in to comment.