Skip to content

Commit 3fb462d

Browse files
committed
createParseColorDecisionName
1 parent cb9e5b6 commit 3fb462d

File tree

4 files changed

+105
-44
lines changed

4 files changed

+105
-44
lines changed

src/bin/css_to_ts/colorDecisions.ts

Lines changed: 91 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,21 @@
11
import type { State } from "./colorOptions";
2-
3-
//export type Context= "background" | "border" | "text" | "illustration";
4-
5-
//export type Usage = "action" |
6-
//"active" | "disabled" | "error" | "success" |
7-
//"default" | "title" | "label" | "contrast";
8-
9-
//export type Usage = "default" | "alt";
10-
11-
//Not in path
12-
//export type Variant = "high" | "low";
2+
import { states } from "./colorOptions";
3+
import { id } from "tsafe/id";
4+
import { assert } from "tsafe/assert";
5+
import { capitalize } from "tsafe/capitalize";
136

147
const contexts = ["background", "text", "border", "artwork"] as const;
158

169
type Context = typeof contexts[number];
1710

18-
/*
19-
const usages = [
20-
"default",
21-
"alt",
22-
"contrast",
23-
"flat",
24-
"action",
25-
"active",
26-
"open",
27-
"disabled",
28-
"raised",
29-
"altRaised",
30-
"altOverlap",
31-
"contrastRaised"
32-
] as const;
33-
*/
34-
35-
//type Usage = typeof usages[number];
11+
const variants = ["hight", "low"] as const;
12+
13+
type Variant = typeof variants[number];
3614

3715
export type ParsedColorDecisionName = {
3816
context: Context;
39-
usage: string; //Usage In reality use string because it's susceptible to change.
40-
variant: "high" | "low" | undefined;
17+
usage: string; //default alt contrast altOverlap contrastRaised
18+
variant: Variant | undefined;
4119
colorName: string; // "grey" "blueFrance"
4220
state: State | undefined;
4321
};
@@ -51,24 +29,96 @@ export function createParseColorDecisionName(params: {
5129
}) {
5230
const { colorNames } = params;
5331

54-
console.log(colorNames);
55-
56-
function parseColorDecisionName(decisionName: `--${string}`): ParsedColorDecisionName {
57-
console.log(decisionName);
58-
59-
//const parsedColorDecisionName: ParsedColorDecisionName = { } as any;
60-
61-
//let revArr = colorOptionName.replace(/^--/, "").split("-").reverse();
62-
32+
function parseColorDecisionName(colorDecisionName: `--${string}`): ParsedColorDecisionName {
6333
/*
34+
colorDecisionName:
6435
--background-default-grey-hover
6536
--background-default-grey
6637
--border-action-low-orange-terre-battue
6738
--background-alt-raised-grey-hover
6839
--background-contrast-overlap-grey
6940
*/
7041

71-
return null as any;
42+
const parsedColorDecisionName: ParsedColorDecisionName = {} as any;
43+
44+
let arr = colorDecisionName.replace(/^--/, "").split("-");
45+
46+
/*
47+
arr:
48+
[ "background", "default", "grey", "hover" ]
49+
[ "background", "default", "grey" ]
50+
[ "border", "action-low", "orange", "terre", "battue"]
51+
[ "background", "alt", "raised", "grey", "hover" ]
52+
[ "background", "contrast-overlap", "grey" ]
53+
*/
54+
55+
//parse context
56+
{
57+
const [firstWord, ...rest] = arr;
58+
59+
arr = rest;
60+
61+
assert(id<readonly string[]>(contexts).includes(firstWord));
62+
63+
parsedColorDecisionName.context = firstWord as any;
64+
}
65+
66+
parse_state: {
67+
const [lastWord, ...rest] = [...arr].reverse();
68+
rest.reverse();
69+
70+
if (!id<readonly string[]>(states).includes(lastWord)) {
71+
parsedColorDecisionName.variant = undefined;
72+
break parse_state;
73+
}
74+
75+
arr = rest;
76+
77+
parsedColorDecisionName.state = lastWord as any;
78+
}
79+
80+
parse_colorName: {
81+
for (const colorName of colorNames) {
82+
const kebabCaseColorName = colorName.replace(
83+
/([A-Z])/g,
84+
group => `-${group.toLowerCase()}`
85+
);
86+
87+
const join = arr.join("-");
88+
89+
if (!join.endsWith(kebabCaseColorName)) {
90+
continue;
91+
}
92+
93+
arr = join.split("-" + kebabCaseColorName)[0].split("-");
94+
95+
parsedColorDecisionName.colorName = colorName;
96+
97+
break parse_colorName;
98+
}
99+
100+
assert(false);
101+
}
102+
103+
parse_variant: {
104+
const [lastWord, ...rest] = [...arr].reverse();
105+
rest.reverse();
106+
107+
if (!id<readonly string[]>(variants).includes(lastWord)) {
108+
parsedColorDecisionName.variant = undefined;
109+
break parse_variant;
110+
}
111+
112+
arr = rest;
113+
114+
parsedColorDecisionName.variant = lastWord as any;
115+
}
116+
117+
parsedColorDecisionName.usage = arr
118+
.map((word, i) => (i === 0 ? word : capitalize(word)))
119+
.join("");
120+
121+
return parsedColorDecisionName;
72122
}
73123

74124
return { parseColorDecisionName };

src/bin/css_to_ts/colorOptions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export const data_fr_theme = "data-fr-theme";
1313

1414
export type Variant = "main" | "sun" | "moon";
1515

16-
export type State = "hover" | "active";
16+
export const states = ["hover", "active"] as const;
17+
export type State = typeof states[number];
1718

1819
export type BrightnessIndex = {
1920
value: number;

src/test/bin/colorDecisions/parseColorDecisionName.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const { parseColorDecisionName } = createParseColorDecisionName({ colorNames });
2121
const got = parseColorDecisionName("--background-default-grey-hover");
2222

2323
assert(same(got, expected));
24+
25+
console.log("PASS 1");
2426
}
2527

2628
{
@@ -35,20 +37,24 @@ const { parseColorDecisionName } = createParseColorDecisionName({ colorNames });
3537
const got = parseColorDecisionName("--background-default-grey");
3638

3739
assert(same(got, expected));
40+
41+
console.log("PASS 2");
3842
}
3943

4044
{
4145
const expected: ParsedColorDecisionName = {
4246
"context": "border",
4347
"usage": "action",
4448
"variant": "low",
45-
"colorName": "orange-terre-battue",
49+
"colorName": "orangeTerreBattue",
4650
"state": undefined
4751
};
4852

4953
const got = parseColorDecisionName("--border-action-low-orange-terre-battue");
5054

5155
assert(same(got, expected));
56+
57+
console.log("PASS 3");
5258
}
5359

5460
{
@@ -63,6 +69,8 @@ const { parseColorDecisionName } = createParseColorDecisionName({ colorNames });
6369
const got = parseColorDecisionName("--background-alt-raised-grey-hover");
6470

6571
assert(same(got, expected));
72+
73+
console.log("PASS 4");
6674
}
6775

6876
{
@@ -77,4 +85,6 @@ const { parseColorDecisionName } = createParseColorDecisionName({ colorNames });
7785
const got = parseColorDecisionName("--background-contrast-overlap-grey");
7886

7987
assert(same(got, expected));
88+
89+
console.log("PASS 5");
8090
}

src/test/bin/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
import "./colorOptions";
2-
//import "./colorDecisions";
2+
import "./colorDecisions";

0 commit comments

Comments
 (0)