Skip to content

Commit b4a9447

Browse files
add css style optimizer comparing logics
1 parent 3e2a185 commit b4a9447

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
///
2+
/// General duplicated style optimization.
3+
/// this is based on static rule, based on the names of the layer and styles.
4+
///
5+
6+
import type { ElementCssStyleData } from "@coli.codes/css";
7+
8+
/// 1. based on final built style
9+
/// 2. based on pre-build style comparison
10+
/// - suggesting the merged style name
11+
12+
interface MinimalStyleRepresenation {
13+
name: string;
14+
style: ElementCssStyleData;
15+
}
16+
17+
type NameMatcher =
18+
// /**
19+
// * provide a custom regex to compare two names
20+
// */
21+
// | "custom-regex";
22+
/**
23+
* match exactly
24+
*/
25+
| "exact"
26+
/**
27+
* allow matching with numbered suffix
28+
* e.g.
29+
* - 'Wrapper' = 'Wrapper1'
30+
* - 'Container1' = 'Container2'
31+
* - 'View' = 'View_001' = 'View_002'
32+
*/
33+
| "suffix-number"
34+
/**
35+
* allow matching with separator (.-_) with following numbered suffix
36+
* e.g.
37+
* - 'Wrapper' = 'Wrapper-1'
38+
* - 'Container' = 'Container.2'
39+
* - 'View' = 'View_001' = 'View_002'
40+
*/
41+
| "suffix-separator-number";
42+
43+
function is_matching_name(a: string, b: string, matcher: NameMatcher) {
44+
switch (matcher) {
45+
case "exact":
46+
return a === b;
47+
case "suffix-number": {
48+
// the suffix is optional.
49+
// yes: 'Wrapper' = 'Wrapper1' = 'Wrapper2' = 'Wrapper002'
50+
// no: 'Wrapper' !== 'Wrapper_001' !== 'Wrapper_A' !== 'Wrapper_A'
51+
52+
if (a.includes(b)) {
53+
const suffix = a.replace(b, "");
54+
return /(\d+)/.test(suffix);
55+
} else if (b.includes(a)) {
56+
const suffix = b.replace(a, "");
57+
return /(\d+)/.test(suffix);
58+
} else {
59+
return false;
60+
}
61+
}
62+
63+
case "suffix-separator-number":
64+
// allowed spearator is .. '.', '-', '_'
65+
// yes: 'Wrapper' = 'Wrapper-1' = 'Wrapper.2' = 'Wrapper_001'
66+
// no: 'Wrapper' !== 'Wrapper1' !== 'Wrapper2' !== 'Wrapper_A' !== 'Wrapper_A'
67+
68+
if (a.includes(b)) {
69+
const suffix = a.replace(b, "");
70+
return /^((\-|\.|\_)?\d+)$/.test(suffix);
71+
} else if (b.includes(a)) {
72+
const suffix = b.replace(a, "");
73+
return /^((\-|\.|\_)?\d+)$/.test(suffix);
74+
}
75+
return false;
76+
}
77+
}
78+
79+
/**
80+
* returns boolean based on input's name and style data. if both matches, return true.
81+
* @param a 1st element
82+
* @param b 2nd element
83+
* @param options
84+
* @returns
85+
*/
86+
export function is_duplicate_by_name_and_style(
87+
a: MinimalStyleRepresenation,
88+
b: MinimalStyleRepresenation,
89+
options: {
90+
name_match: NameMatcher;
91+
}
92+
) {
93+
// name should be the same
94+
if (!is_matching_name(a.name, b.name, options.name_match)) {
95+
return false;
96+
}
97+
98+
// style should be the same
99+
return JSON.stringify(a.style) === JSON.stringify(b.style);
100+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
///
2+
/// Text style declaration optimization
3+
/// Clear the duplicated text styles, merge it into one.
4+
///

packages/builder-web-styled-components/optimization/index.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)