|
1 | 1 | /* eslint-disable no-param-reassign */
|
2 |
| -const parse = require("postcss-value-parser"); |
3 |
| -const camelizeStyleName = require("fbjs/lib/camelizeStyleName"); |
4 |
| -const transforms = require("./transforms"); |
5 |
| -const TokenStream = require("./TokenStream"); |
| 2 | +const parse = require('postcss-value-parser') |
| 3 | +const camelizeStyleName = require('fbjs/lib/camelizeStyleName') |
| 4 | +const transforms = require('./transforms') |
| 5 | +const TokenStream = require('./TokenStream') |
6 | 6 |
|
7 | 7 | // Note if this is wrong, you'll need to change tokenTypes.js too
|
8 |
| -const numberOrLengthRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?:px)?$/i; |
9 |
| -const boolRe = /^true|false$/i; |
10 |
| -const nullRe = /^null$/i; |
11 |
| -const undefinedRe = /^undefined$/i; |
| 8 | +const numberOrLengthRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?:px)?$/i |
| 9 | +const boolRe = /^true|false$/i |
| 10 | +const nullRe = /^null$/i |
| 11 | +const undefinedRe = /^undefined$/i |
12 | 12 |
|
13 | 13 | // Undocumented export
|
14 | 14 | export const transformRawValue = input => {
|
15 |
| - const value = input.trim(); |
| 15 | + const value = input.trim() |
16 | 16 |
|
17 |
| - const numberMatch = value.match(numberOrLengthRe); |
18 |
| - if (numberMatch !== null) return Number(numberMatch[1]); |
| 17 | + const numberMatch = value.match(numberOrLengthRe) |
| 18 | + if (numberMatch !== null) return Number(numberMatch[1]) |
19 | 19 |
|
20 |
| - const boolMatch = input.match(boolRe); |
21 |
| - if (boolMatch !== null) return boolMatch[0].toLowerCase() === "true"; |
| 20 | + const boolMatch = input.match(boolRe) |
| 21 | + if (boolMatch !== null) return boolMatch[0].toLowerCase() === 'true' |
22 | 22 |
|
23 |
| - const nullMatch = input.match(nullRe); |
24 |
| - if (nullMatch !== null) return null; |
| 23 | + const nullMatch = input.match(nullRe) |
| 24 | + if (nullMatch !== null) return null |
25 | 25 |
|
26 |
| - const undefinedMatch = input.match(undefinedRe); |
27 |
| - if (undefinedMatch !== null) return undefined; |
| 26 | + const undefinedMatch = input.match(undefinedRe) |
| 27 | + if (undefinedMatch !== null) return undefined |
28 | 28 |
|
29 |
| - return value; |
30 |
| -}; |
| 29 | + return value |
| 30 | +} |
31 | 31 |
|
32 | 32 | const baseTransformShorthandValue = (propName, inputValue) => {
|
33 |
| - const ast = parse(inputValue.trim()); |
34 |
| - const tokenStream = new TokenStream(ast.nodes); |
35 |
| - return transforms[propName](tokenStream); |
36 |
| -}; |
| 33 | + const ast = parse(inputValue.trim()) |
| 34 | + const tokenStream = new TokenStream(ast.nodes) |
| 35 | + return transforms[propName](tokenStream) |
| 36 | +} |
37 | 37 |
|
38 | 38 | const transformShorthandValue =
|
39 |
| - process.env.NODE_ENV === "production" |
| 39 | + process.env.NODE_ENV === 'production' |
40 | 40 | ? baseTransformShorthandValue
|
41 | 41 | : (propName, inputValue) => {
|
42 | 42 | try {
|
43 |
| - return baseTransformShorthandValue(propName, inputValue); |
| 43 | + return baseTransformShorthandValue(propName, inputValue) |
44 | 44 | } catch (e) {
|
45 | 45 | throw new Error(
|
46 | 46 | `Failed to parse declaration "${propName}: ${inputValue}"`
|
47 |
| - ); |
| 47 | + ) |
48 | 48 | }
|
49 |
| - }; |
| 49 | + } |
50 | 50 |
|
51 | 51 | export const getStylesForProperty = (propName, inputValue, allowShorthand) => {
|
52 |
| - const isRawValue = allowShorthand === false || !(propName in transforms); |
| 52 | + const isRawValue = allowShorthand === false || !(propName in transforms) |
53 | 53 | const propValue = isRawValue
|
54 | 54 | ? transformRawValue(inputValue)
|
55 |
| - : transformShorthandValue(propName, inputValue.trim()); |
| 55 | + : transformShorthandValue(propName, inputValue.trim()) |
56 | 56 |
|
57 | 57 | return propValue && propValue.$merge
|
58 | 58 | ? propValue.$merge
|
59 |
| - : { [propName]: propValue }; |
60 |
| -}; |
| 59 | + : { [propName]: propValue } |
| 60 | +} |
61 | 61 |
|
62 |
| -export const getPropertyName = camelizeStyleName; |
| 62 | +export const getPropertyName = camelizeStyleName |
63 | 63 |
|
64 | 64 | export default (rules, shorthandBlacklist = []) =>
|
65 | 65 | rules.reduce((accum, rule) => {
|
66 |
| - const propertyName = getPropertyName(rule[0]); |
67 |
| - const value = rule[1]; |
68 |
| - const allowShorthand = shorthandBlacklist.indexOf(propertyName) === -1; |
| 66 | + const propertyName = getPropertyName(rule[0]) |
| 67 | + const value = rule[1] |
| 68 | + const allowShorthand = shorthandBlacklist.indexOf(propertyName) === -1 |
69 | 69 | return Object.assign(
|
70 | 70 | accum,
|
71 | 71 | getStylesForProperty(propertyName, value, allowShorthand)
|
72 |
| - ); |
73 |
| - }, {}); |
| 72 | + ) |
| 73 | + }, {}) |
0 commit comments