forked from okikio/native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.js
463 lines (402 loc) · 15.6 KB
/
repl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
let counter = 0;
export const addCSSUnit = (unit = "") => {
return (value) => {
counter++;
return typeof value == "string" ? value : `${value}${unit}`
};
};
export const UnitLess = addCSSUnit();
export const UnitPX = addCSSUnit("px");
export const UnitDEG = addCSSUnit("deg");
export const toArr = (input) => {
if (Array.isArray(input) || typeof input == "string") {
if (typeof input == "string") input = input.split(",");
return input;
}
return [input];
};
export const CSSValue = (unit) => {
return (input) => {
return isValid(input) ? toArr(input).map(val => {
if (typeof val != "number" && typeof val != "string")
return val;
// Basically if you can convert it to a number try to,
// otherwise just return whatever you can
let num = Number(val);
let value = Number.isNaN(num) ? (typeof val == "string" ? val.trim() : val) : num;
return unit(value); // Add the default units
}) : [];
};
}
export const isValid = (value) => {
return Boolean(Array.isArray(value) || typeof value == "string" ? value.length : value != null && value != undefined);
}
export const transpose = (...arrays) => {
let largestArrLen = 0;
arrays = arrays.map((arr, i) => {
// Convert all values in arrays to an array
// This ensures that `arrays` is an array of arrays
let result = toArr(arr);
// Finds the largest array
let len = result.length;
if (len > largestArrLen) largestArrLen = len;
return result;
});
// Flip the rows and columns of arrays
let result = [];
let len = arrays.length;
for (let col = 0; col < largestArrLen; col++) {
result[col] = [];
for (let row = 0; row < len; row++) {
let val = arrays[row][col];
if (isValid(val)) result[col][row] = val;
}
}
return result;
}
export const CSSArrValue = (arr, unit) => {
// This is for the full varients of the transform function as well as the 3d varients
// zipping the `CSSValue` means if a user enters a string, it will treat each value in that
// string as a seperate transition state
return toArr(arr).map(CSSValue(unit));
}
// export const TransformFunctionNames = [
// "translate",
// "translate3d",
// "translateX",
// "translateY",
// "translateZ",
// "rotate",
// "rotate3d",
// "rotateX",
// "rotateY",
// "rotateZ",
// "scale",
// "scale3d",
// "scaleX",
// "scaleY",
// "scaleZ",
// "skew",
// "skewX",
// "skewY",
// "perspective"
// ];
export const UnitLessCSSValue = CSSValue(UnitLess);
export const UnitPXCSSValue = CSSValue(UnitPX);
export const UnitDEGCSSValue = CSSValue(UnitDEG);
/** Convert a dash-separated string into camelCase strings */
export const camelCase = (str) => {
if (str.includes("--")) return str;
let result = `${str}`.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
return result;
}
/** Convert a camelCase string to a dash-separated string {@link camelCase} */
export const convertToDash = (str) => {
str = str.replace(/([A-Z])/g, letter => `-${letter.toLowerCase()}`);
// Remove first dash
return ( str.charAt( 0 ) == '-' ) ? str.substr(1) : str;
}
/**
* Acts like array.map(...) but for functions
*/
export const mapObject = (obj, fn) => {
let keys = Object.keys(obj);
let key, value, result = {};
for (let i = 0, len = keys.length; i < len; i++) {
key = keys[i];
value = obj[key];
result[key] = fn(value, key, obj);
}
return result;
};
/**
* Remove dashs from CSS properties & maps the values to camelCase keys
*/
export const ParseCSSProperties = (obj) => {
let keys = Object.keys(obj);
let key, value, result = {};
for (let i = 0, len = keys.length; i < len; i++) {
key = camelCase(keys[i]);
value = obj[keys[i]];
result[key] = value;
}
return result;
}
/** Common CSS Property names with the units "px" as an acceptable value */
export const CSSPXDataType = ["margin", "padding", "size", "width", "height", "left", "right", "top", "bottom", "radius", "gap", "basis", "inset", "outline-offset", "perspective", "thickness", "position", "distance", "spacing"].map(camelCase).join("|");
/** Convert the {@link CSSPXDataType} array, to a Regular Expression */
export const RegExpCSSPXDataType = new RegExp(CSSPXDataType, "gi");
/**
* Return a copy of the object without the keys specified in the keys argument
*
* @param keys - arrays of keys to remove from the object
* @param obj - the object in question
* @returns
* a copy of the object without certain key
*/
export const omit = (keys, obj) => {
let arr = [...keys];
let rest = { ...obj };
while (arr.length) {
let { [arr.pop()]: omitted, ...remaining } = rest;
rest = remaining;
}
return rest;
}
export const TransformFunctions = {
"translate": value => CSSArrValue(value, UnitPX),
"translate3d": value => CSSArrValue(value, UnitPX),
"translateX": value => UnitPXCSSValue(value),
"translateY": value => UnitPXCSSValue(value),
"translateZ": value => UnitPXCSSValue(value),
"rotate": value => CSSArrValue(value, UnitDEG),
"rotate3d": value => CSSArrValue(value, UnitLess),
"rotateX": value => UnitDEGCSSValue(value),
"rotateY": value => UnitDEGCSSValue(value),
"rotateZ": value => UnitDEGCSSValue(value),
"scale": value => CSSArrValue(value, UnitLess),
"scale3d": value => CSSArrValue(value, UnitLess),
"scaleX": value => UnitLessCSSValue(value),
"scaleY": value => UnitLessCSSValue(value),
"scaleZ": value => UnitLessCSSValue(value),
"skew": value => CSSArrValue(value, UnitDEG),
"skewX": value => UnitDEGCSSValue(value),
"skewY": value => UnitDEGCSSValue(value),
"perspective": value => UnitPXCSSValue(value),
};
/**
* Creates the transform property text
*/
export const createTransformProperty = (transformFnNames, arr) => {
let result = "";
let len = transformFnNames.length;
for (let i = 0; i < len; i++) {
let name = transformFnNames[i];
let value = arr[i];
if (isValid(value))
result += `${name}(${Array.isArray(value) ? value.join(", ") : value}) `;
}
return result.trim();
}
export const TransformFunctionNames = Object.keys(TransformFunctions);
/** Converts values to strings */
export const toStr = (input) => `` + input;
export const ParseTransformableCSSProperties = (properties) => {
// Convert dash seperated strings to camelCase strings
let AllCSSProperties = ParseCSSProperties(properties);
let rest = omit(TransformFunctionNames, AllCSSProperties);
// Adds support for ordered transforms
let transformFunctionNames = Object.keys(AllCSSProperties)
.filter(key => TransformFunctionNames.includes(key));
let transformFunctionValues = transformFunctionNames.map((key) => TransformFunctions[key](AllCSSProperties[key]));
let transform = transpose(...transformFunctionValues)
.filter(isValid)
.map(arr => createTransformProperty(transformFunctionNames, arr));
// Wrap non array CSS property values in an array
rest = mapObject(rest, (value, key) => {
let unit;
// If key doesn't have the word color in it, try to add the default "px" or "deg" to it
if (!/color/i.test(key)) {
let isAngle = /rotate/i.test(key);
let isLength = new RegExp(CSSPXDataType, "i").test(key);
// There is an intresting bug that occurs if you test a string againt the same instance of a Regular Expression
// where the answer will be different every test
// so, to avoid this bug, I create a new instance every time
if (isAngle || isLength) {
// If the key has rotate in it's name use "deg" as a default unit
if (isAngle) unit = UnitDEGCSSValue;
// If it has a common CSS Property name with the units "px" as an acceptable value
// try to add "px" as the default unit
else if (isLength) unit = UnitPXCSSValue;
return unit(value).map(str => unit(str.trim().split(" ")).join(" "));
}
}
return [].concat(value).map(toStr);
});
return Object.assign({},
isValid(transform) ? { transform } : null,
rest);
}
let x = ParseTransformableCSSProperties({
// It will automatically add the "px" units for you, or you can write a string with the units you want
translateY: ["20, 90", "100", 5], // Note: this will actually result in an error, make sure to pay attention to where you are putting strings and commas
translate: "25, 35, 60%",
translate3d: ["0, 5", 0],
// translateX: -1,
// translateZ: 0,
// perspective: 0,
// opacity: 0,
// color: 0,
// scale: [
// [1, "2"],
// ["2", 1]
// ],
rotateC: "5,6",
// "offset-size": 5,
inset: "5px",
"outline-offset":90,
"offset-rotate": "10, 20",
margin: "5 5"
// rotate3d: [
// [1, 2, 5, "3deg"], // The last value in the array must be a string with units for rotate3d
// [2, "4", 6, "45turn"],
// ["2", "4", "6", "-1rad"]
// ]
})
// console.log(x) // x,
// console.log(ParseTransformableCSSProperties({
// // It will automatically add the "px" units for you, or you can write a string with the units you want
// translate3d: [
// "25, 35, 60%",
// [50, "60px", 70],
// ["70", 50]
// ],
// translate: "25, 35, 60%",
// translateX: [50, "60px", "70"],
// translateY: ["50, 60", "60"], // Note: this will actually result in an error, make sure to pay attention to where you are putting strings and commas
// translateZ: 0,
// perspective: 0,
// opacity: "0, 5",
// scale: [
// [1, "2"],
// ["2", 1]
// ],
// // rotate3d: [
// // [1, 2, 5, "3deg"], // The last value in the array must be a string with units for rotate3d
// // [2, "4", 6, "45turn"],
// // ["2", "4", "6", "-1rad"]
// // ]
// }))
// console.log(CSSArrValue(["6", 5, 5], UnitPX))
// console.log(CSSValue(UnitPX)(["70px", 50]));
export const ParseTransformableCSSKeyframes = (keyframes) => {
return keyframes.map(properties => {
let {
translate,
translate3d,
translateX,
translateY,
translateZ,
rotate,
rotate3d,
rotateX,
rotateY,
rotateZ,
scale,
scale3d,
scaleX,
scaleY,
scaleZ,
skew,
skewX,
skewY,
perspective,
easing,
iterations,
offset,
...rest
// Convert dash seperated strings to camelCase strings
} = ParseCSSProperties(properties);
translate = UnitPXCSSValue(translate);
translate3d = UnitPXCSSValue(translate3d);
translateX = UnitPXCSSValue(translateX)[0];
translateY = UnitPXCSSValue(translateY)[0];
translateZ = UnitPXCSSValue(translateZ)[0];
rotate = UnitDEGCSSValue(rotate);
rotate3d = UnitLessCSSValue(rotate3d);
rotateX = UnitDEGCSSValue(rotateX)[0];
rotateY = UnitDEGCSSValue(rotateY)[0];
rotateZ = UnitDEGCSSValue(rotateZ)[0];
scale = UnitLessCSSValue(scale);
scale3d = UnitLessCSSValue(scale3d);
scaleX = UnitLessCSSValue(scaleX)[0];
scaleY = UnitLessCSSValue(scaleY)[0];
scaleZ = UnitLessCSSValue(scaleZ)[0];
skew = UnitDEGCSSValue(skew);
skewX = UnitDEGCSSValue(skewX)[0];
skewY = UnitDEGCSSValue(skewY)[0];
perspective = UnitPXCSSValue(perspective)[0];
return [
rest,
translate, translate3d, translateX, translateY, translateZ,
rotate, rotate3d, rotateX, rotateY, rotateZ,
scale, scale3d, scaleX, scaleY, scaleZ,
skew, skewX, skewY,
perspective
];
}).map(([rest, ...transformFunctions]) => {
let transform = createTransformProperty(TransformFunctionNames, transformFunctions);
let unit;
// Wrap non array CSS property values in an array
rest = mapObject(rest, (value, key) => {
// If key doesn't have the word color in it, try to add the default "px" or "deg" to it
if (!/color/i.test(key)) {
let isAngle = /rotate/i.test(key);
let isLength = new RegExp(CSSPXDataType, "i").test(key);
// There is an intresting bug that occurs if you test a string againt the same instance of a Regular Expression
// where the answer will be different every test
// so, to avoid this bug, I create a new instance every time
if (isAngle || isLength) {
// If the key has rotate in it's name use "deg" as a default unit
if (isAngle) unit = UnitDEGCSSValue;
// If it has a common CSS Property name with the units "px" as an acceptable value
// try to add "px" as the default unit
else if (isLength) unit = UnitPXCSSValue;
// To support multi-value CSS properties like "margin", "padding", and "inset"
// split the value into an array using spaces as the seperator
// then apply the valid default units and join them back with spaces
// seperating them
let arr = toStr(value).trim().split(" ");
return unit(arr).join(" ");
}
}
return toStr(value);
});
return Object.assign({},
isValid(transform) ? { transform } : null,
rest);
});
}
console.log(ParseTransformableCSSKeyframes([
{
// It will automatically add the "px" units for you, or you can write a string with the units you want
translate3d: "25, 35, 60%",
translate: "25, 35, 60%",
translateX: 10,
translateY: ["50, 60", "60"], // Note: this will actually result in an error, make sure to pay attention to where you are putting strings and commas
translateZ: 0,
perspective: 0,
opacity: "0, 5",
scale: [1, "2"],
// rotate3d: [
// [1, 2, 5, "3deg"], // The last value in the array must be a string with units for rotate3d
// [2, "4", 6, "45turn"],
// ["2", "4", "6", "-1rad"]
// ]
},
{
// It will automatically add the "px" units for you, or you can write a string with the units you want
// translate3d: "55, 15, 20%",
// translate: "20, 5, 6%",
// translateX: [0, "60px", "70"],
translateX: "5deg",
// translateY: ["0, 0", "0"], // Note: this will actually result in an error, make sure to pay attention to where you are putting strings and commas
// translateZ: 6,
// perspective: 500,
opacity: "5",
margin: "25 65"
// scale: "2",
// rotate3d: [
// [1, 2, 5, "3deg"], // The last value in the array must be a string with units for rotate3d
// [2, "4", 6, "45turn"],
// ["2", "4", "6", "-1rad"]
// ]
},
]))
// console.log(CSSArrValue(["6", 5, 5], UnitPX))
// console.log(CSSValue(UnitPX)(["70px", 50]));
// let arr = [5, 6, 7, 8, 9, 10, ...Array(1000).fill("")];
// console.time("Test");
// const mySet = new Set([1,2,3,4]);
// [...mySet].reduce()
// console.timeEnd("Test");