@wise/art 0.0.0-experimental-6054253 → 0.0.0-experimental-e5fd55b

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wise/art",
3
- "version": "0.0.0-experimental-6054253",
3
+ "version": "0.0.0-experimental-e5fd55b",
4
4
  "license": "MIT",
5
5
  "description": "React library for art elements in UI",
6
6
  "homepage": "https://github.com/transferwise/web-art#readme",
@@ -11,43 +11,66 @@ export default meta;
11
11
 
12
12
  type Story = StoryObj<typeof Flag>;
13
13
 
14
- type GroupedFlag = { country: string[]; currencies: string[][] };
15
-
16
- const groupFlags = (flags: { country: string[]; currency: string[] }[]): GroupedFlag[] =>
17
- flags.reduce<GroupedFlag[]>((acc, flag) => {
18
- const existing = acc.find(
19
- (currentFlag) => currentFlag.country[0] && currentFlag.country[0] === flag.country[0],
20
- );
21
- if (existing) {
22
- existing.currencies.push(flag.currency);
23
- } else {
24
- acc.push({ country: flag.country, currencies: [flag.currency] });
14
+ interface GroupedFlag { name: string; countryCode?: string; currencyCodes: string[] }
15
+
16
+ const toTitleCase = (str: string) =>
17
+ str.replace(/\b\w/gu, (char) => char.toUpperCase());
18
+
19
+ const deriveName = (slug: string): string => {
20
+ if (!slug.includes('-')) return toTitleCase(slug);
21
+ const nameSlug = slug.slice(slug.indexOf('-') + 1);
22
+ return toTitleCase(nameSlug.replace(/-/gu, ' '));
23
+ };
24
+
25
+ const groupByCountry = (
26
+ entries: [string, (typeof flagRawMetaData)[keyof typeof flagRawMetaData]][],
27
+ ): GroupedFlag[] => {
28
+ const countryMap = new Map<string, GroupedFlag>();
29
+ const standaloneFlags: GroupedFlag[] = [];
30
+
31
+ for (const [slug, data] of entries) {
32
+ const isDetailed = 'detailed' in data && data.detailed;
33
+ if (!isDetailed) {
34
+ const iso2 = 'iso2' in data ? data.iso2 : undefined;
35
+ const currencyCode = 'currencyCode' in data ? data.currencyCode : undefined;
36
+
37
+ if (iso2) {
38
+ const existing = countryMap.get(iso2);
39
+ if (existing) {
40
+ if (currencyCode) existing.currencyCodes.push(currencyCode);
41
+ } else {
42
+ countryMap.set(iso2, {
43
+ name: deriveName(slug),
44
+ countryCode: iso2,
45
+ currencyCodes: currencyCode ? [currencyCode] : [],
46
+ });
47
+ }
48
+ } else {
49
+ standaloneFlags.push({
50
+ name: deriveName(slug),
51
+ currencyCodes: currencyCode ? [currencyCode] : [],
52
+ });
53
+ }
25
54
  }
26
- return acc;
27
- }, []);
28
-
29
- const allFlagsSorted = groupFlags(
30
- Object.values(flagRawMetaData)
31
- .flat()
32
- .sort((a, b) => {
33
- const nameA = (a.country.at(-1) ?? a.currency?.at(-1) ?? '').toLowerCase();
34
- const nameB = (b.country.at(-1) ?? b.currency?.at(-1) ?? '').toLowerCase();
35
- return nameA.localeCompare(nameB);
36
- }),
55
+ }
56
+
57
+ return [...countryMap.values(), ...standaloneFlags];
58
+ };
59
+
60
+ const allEntries = Object.entries(flagRawMetaData);
61
+ const allFlagsSorted = groupByCountry(allEntries).sort((a, b) =>
62
+ a.name.localeCompare(b.name),
37
63
  );
38
64
 
65
+ const getFlagsByRegion = (region: string): GroupedFlag[] =>
66
+ groupByCountry(allEntries.filter(([, data]) => data.group === region));
67
+
39
68
  // Helper function to render all flags and sizes for a given region
40
69
  const AllFlagsAndSizes = (
41
70
  region: string,
42
71
  flagsOverride?: GroupedFlag[],
43
72
  ) => {
44
- const regionFlags =
45
- flagsOverride ??
46
- groupFlags(
47
- Object.entries(flagRawMetaData)
48
- .filter(([currentRegion]) => currentRegion === region)
49
- .flatMap(([, flags]) => flags),
50
- );
73
+ const regionFlags = flagsOverride ?? getFlagsByRegion(region);
51
74
 
52
75
  return (
53
76
  <>
@@ -71,19 +94,11 @@ const AllFlagsAndSizes = (
71
94
  }}
72
95
  >
73
96
  {regionFlags.map((flag) => {
74
- const countryCode = flag.country[0]?.toUpperCase();
75
- const primaryCurrencyCode = flag.currencies[0]?.[0]?.toUpperCase();
76
- const flagCode = countryCode ?? primaryCurrencyCode;
77
- const currencyCodes = flag.currencies.map((currency) => currency[0]).filter(Boolean).map((code) => code.toUpperCase());
78
-
79
- // Covers cases where there's just a currency code and no country (e.g. Euro)
80
- const flagName = countryCode
81
- ? flag.country.at(-1)
82
- : flag.currencies[0]?.at(-1);
97
+ const flagCode = flag.countryCode ?? flag.currencyCodes[0];
83
98
 
84
99
  return (
85
100
  <div
86
- key={flagName}
101
+ key={flag.name}
87
102
  style={{
88
103
  display: 'flex',
89
104
  flexDirection: 'column',
@@ -111,19 +126,19 @@ const AllFlagsAndSizes = (
111
126
  alignItems: 'flex-start',
112
127
  }}
113
128
  >
114
- <strong>{flagName}</strong>
129
+ <strong>{flag.name}</strong>
115
130
  <span>
116
- {countryCode && (
131
+ {flag.countryCode && (
117
132
  <>
118
- Country: <code>{countryCode}</code>
133
+ Country: <code>{flag.countryCode}</code>
119
134
  </>
120
135
  )}
121
- {currencyCodes.length > 0 && (
136
+ {flag.currencyCodes.length > 0 && (
122
137
  <>
123
138
  <br />
124
139
  <span>
125
- {`${currencyCodes.length === 1 ? 'Currency' : 'Currencies'}: `}
126
- {currencyCodes.map((currencyCode) => (
140
+ {`${flag.currencyCodes.length === 1 ? 'Currency' : 'Currencies'}: `}
141
+ {flag.currencyCodes.map((currencyCode) => (
127
142
  <code key={currencyCode}>{currencyCode}</code>
128
143
  ))}
129
144
  </span>