Skip to content

Commit 45ebd3b

Browse files
committed
Updated logic to capture category names
1 parent 0aaa714 commit 45ebd3b

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

src/__tests__/utils/tokens.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ jest.mock('@patternfly/react-tokens', () => ({
2525
},
2626
// eslint-disable-next-line camelcase
2727
t_global_color_200: {
28-
name: '--pf-v6-t-global--color--200',
28+
name: '--pf-t-global--color--200',
2929
value: '#e0e0e0',
30-
var: 'var(--pf-v6-t-global--color--200)',
30+
var: 'var(--pf-t-global--color--200)',
3131
},
3232
// eslint-disable-next-line camelcase
3333
chart_global_Fill: {
@@ -149,7 +149,8 @@ describe('getTokensByCategory', () => {
149149

150150
Object.entries(byCategory).forEach(([category, tokens]) => {
151151
tokens.forEach((token) => {
152-
const prefix = token.name.split('-')[4] // --pf-v6-{prefix}-...
152+
const isVersionedToken = /^--pf-v6/.test(token.name)
153+
const prefix = token.name.split('-')[isVersionedToken ? 4 : 3] // --pf-v6-{prefix}- if versioned or --pf-{prefix}- if unversioned
153154
expect(prefix).toBe(category)
154155
})
155156
})

src/utils/tokens.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,20 @@ let cachedCategories: string[] | null = null
1515
let cachedTokensByCategory: TokensByCategory | null = null
1616

1717
function getCategoryFromTokenName(tokenName: string): string {
18-
// CSS variable format: --pf-v6-{category}-...
19-
// Split by hyphen and get the category part (index 4 after splitting)
20-
const parts = tokenName.split('-')
21-
if (parts.length >= 5) {
22-
// Handle multi-word categories like 'chart'
23-
// Find where the component name starts (after category)
24-
// Categories can be: c, t, l, chart, global, hidden, patternfly
25-
const category = parts[4]
26-
27-
// Check if this might be a multi-word category
28-
// For patterns like: --pf-v6-chart-global-...
29-
// we want to check if parts[4] + parts[5] forms a known longer category
30-
if (parts.length >= 6 && parts[4] === 'chart' && parts[5] !== '') {
31-
// For chart tokens, the category is just 'chart'
32-
return 'chart'
33-
}
34-
35-
return category
18+
const nameWithoutPfPrefix = tokenName.replace(/^--pf-/, '')
19+
const parts = nameWithoutPfPrefix.split(/-+/)
20+
if (/^v\d+/.test(parts[0])) {
21+
return parts[1]
3622
}
3723

38-
return tokenName
24+
return parts[0]
25+
// --pf-[t|vX]-
26+
// add test for nonversion token
27+
// strip out leading --pf-
28+
// check if 1st thing is version, strip out version and grab first part after, v6-chart-global
29+
// if not version, put that thing in category
30+
// CSS variable format: --pf-v6-{category}-...
31+
// Split by hyphen and get the category part (index 4 after splitting)
3932
}
4033

4134
export function getAllTokens(): Token[] {

0 commit comments

Comments
 (0)