Skip to content

Commit 3abff94

Browse files
authored
Merge pull request #52 from styled-components/brunolemos-patch-1
Handle null and undefined values
2 parents 10a8767 + ac06fd6 commit 3abff94

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const TokenStream = require('./TokenStream');
77
// Note if this is wrong, you'll need to change tokenTypes.js too
88
const numberOrLengthRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?:px)?$/i;
99
const boolRe = /^true|false$/i;
10+
const nullRe = /^null$/i;
11+
const undefinedRe = /^undefined$/i;
1012

1113
// Undocumented export
1214
export const transformRawValue = (input) => {
@@ -18,6 +20,12 @@ export const transformRawValue = (input) => {
1820
const boolMatch = input.match(boolRe);
1921
if (boolMatch !== null) return boolMatch[0].toLowerCase() === 'true';
2022

23+
const nullMatch = input.match(nullRe);
24+
if (nullMatch !== null) return null;
25+
26+
const undefinedMatch = input.match(undefinedRe);
27+
if (undefinedMatch !== null) return undefined;
28+
2129
return value;
2230
};
2331

src/index.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ it('allows pixels in unspecialized transform', () => runTest([
1717
['top', '0px'],
1818
], { top: 0 }));
1919

20-
it('allows boolean values values', () => runTest([
20+
it('allows boolean values', () => runTest([
2121
['boolTrue1', 'true'],
2222
['boolTrue2', 'TRUE'],
2323
['boolFalse1', 'false'],
@@ -29,6 +29,22 @@ it('allows boolean values values', () => runTest([
2929
boolFalse2: false,
3030
}));
3131

32+
it('allows null values', () => runTest([
33+
['null1', 'null'],
34+
['null2', 'NULL'],
35+
], {
36+
null1: null,
37+
null2: null,
38+
}));
39+
40+
it('allows undefined values', () => runTest([
41+
['undefined1', 'undefined'],
42+
['undefined2', 'UNDEFINED'],
43+
], {
44+
undefined1: undefined,
45+
undefined2: undefined,
46+
}));
47+
3248
it('allows percent in unspecialized transform', () => runTest([
3349
['top', '0%'],
3450
], { top: '0%' }));

0 commit comments

Comments
 (0)