11import 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
147const contexts = [ "background" , "text" , "border" , "artwork" ] as const ;
158
169type 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
3715export 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 } ;
0 commit comments