Skip to content

Commit

Permalink
Fix break on "0"
Browse files Browse the repository at this point in the history
  • Loading branch information
SorsOps committed Nov 28, 2023
1 parent c0f6782 commit 71f145e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/utils/TokenResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ const tokens = [
value: '000001',
type: TokenTypes.TEXT,
},
{
name: 'pure-numeric',
value: '0',
type: TokenTypes.DIMENSION,
},
];

const output = [
Expand Down Expand Up @@ -589,6 +594,12 @@ const output = [
rawValue: '000001',
type: TokenTypes.TEXT,
},
{
name: 'pure-numeric',
value: 0,
rawValue: '0',
type: TokenTypes.DIMENSION,
},
];
describe('resolveTokenValues deep nested', () => {
it('resolves all values it can resolve', () => {
Expand Down Expand Up @@ -622,4 +633,20 @@ describe('resolveTokenValues deep nested', () => {
const end = performance.now();
expect(end - start).toBeLessThan(500); // Setting to x2 the amount it takes on a test run to cover for variations in performance
});

it('resolves zeros correctly', () => {
const resolvedTokens = defaultTokenResolver.setTokens([{
name: 'pure-zero',
value: '0',
type: TokenTypes.DIMENSION,
}]);
expect(resolvedTokens).toEqual([
{
name: 'pure-zero',
rawValue: '0',
value: 0,
type: TokenTypes.DIMENSION,
},
]);
});
});
3 changes: 2 additions & 1 deletion src/utils/TokenResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class TokenResolver {
const regex = /^[+-]?\d*\.?\d+([eE][+-]?\d+)?$/;
const numericRegex = /^[+-]?\d*\.?\d+$/;

const allZerosRegex = /^0+$/;
// Needs to be more than a single zero otherwise "0" will get evaluated incorrectly
const allZerosRegex = /^00+$/;
const leadingZerosRegex = /^0+[1-9]\d*$/;

return (regex.test(str) && !numericRegex.test(str)) || (allZerosRegex.test(str) || leadingZerosRegex.test(str));
Expand Down

0 comments on commit 71f145e

Please sign in to comment.