diff --git a/package.json b/package.json index 96c290f..ac50a10 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "css-to-react-native", - "version": "1.0.5", + "version": "1.0.6", "description": "Convert CSS text to a React Native stylesheet object", "main": "dist/index.js", "scripts": { diff --git a/src/grammar.ne b/src/grammar.ne index c69a116..debbc08 100644 --- a/src/grammar.ne +++ b/src/grammar.ne @@ -49,8 +49,14 @@ }; %} +int + -> "0" | [1-9] [0-9]:* + +decimal + -> "." [0-9]:+ + number - -> "-":? ([0-9]:? "." [0-9]:+ | [1-9] [0-9]:* | "0") {% d => Number(text(d)) %} + -> "-":? (int decimal | int | decimal) {% d => Number(text(d)) %} angle -> number ("deg" | "rad") {% text %} diff --git a/src/index.js b/src/index.js index 9f1be86..4434420 100644 --- a/src/index.js +++ b/src/index.js @@ -27,11 +27,14 @@ const transformRawValue = input => ( : input ); +export const parseProp = (propName, value) => + new nearley.Parser(grammar.ParserRules, propName).feed(value).results[0]; + export const getStylesForProperty = (propName, inputValue, allowShorthand) => { const value = inputValue.trim(); const propValue = (allowShorthand && transforms.indexOf(propName) !== -1) - ? (new nearley.Parser(grammar.ParserRules, propName).feed(value).results[0]) + ? parseProp(propName, value) : transformRawValue(value); return (propValue && propValue.$merge) diff --git a/src/index.test.js b/src/index.test.js index cb41e7a..c30d6ad 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -1,5 +1,5 @@ /* global jest it, expect */ -import transformCss from '.'; +import transformCss, { parseProp } from '.'; const runTest = (inputCss, expectedStyles) => { const actualStyles = transformCss(inputCss); @@ -13,9 +13,18 @@ it('transforms numbers', () => runTest([ ['bottom', '0'], ], { top: 0, left: 0, right: 0, bottom: 0 })); -it('allows decimal values', () => runTest([ - ['top', '1.5'], -], { top: 1.5 })); +it('allows decimal values', () => { + expect(parseProp('number', '0.5')).toBe(0.5); + expect(parseProp('number', '1.5')).toBe(1.5); + expect(parseProp('number', '10.5')).toBe(10.5); + expect(parseProp('number', '100.5')).toBe(100.5); + expect(parseProp('number', '-0.5')).toBe(-0.5); + expect(parseProp('number', '-1.5')).toBe(-1.5); + expect(parseProp('number', '-10.5')).toBe(-10.5); + expect(parseProp('number', '-100.5')).toBe(-100.5); + expect(parseProp('number', '.5')).toBe(0.5); + expect(parseProp('number', '-.5')).toBe(-0.5); +}); it('allows decimal values in transformed values', () => runTest([ ['border-radius', '1.5'],