4.54 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var normalizeColor_1 = require("./normalizeColor");
4var NUMBER_REG = /^[-+]?\d*\.?\d+$/;
5function convertUnit(val) {
6 if (NUMBER_REG.test(val)) {
7 return parseFloat(val);
8 }
9 return val;
10}
11function measure(value, key) {
12 var direction = [];
13 if (typeof value === 'number') {
14 direction = [value, value, value, value];
15 }
16 else if (typeof value === 'string') {
17 direction = value.split(/\s+/);
18 switch (direction.length) {
19 case 2:
20 direction[2] = direction[0];
21 direction[3] = direction[1];
22 break;
23 case 3:
24 direction[3] = direction[1];
25 break;
26 case 4:
27 break;
28 default:
29 return {};
30 }
31 }
32 var topKey = key + "Top";
33 var rightKey = key + "Right";
34 var bottomKey = key + "Bottom";
35 var leftKey = key + "Left";
36 var result = {
37 isDeleted: true,
38 };
39 result[topKey] = convertUnit(direction[0]);
40 result[rightKey] = convertUnit(direction[1]);
41 result[bottomKey] = convertUnit(direction[2]);
42 result[leftKey] = convertUnit(direction[3]);
43 return result;
44}
45var border = function (key, value) {
46 var result = {
47 isDeleted: true,
48 };
49 var direction = value && value.split(' ');
50 result[key + "Width"] = direction && convertUnit(direction[0]);
51 result[key + "Style"] = direction && direction[1];
52 result[key + "Color"] = direction && normalizeColor_1.default(direction[2]);
53 return result;
54};
55var toMs = function (value) {
56 if (typeof value === 'string') {
57 if (/^\./.test(value))
58 value = "0" + value; // .5s
59 if (/s$/.test(value) && !/ms$/.test(value)) { // 1.5s
60 value = parseFloat(value) * 1000;
61 }
62 else { // 150 or 150ms
63 value = parseFloat(value);
64 }
65 }
66 return (value || 0) + "ms";
67};
68var transitionProperty = function (value) {
69 if (value === 'all') {
70 value = 'width,height,top,bottom,left,right,backgroundColor,opacity,transform';
71 }
72 else if (value === 'none' || !value) {
73 return { isDeleted: true };
74 }
75 return {
76 transitionProperty: value.replace('background-color', 'backgroundColor'),
77 };
78};
79var transition = function (value) {
80 var result = {
81 isDeleted: true,
82 };
83 var options = (value || '')
84 .trim()
85 .replace(/cubic-bezier\(.*\)/g, function ($0) { return $0.replace(/\s+/g, ''); }) // transition: all 0.2s cubic-bezier( 0.42, 0, 0.58, 1 ) 0s
86 .split(/\s+/);
87 var property = transitionProperty(options[0] || 'all');
88 if (!property.isDeleted)
89 result.transitionProperty = property.transitionProperty;
90 result.transitionTimingFunction = (options[2] || 'ease').replace(/\s+/g, '');
91 result.transitionDuration = toMs(options[1]);
92 result.transitionDelay = toMs(options[3]);
93 return result;
94};
95exports.default = {
96 border: function (value) {
97 return border('border', value);
98 },
99 borderTop: function (value) {
100 return border('borderTop', value);
101 },
102 borderRight: function (value) {
103 return border('borderRight', value);
104 },
105 borderBottom: function (value) {
106 return border('borderBottom', value);
107 },
108 borderLeft: function (value) {
109 return border('borderLeft', value);
110 },
111 padding: function (value) {
112 return measure(value, 'padding');
113 },
114 margin: function (value) {
115 return measure(value, 'margin');
116 },
117 lineHeight: function (value) {
118 return {
119 lineHeight: value,
120 };
121 },
122 fontWeight: function (value) {
123 return {
124 // eslint-disable-next-line @iceworks/best-practices/recommend-polyfill
125 fontWeight: value.toString(),
126 };
127 },
128 transition: function (value) {
129 return transition(value);
130 },
131 transitionProperty: function (value) {
132 return transitionProperty(value);
133 },
134 transitionDuration: function (value) {
135 return {
136 transitionDuration: toMs(value),
137 };
138 },
139 transitionDelay: function (value) {
140 return {
141 transitionDelay: toMs(value),
142 };
143 },
144 transitionTimingFunction: function (value) {
145 return {
146 transitionTimingFunction: value.replace(/\s+/g, ''),
147 };
148 },
149};
150//# sourceMappingURL=particular.js.map
\No newline at end of file