From 2bfc4257e5970b2a795c18b8801001c7ed17ab0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Thu, 14 Nov 2024 16:58:34 +0800 Subject: [PATCH] refactor: all flattenToken need hashed --- src/util/index.ts | 10 ++++------ tests/index.spec.tsx | 2 +- tests/util.spec.tsx | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/util/index.ts b/src/util/index.ts index 95bf26d..f137fff 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -35,7 +35,7 @@ const flattenTokenCache = new WeakMap(); /** * 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) { @@ -45,7 +45,7 @@ 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; } @@ -53,9 +53,7 @@ export function flattenToken(token: any, hashed: boolean = false) { // 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); @@ -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( diff --git a/tests/index.spec.tsx b/tests/index.spec.tsx index 028b882..22930e6 100644 --- a/tests/index.spec.tsx +++ b/tests/index.spec.tsx @@ -202,7 +202,7 @@ describe('csssinjs', () => { const { container } = render(); // src/util.tsx - token2key func - expect(container.textContent).toEqual('1cpx0di'); + expect(container.textContent).toEqual('1fs647j'); }); it('hash', () => { diff --git a/tests/util.spec.tsx b/tests/util.spec.tsx index d9faffc..57879ae 100644 --- a/tests/util.spec.tsx +++ b/tests/util.spec.tsx @@ -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);