Skip to content

Commit 1aef9df

Browse files
committed
Allow percent values, cleanup
1 parent cd87b38 commit 1aef9df

File tree

6 files changed

+75
-82
lines changed

6 files changed

+75
-82
lines changed

src/index.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ it('allows negative values in transformed values', () => runTest([
5252
borderBottomLeftRadius: -1.5,
5353
}));
5454

55+
it('allows percent values in transformed values', () => runTest([
56+
['margin', '10%'],
57+
], {
58+
marginTop: '10%',
59+
marginRight: '10%',
60+
marginBottom: '10%',
61+
marginLeft: '10%',
62+
}));
63+
5564
it('transforms strings', () => runTest([
5665
['color', 'red'],
5766
], { color: 'red' }));

src/transforms/border.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/transforms/flex.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ module.exports = (tokenStream) => {
2929
}
3030
} else if (flexBasis === undefined && tokenStream.match(LENGTH)) {
3131
flexBasis = tokenStream.lastValue;
32+
} else {
33+
tokenStream.throw();
3234
}
3335

3436
partsParsed += 1;

src/transforms/flexFlow.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/transforms/index.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
const { tokens } = require('../tokenTypes');
2-
const border = require('./border');
1+
const { regExpToken, tokens } = require('../tokenTypes');
32
const flex = require('./flex');
4-
const flexFlow = require('./flexFlow');
53
const font = require('./font');
64
const transform = require('./transform');
7-
const { directionFactory, shadowOffsetFactory } = require('./util');
5+
const { directionFactory, anyOrderFactory, shadowOffsetFactory } = require('./util');
86

97
const { WORD, COLOR } = tokens;
108

119
const background = tokenStream => ({ $merge: { backgroundColor: tokenStream.match(COLOR) } });
10+
const border = anyOrderFactory({
11+
borderWidth: {
12+
token: tokens.LENGTH,
13+
default: 1,
14+
},
15+
borderColor: {
16+
token: COLOR,
17+
default: 'black',
18+
},
19+
borderStyle: {
20+
token: regExpToken(/^(solid|dashed|dotted)$/),
21+
default: 'solid',
22+
},
23+
});
1224
const borderColor = directionFactory({ type: 'word', prefix: 'border', suffix: 'Color' });
1325
const borderRadius = directionFactory({
1426
directions: ['TopRight', 'BottomRight', 'BottomLeft', 'TopLeft'],
@@ -18,6 +30,16 @@ const borderRadius = directionFactory({
1830
const borderWidth = directionFactory({ prefix: 'border', suffix: 'Width' });
1931
const margin = directionFactory({ prefix: 'margin' });
2032
const padding = directionFactory({ prefix: 'padding' });
33+
const flexFlow = anyOrderFactory({
34+
flexWrap: {
35+
token: regExpToken(/(nowrap|wrap|wrap-reverse)/),
36+
default: 'nowrap',
37+
},
38+
flexDirection: {
39+
token: regExpToken(/(row|row-reverse|column|column-reverse)/),
40+
default: 'row',
41+
},
42+
});
2143
const fontVariant = tokenStream => [tokenStream.match(WORD)];
2244
const fontWeight = tokenStream => tokenStream.match(WORD);
2345
const shadowOffset = shadowOffsetFactory();

src/transforms/util.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
const { tokens } = require('../tokenTypes');
22

3-
const { LENGTH, SPACE } = tokens;
3+
const { LENGTH, PERCENT, SPACE } = tokens;
44

55
module.exports.directionFactory = ({
6-
types = [LENGTH],
76
directions = ['Top', 'Right', 'Bottom', 'Left'],
87
prefix = '',
98
suffix = '',
109
}) => (tokenStream) => {
1110
const values = [];
1211

13-
values.push(tokenStream.expect(...types));
12+
// borderWidth doesn't currently allow a percent value, but may do in the future
13+
values.push(tokenStream.expect(LENGTH, PERCENT));
1414

1515
while (values.length < 4 && tokenStream.hasTokens()) {
1616
tokenStream.expect(SPACE);
17-
values.push(tokenStream.expect(...types));
17+
values.push(tokenStream.expect(LENGTH, PERCENT));
1818
}
1919

2020
tokenStream.expectEmpty();
@@ -33,6 +33,40 @@ module.exports.directionFactory = ({
3333
return { $merge: output };
3434
};
3535

36+
module.exports.anyOrderFactory = (properties, delim = SPACE) => (tokenStream) => {
37+
const propertyNames = Object.keys(properties);
38+
const values = propertyNames.reduce((accum, propertyName) => {
39+
accum[propertyName] === undefined; // eslint-disable-line
40+
return accum;
41+
}, {});
42+
43+
let numParsed = 0;
44+
while (numParsed < propertyNames.length && tokenStream.hasTokens()) {
45+
if (numParsed) tokenStream.expect(delim);
46+
47+
let didMatch = false;
48+
for (const propertyName of propertyNames) { // eslint-disable-line
49+
if (values[propertyName] === undefined && tokenStream.match(properties[propertyName].token)) {
50+
values[propertyName] = tokenStream.lastValue;
51+
didMatch = true;
52+
break;
53+
}
54+
}
55+
56+
if (!didMatch) tokenStream.throw();
57+
58+
numParsed += 1;
59+
}
60+
61+
tokenStream.expectEmpty();
62+
63+
propertyNames.forEach((propertyName) => {
64+
if (values[propertyName] === undefined) values[propertyName] = properties[propertyName].default;
65+
});
66+
67+
return { $merge: values };
68+
};
69+
3670
module.exports.shadowOffsetFactory = () => (tokenStream) => {
3771
const width = tokenStream.expect(LENGTH);
3872
const height = tokenStream.match(SPACE)

0 commit comments

Comments
 (0)