Skip to content

Commit

Permalink
fix: fix sizeRem not transforming negative values (#1432)
Browse files Browse the repository at this point in the history
Co-authored-by: teunverhaert <[email protected]>
  • Loading branch information
teunverhaert and teunverhaert authored Jan 20, 2025
1 parent 7b7d402 commit 1684a8e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/cool-bears-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

Fix sizeRem to allow negative values
50 changes: 50 additions & 0 deletions __tests__/common/transforms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,16 @@ describe('common', () => {
);
expect(value).to.equal('10px');
});
it('should work for negative values', () => {
const value = transforms[sizePx].transform(
{
value: '-10',
},
{},
{},
);
expect(value).to.equal('-10px');
});
it('should throw an error if prop value is Nan', () => {
expect(() => transforms[sizeDp].transform({ value: 'a' }, {}, {})).to.throw();
});
Expand Down Expand Up @@ -957,6 +967,46 @@ describe('common', () => {
);
expect(value).to.equal('1rem');
});
it('should work for negative values with unit', () => {
const value = transforms[sizeRem].transform(
{
value: '-1rem',
},
{},
{},
);
expect(value).to.equal('-1rem');
});
it('should work for negative values', () => {
const value = transforms[sizeRem].transform(
{
value: '-1',
},
{},
{},
);
expect(value).to.equal('-1rem');
});
it('should work for positive values', () => {
const value = transforms[sizeRem].transform(
{
value: '+1',
},
{},
{},
);
expect(value).to.equal('1rem');
});
it('should work for floating values', () => {
const value = transforms[sizeRem].transform(
{
value: '.5',
},
{},
{},
);
expect(value).to.equal('0.5rem');
});
['0', 0].forEach((value) => {
it('zero value is returned without a unit and remains same type', () => {
expect(
Expand Down
2 changes: 1 addition & 1 deletion lib/common/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ export default {
transform: function (token, _, options) {
const nonParsed = options.usesDtcg ? token.$value : token.value;
// if the dimension already has a unit (non-digit / . period character)
if (`${nonParsed}`.match(/[^0-9.]/g)) {
if (`${nonParsed}`.match(/^[^0-9.-]+$/)) {
return nonParsed;
}
const parsedVal = parseFloat(nonParsed);
Expand Down

0 comments on commit 1684a8e

Please sign in to comment.