Skip to content

Commit 01d5655

Browse files
committed
Fix parsing numbers, bump version. Fixes styled-components#17
1 parent e3ea005 commit 01d5655

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "css-to-react-native",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Convert CSS text to a React Native stylesheet object",
55
"main": "dist/index.js",
66
"scripts": {

src/grammar.ne

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
%}
5151

5252
number
53-
-> "-":? ([0-9]:? "." [0-9]:+ | [1-9] [0-9]:* | "0") {% d => Number(text(d)) %}
53+
-> "-":? ([0-9]:? "." [0-9]:+ | [1-9] [0-9]:* ("." [0-9]:+):? | "0") {% d => Number(text(d)) %}
5454

5555
angle -> number ("deg" | "rad") {% text %}
5656

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ const transformRawValue = input => (
2727
: input
2828
);
2929

30+
export const parseProp = (propName, value) =>
31+
new nearley.Parser(grammar.ParserRules, propName).feed(value).results[0];
32+
3033
export const getStylesForProperty = (propName, inputValue) => {
3134
const value = inputValue.trim();
3235

3336
const propValue = (transforms.indexOf(propName) !== -1)
34-
? (new nearley.Parser(grammar.ParserRules, propName).feed(value).results[0])
37+
? parseProp(propName, value)
3538
: transformRawValue(value);
3639

3740
return (propValue && propValue.$merge)

src/index.test.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* global jest it, expect */
2-
import transformCss from '.';
2+
import transformCss, { parseProp } from '.';
33

44
const runTest = (inputCss, expectedStyles) => {
55
const actualStyles = transformCss(inputCss);
@@ -13,9 +13,16 @@ it('transforms numbers', () => runTest([
1313
['bottom', '0'],
1414
], { top: 0, left: 0, right: 0, bottom: 0 }));
1515

16-
it('allows decimal values', () => runTest([
17-
['top', '1.5'],
18-
], { top: 1.5 }));
16+
it('allows decimal values', () => {
17+
expect(parseProp('number', '0.5')).toBe(0.5);
18+
expect(parseProp('number', '1.5')).toBe(1.5);
19+
expect(parseProp('number', '10.5')).toBe(10.5);
20+
expect(parseProp('number', '100.5')).toBe(100.5);
21+
expect(parseProp('number', '-0.5')).toBe(-0.5);
22+
expect(parseProp('number', '-1.5')).toBe(-1.5);
23+
expect(parseProp('number', '-10.5')).toBe(-10.5);
24+
expect(parseProp('number', '-100.5')).toBe(-100.5);
25+
});
1926

2027
it('allows decimal values in transformed values', () => runTest([
2128
['border-radius', '1.5'],

0 commit comments

Comments
 (0)