-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathindex.ts
63 lines (53 loc) · 1.34 KB
/
index.ts
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
import { Axis, DimensionLength } from "@reflect-ui/core";
import { calc, operation } from "../calc";
import { px, vw, vh } from "../dimensions";
import { percent } from "../percent";
export function length(d: DimensionLength | string, a?: Axis) {
if (d === undefined) {
return;
}
if (typeof d === "number") {
return px(d);
}
if (typeof d === "string") {
// To handle cases such as "0%"
const extractNum = d.replace(/[^0-9]/, "");
if (parseInt(extractNum) === 0) {
return;
}
if (d === "match-screen-size") {
switch (a) {
case Axis.horizontal:
return vw(100);
case Axis.vertical:
return vh(100);
}
throw new Error("Invalid axis");
}
if (d.endsWith("px")) {
return px(parseFloat(d));
}
if (d.endsWith("vw")) {
return vw(parseFloat(d));
}
if (d.endsWith("vh")) {
return vh(parseFloat(d));
}
if (d.endsWith("%")) {
return percent(d as `${number}%`);
}
// this case, the d value is already processed by other css builders.
else {
return d;
}
}
if (d.type == "calc") {
return calc(d.operations, a);
}
if (d.type == "op") {
//@ts-ignore
return operation(d, a);
}
throw `no matching length type found. "${JSON.stringify(d)}" was givven`;
return px((d as any) as number);
}