Skip to content

Commit

Permalink
refactor: all flattenToken need hashed
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Nov 14, 2024
1 parent eabc24e commit 2bfc425
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
10 changes: 4 additions & 6 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const flattenTokenCache = new WeakMap<any, string>();
/**
* Flatten token to string, this will auto cache the result when token not change
*/
export function flattenToken(token: any, hashed: boolean = false) {
export function flattenToken(token: any) {
let str = flattenTokenCache.get(token) || '';

if (!str) {
Expand All @@ -45,17 +45,15 @@ export function flattenToken(token: any, hashed: boolean = false) {
if (value instanceof Theme) {
str += value.id;
} else if (value && typeof value === 'object') {
str += flattenToken(value, hashed);
str += flattenToken(value);
} else {
str += value;
}
});

// https://github.com/ant-design/ant-design/issues/48386
// Should hash the string to avoid style tag name too long
if (hashed) {
str = hash(str);
}
str = hash(str);

// Put in cache
flattenTokenCache.set(token, str);
Expand All @@ -67,7 +65,7 @@ export function flattenToken(token: any, hashed: boolean = false) {
* Convert derivative token to key string
*/
export function token2key(token: any, salt: string): string {
return hash(`${salt}_${flattenToken(token, true)}`);
return hash(`${salt}_${flattenToken(token)}`);
}

const randomSelectorKey = `random-${Date.now()}-${Math.random()}`.replace(
Expand Down
2 changes: 1 addition & 1 deletion tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ describe('csssinjs', () => {
const { container } = render(<TokenShower />);

// src/util.tsx - token2key func
expect(container.textContent).toEqual('1cpx0di');
expect(container.textContent).toEqual('1fs647j');
});

it('hash', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/util.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('util', () => {
// Repeat call flattenToken
for (let i = 0; i < 10000; i += 1) {
const tokenStr = flattenToken(token);
expect(tokenStr).toEqual('a1');
expect(tokenStr).toEqual('d9a1z5');
}

expect(checkTimes).toEqual(1);
Expand Down

0 comments on commit 2bfc425

Please sign in to comment.