Skip to content

Commit

Permalink
fix the hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
sstraatemans committed Aug 31, 2023
1 parent a23d8ef commit 70d1f3d
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions packages/apps/docs/src/utils/createSlug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ const createSlugHash = (str: string): string => {
// eslint-disable-next-line @typescript-eslint/naming-convention
str.split('').forEach((_char, idx) => {
const ch = str.charCodeAt(idx);
hash += ch;
hash = (hash << 5) - hash + ch;
// eslint-disable-next-line no-bitwise
hash = hash & hash;
});
return `h${hash}`;
};

export const createSlug = (str?: string): string => {
if (str === undefined) return '';

let normalizedSlug = str
const normalizedSlug = str
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/[^\w\-\s]+/g, '')
Expand All @@ -23,9 +25,5 @@ export const createSlug = (str?: string): string => {
.toLowerCase()
.replace(/^-+|-+$/g, '');

if (normalizedSlug === '') {
normalizedSlug = createSlugHash(str);
}

return normalizedSlug;
return `${normalizedSlug}${createSlugHash(str)}`;
};

0 comments on commit 70d1f3d

Please sign in to comment.