Skip to content

Commit cd87b38

Browse files
committed
Cleanup
1 parent b30abe3 commit cd87b38

File tree

5 files changed

+48
-39
lines changed

5 files changed

+48
-39
lines changed

README.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Converts CSS text to a React Native stylesheet object.
44

55
```css
6-
font-size: 18;
7-
line-height: 24;
6+
font-size: 18px;
7+
line-height: 24px;
88
color: red;
99
```
1010

@@ -21,9 +21,9 @@ Converts all number-like values to numbers, and string-like to strings.
2121
Automatically converts indirect values to their React Native equivalents.
2222

2323
```css
24-
text-shadow-offset: 10 5;
24+
text-shadow-offset: 10px 5px;
2525
font-variant: small-caps;
26-
transform: translate(10, 5) scale(5);
26+
transform: translate(10px, 5px) scale(5);
2727
```
2828

2929
```js
@@ -42,8 +42,8 @@ transform: translate(10, 5) scale(5);
4242
Also allows shorthand values.
4343

4444
```css
45-
font: bold 14/16 "Helvetica";
46-
margin: 5 7 2;
45+
font: bold 14px/16px "Helvetica";
46+
margin: 5px 7px 2px;
4747
```
4848

4949
```js
@@ -67,8 +67,6 @@ Shorthands will only accept values that are supported in React, so `background`
6767

6868
`border{Top,Right,Bottom,Left}` shorthands are not supported, because `borderStyle` cannot be applied to individual border sides.
6969

70-
`flex` does not support putting `flexBasis` before `flexGrow`. The supported syntax is `flex: <flex-grow> <flex-shrink> <flex-basis>`.
71-
7270
# API
7371

7472
The API is mostly for implementors. However, the main API may be useful for non-impmentors. The main API is,
@@ -78,9 +76,9 @@ import transform from 'css-to-react-native';
7876
// or const transform = require('css-to-react-native').default;
7977

8078
transform([
81-
['font', 'bold 14/16 "Helvetica"'],
82-
['margin', '5 7 2'],
83-
['border-left-width', '5'],
79+
['font', 'bold 14px/16px "Helvetica"'],
80+
['margin', '5px 7px 2px'],
81+
['border-left-width', '5px'],
8482
]); // => { fontFamily: 'Helvetica', ... }
8583
```
8684

@@ -90,13 +88,13 @@ For implementors, there is also,
9088
import { getPropertyName, getStylesForProperty } from 'css-to-react-native';
9189

9290
getPropertyName('border-width'); // => 'borderWidth'
93-
getStylesForProperty('borderWidth', '1 0 2 0'); // => { borderTopWidth: 1, ... }
91+
getStylesForProperty('borderWidth', '1px 0px 2px 0px'); // => { borderTopWidth: 1, ... }
9492
```
9593

9694
Should you wish to opt-out of transforming certain shorthands, an array of property names in camelCase can be passed as a second argument to `transform`.
9795

9896
```js
99-
transform([['border-radius', '50']], ['borderRadius']);
97+
transform([['border-radius', '50px']], ['borderRadius']);
10098
// { borderRadius: 50 } rather than { borderTopLeft: ... }
10199
```
102100

src/index.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@ const camelizeStyleName = require('fbjs/lib/camelizeStyleName');
44
const transforms = require('./transforms');
55
const TokenStream = require('./TokenStream');
66

7-
const transformRawValue = input => (
8-
(input !== '' && !isNaN(input))
9-
? Number(input)
10-
: input
11-
);
7+
// Note if this is wrong, you'll need to change tokenTypes.js too
8+
const numberOrLengthRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?:px)?$/;
129

