-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathutils.js
More file actions
92 lines (84 loc) · 2.22 KB
/
Copy pathutils.js
File metadata and controls
92 lines (84 loc) · 2.22 KB
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
import { css } from "./src/css.js";
import { IS_NON_DIMENSIONAL } from "./src/render.js";
import { isNumber, isObject } from "./src/utils.js";
const W = globalThis;
const COMPATIBILITY_LIST = [
["customElements", W],
["ShadowRoot", W],
["Map", W],
["append", document],
["prepend", document],
["Symbol", W],
["for", W.Symbol],
];
/**
* serialize a string
* @param {...any} args
* @returns {string}
*/
export const serialize = (...args) => args.filter((value) => value).join(" ");
/**
* check the features that Atomico leverages the browser
* @returns {string[]}
*/
export const checkIncompatibility = () =>
COMPATIBILITY_LIST
//@ts-ignore
.map(([check, ctx]) => (!ctx || !(check in ctx) ? check : 0))
.filter((check) => check);
/**
* @type {{[id:string]:string}}
*/
const PROPS = {};
/**
* @param {string} str
*/
const hyphenate = (str) =>
(PROPS[str] =
PROPS[str] ||
str
.replace(/([A-Z])/g, "-$1")
.toLowerCase()
.replace(/^ms-/, "-ms-"));
/**
* @param {object} obj
* @returns {string}
*/
const stringify = (obj) =>
Object.entries(obj)
.map(([key, value]) => {
if (isObject(value)) {
return `${key}{${stringify(value)};}`.replace(/;;/g, ";");
}
if (key[0] === "-") {
return `${key}:${value}`;
}
return `${hyphenate(key)}:${
!isNumber(value) || IS_NON_DIMENSIONAL.test(key)
? value
: `${value}px`
}`;
})
.join(";")
.replace(/};/g, "}");
/**
* Create a Style from an object
* @param {{[key:string]:import("csstype").Properties<string | number>}|string} obj
* @returns {import("./types/css.js").Sheet | undefined}
*/
export function toCss(obj) {
if (typeof obj === "string") {
if (obj.match(/^https?:\/\//)) {
const request = new XMLHttpRequest();
request.open("get", obj, false);
request.send();
if (request.status === 200)
return css`
${request.responseText}
`;
}
}
return css`
${stringify(obj)}
`;
}