Skip to content

Commit

Permalink
fix(transforms): add NaN check to all size transforms (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyjwalt authored and dbanksdesign committed Sep 28, 2020
1 parent 199ac4d commit 14915c4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
24 changes: 24 additions & 0 deletions __tests__/common/transforms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ describe('common', () => {
expect(value).toBe("12.00sp");
expect(value2).toBe("12.00sp");
});
it('should throw an error if prop value is Nan', () => {
expect( () => transforms["size/sp"].transformer({value: "a"})).toThrow();
});
});

describe('size/dp', () => {
Expand All @@ -368,6 +371,9 @@ describe('common', () => {
expect(value).toBe("12.00dp");
expect(value2).toBe("12.00dp");
});
it('should throw an error if prop value is Nan', () => {
expect( () => transforms["size/dp"].transformer({value: "a"})).toThrow();
});
});

describe('size/remToSp', () => {
Expand All @@ -377,6 +383,9 @@ describe('common', () => {
});
expect(value).toBe("16.00sp");
});
it('should throw an error if prop value is Nan', () => {
expect( () => transforms["size/dp"].transformer({value: "a"})).toThrow();
});
});

describe('size/remToDp', () => {
Expand All @@ -386,6 +395,9 @@ describe('common', () => {
});
expect(value).toBe("16.00dp");
});
it('should throw an error if prop value is Nan', () => {
expect( () => transforms["size/dp"].transformer({value: "a"})).toThrow();
});
});

describe('size/px', () => {
Expand All @@ -395,6 +407,9 @@ describe('common', () => {
});
expect(value).toBe("10px");
});
it('should throw an error if prop value is Nan', () => {
expect( () => transforms["size/dp"].transformer({value: "a"})).toThrow();
});
});

describe('size/remToPt', () => {
Expand All @@ -404,6 +419,9 @@ describe('common', () => {
});
expect(value).toBe("16.00f");
});
it('should throw an error if prop value is Nan', () => {
expect( () => transforms["size/dp"].transformer({value: "a"})).toThrow();
});
});

describe('size/remToPx', () => {
Expand All @@ -413,6 +431,9 @@ describe('common', () => {
});
expect(value).toBe("16px");
});
it('should throw an error if prop value is Nan', () => {
expect( () => transforms["size/dp"].transformer({value: "a"})).toThrow();
});
});

describe('size/rem', () => {
Expand All @@ -422,6 +443,9 @@ describe('common', () => {
});
expect(value).toBe("1rem");
});
it('should throw an error if prop value is Nan', () => {
expect( () => transforms["size/dp"].transformer({value: "a"})).toThrow();
});
});

describe('content/quote', () => {
Expand Down
40 changes: 31 additions & 9 deletions lib/common/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ function wrapValueWith(character, prop) {
function wrapValueWithDoubleQuote(prop) {
return wrapValueWith('"', prop);
}

function throwSizeError(name, value, unitType) {
throw `Invalid Number: '${name}: ${value}' is not a valid number, cannot transform to '${unitType}' \n`;
}
/**
* @namespace Transforms
*/
Expand Down Expand Up @@ -463,7 +467,9 @@ module.exports = {
type: 'value',
matcher: isFontSize,
transformer: function(prop) {
return parseFloat(prop.value).toFixed(2) + 'sp';
const val = parseFloat(prop.value);
if (isNaN(val)) throwSizeError(prop.name, prop.value, 'sp');
return val.toFixed(2) + 'sp';
}
},

Expand All @@ -482,7 +488,9 @@ module.exports = {
type: 'value',
matcher: isNotFontSize,
transformer: function(prop) {
return parseFloat(prop.value).toFixed(2) + 'dp';
const val = parseFloat(prop.value);
if (isNaN(val)) throwSizeError(prop.name, prop.value, 'dp');
return val.toFixed(2) + 'dp';
}
},

Expand All @@ -501,7 +509,9 @@ module.exports = {
type: 'value',
matcher: isFontSize,
transformer: function(prop) {
return (parseFloat(prop.value) * 16).toFixed(2) + 'sp';
const val = parseFloat(prop.value);
if (isNaN(val)) throwSizeError(prop.name, prop.value, 'sp');
return (val * 16).toFixed(2) + 'sp';
}
},

Expand All @@ -521,7 +531,9 @@ module.exports = {
type: 'value',
matcher: isNotFontSize,
transformer: function(prop) {
return (parseFloat(prop.value) * 16).toFixed(2) + 'dp';
const val = parseFloat(prop.value);
if (isNaN(val)) throwSizeError(prop.name, prop.value, 'dp');
return (val * 16).toFixed(2) + 'dp';
}
},

Expand All @@ -541,7 +553,9 @@ module.exports = {
type: 'value',
matcher: isSize,
transformer: function(prop) {
return parseFloat(prop.value) + 'px';
const val = parseFloat(prop.value);
if (isNaN(val)) throwSizeError(prop.name, prop.value, 'px');
return val + 'px';
}
},

Expand All @@ -560,7 +574,9 @@ module.exports = {
type: 'value',
matcher: isSize,
transformer: function(prop) {
return parseFloat(prop.value) + 'rem';
const val = parseFloat(prop.value);
if (isNaN(val)) throwSizeError(prop.name, prop.value, 'rem');
return val + 'rem';
}
},

Expand All @@ -579,7 +595,9 @@ module.exports = {
type: 'value',
matcher: isSize,
transformer: function(prop) {
return (parseFloat(prop.value) * 16).toFixed(2) + 'f';
const val = parseFloat(prop.value);
if (isNaN(val)) throwSizeError(prop.name, prop.value, 'pt');
return (val * 16).toFixed(2) + 'f';
}
},

Expand All @@ -597,7 +615,9 @@ module.exports = {
type: 'value',
matcher: isSize,
transformer: function(prop) {
return `CGFloat(${(parseFloat(prop.value) * 16).toFixed(2)})`;
const val = parseFloat(prop.value);
if (isNaN(val)) throwSizeError(prop.name, prop.value, 'CGFloat');
return `CGFloat(${(val * 16).toFixed(2)})`;
}
},

Expand All @@ -616,7 +636,9 @@ module.exports = {
type: 'value',
matcher: isSize,
transformer: function(prop) {
return (parseFloat(prop.value) * 16).toFixed(0) + 'px';
const val = parseFloat(prop.value);
if (isNaN(val)) throwSizeError(prop.name, prop.value, 'px');
return (val * 16).toFixed(0) + 'px';
}
},

Expand Down

0 comments on commit 14915c4

Please sign in to comment.