13-
export const parseProp = (propName, value) => {
14-
const ast = parse(value).nodes;
15-
const tokenStream = new TokenStream(ast);
16-
return transforms[propName](tokenStream);
10+
// Undocumented export
11+
export const transformRawValue = (input) => {
12+
const value = input.trim().match(numberOrLengthRe);
13+
return value ? Number(value[1]) : input;
1714
};
1815

1916
export const getStylesForProperty = (propName, inputValue, allowShorthand) => {
20-
const value = inputValue.trim();
17+
// Undocumented: allow ast to be passed in
18+
let propValue;
2119

22-
const propValue = (allowShorthand && (propName in transforms))
23-
? parseProp(propName, value)
24-
: transformRawValue(value);
20+
const isRawValue = (allowShorthand === false) || !(propName in transforms);
21+
if (isRawValue) {
22+
const value = typeof inputValue === 'string' ? inputValue : parse.stringify(inputValue);
23+
propValue = transformRawValue(value);
24+
} else {
25+
const ast = typeof inputValue === 'string' ? parse(inputValue.trim()) : inputValue;
26+
const tokenStream = new TokenStream(ast.nodes);
27+
propValue = transforms[propName](tokenStream);
28+
}
2529

2630
return (propValue && propValue.$merge)
2731
? propValue.$merge

src/index.test.js

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

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

16+
it('allows pixels in unspecialized transform', () => runTest([
17+
['top', '0px'],
18+
], { top: 0 }));
19+
20+
it('allows percent in unspecialized transform', () => runTest([
21+
['top', '0%'],
22+
], { top: '0%' }));
23+
1624
it('allows decimal values', () => {
17-
expect(parseProp('margin', '0.5px').$merge.marginTop).toBe(0.5);
18-
expect(parseProp('margin', '1.5px').$merge.marginTop).toBe(1.5);
19-
expect(parseProp('margin', '10.5px').$merge.marginTop).toBe(10.5);
20-
expect(parseProp('margin', '100.5px').$merge.marginTop).toBe(100.5);
21-
expect(parseProp('margin', '-0.5px').$merge.marginTop).toBe(-0.5);
22-
expect(parseProp('margin', '-1.5px').$merge.marginTop).toBe(-1.5);
23-
expect(parseProp('margin', '-10.5px').$merge.marginTop).toBe(-10.5);
24-
expect(parseProp('margin', '-100.5px').$merge.marginTop).toBe(-100.5);
25-
expect(parseProp('margin', '.5px').$merge.marginTop).toBe(0.5);
26-
expect(parseProp('margin', '-.5px').$merge.marginTop).toBe(-0.5);
25+
expect(getStylesForProperty('margin', '0.5px').marginTop).toBe(0.5);
26+
expect(getStylesForProperty('margin', '1.5px').marginTop).toBe(1.5);
27+
expect(getStylesForProperty('margin', '10.5px').marginTop).toBe(10.5);
28+
expect(getStylesForProperty('margin', '100.5px').marginTop).toBe(100.5);
29+
expect(getStylesForProperty('margin', '-0.5px').marginTop).toBe(-0.5);
30+
expect(getStylesForProperty('margin', '-1.5px').marginTop).toBe(-1.5);
31+
expect(getStylesForProperty('margin', '-10.5px').marginTop).toBe(-10.5);
32+
expect(getStylesForProperty('margin', '-100.5px').marginTop).toBe(-100.5);
33+
expect(getStylesForProperty('margin', '.5px').marginTop).toBe(0.5);
34+
expect(getStylesForProperty('margin', '-.5px').marginTop).toBe(-0.5);
2735
});
2836

2937
it('allows decimal values in transformed values', () => runTest([

src/tokenTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const matchColor = (node) => {
1414
};
1515

1616
const noneRe = /^(none)$/;
17+
// Note if these are wrong, you'll need to change index.js too
1718
const numberRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)$/;
1819
const lengthRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)px$/;
1920
const angleRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(?:deg|rad))$/;

src/transforms/transform.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ const xyTransformFactory = tokenType => (key, valueIfOmitted) => (functionStream
1616

1717
let y;
1818
if (functionStream.hasTokens()) {
19-
functionStream.match(SPACE); // optional space
2019
functionStream.expect(COMMA);
21-
functionStream.match(SPACE); // optional space
2220
y = functionStream.expect(tokenType);
2321
} else if (valueIfOmitted !== undefined) {
2422
y = valueIfOmitted;

0 commit comments

Comments
 (0)