Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 调整className hash算法, 降低哈希碰撞概率(已经出现了) #856

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions packages/shineout-style/src/jss-style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@ export const setJssConfig = (newConfig: { generateId?: GenerateId }) => {
Object.assign(config, newConfig);
};

const hashCache: Record<string, string> = {};
const stringToHash = (str: string) => {
let hash = 0;
if (hashCache[str]) {
return hashCache[str];
}

let hash = BigInt(0);
if (str.length === 0) return '';
for (let i = 0; i < str.length; i += 1) {
const char = str.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash |= 0;
const char = BigInt(str.charCodeAt(i));
hash = (hash << BigInt(5)) - hash + char;
hash &= BigInt('0xFFFFFFFFFFFFFFFF'); // 限制在64位整数范围内
}
return 'c' + hash.toString(36);
const result = 'c' + hash.toString(36);
hashCache[str] = result;
return result;
};

const camelToDash = (str: string) => str.replace(/([A-Z])/g, '-$1').toLowerCase();
Expand Down
2 changes: 1 addition & 1 deletion packages/shineout/src/button/__example__/s-001-base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default () => {

return (
<div style={{ display: 'flex', gap: 24, flexWrap: 'wrap' }}>
<Button type='primary' style={buttonStyle} text>
<Button type='primary' style={buttonStyle}>
Primary
</Button>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`Button[Base] should render correctly 1`] = `
style="display: flex; gap: 24px; flex-wrap: wrap;"
>
<button
class="soui-button soui-button-primary soui-button-button soui-button-text"
class="soui-button soui-button-primary soui-button-button"
dir="ltr"
style="margin: 0px;"
type="button"
Expand Down
Loading