From a84efe1c786de201d0af4bb1d44b275095f5a4c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20de=20la=20Martini=C3=A8re?= Date: Sun, 28 Apr 2024 14:45:24 +0200 Subject: [PATCH] Use node-diacritics to remove accents --- package.json | 4 ++++ src/slugify.ts | 45 ++++++++++++++------------------------------- yarn.lock | 10 ++++++++++ 3 files changed, 28 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 91d750e..0f643a2 100644 --- a/package.json +++ b/package.json @@ -31,9 +31,13 @@ }, "homepage": "https://github.com/martpie/react-slugify#readme", "devDependencies": { + "@types/diacritics": "^1.3.3", "@types/react": "^18.3.1", "react": "^18.3.1", "typescript": "^5.4.5", "vitest": "^1.5.2" + }, + "dependencies": { + "diacritics": "^1.3.0" } } diff --git a/src/slugify.ts b/src/slugify.ts index 90d7bb1..67d4664 100644 --- a/src/slugify.ts +++ b/src/slugify.ts @@ -1,39 +1,24 @@ -import * as React from "react"; +import type { ReactNode } from "react"; +import { remove as stripAccents} from 'diacritics'; -/** - * Remove all accentuated characters from a string - */ -const stripAccents = (input: string): string => { - const accents = - "ÀÁÂÃÄÅĄĀàáâãäåąāÒÓÔÕÕÖØòóôõöøÈÉÊËĘĒèéêëðęēÇĆČçćčÐÌÍÎÏĪìíîïīÙÚÛÜŪùúûüūÑŅñņŠŚšśŸÿýŽŹŻžźżŁĻłļŃŅńņàáãảạăằắẳẵặâầấẩẫậèéẻẽẹêềếểễệđùúủũụưừứửữựòóỏõọôồốổỗộơờớởỡợìíỉĩịäëïîüûñçýỳỹỵỷğışĞİŞĢģĶķ"; - const fixes = - "AAAAAAAAaaaaaaaaOOOOOOOooooooEEEEEEeeeeeeeCCCcccDIIIIIiiiiiUUUUUuuuuuNNnnSSssYyyZZZzzzLLllNNnnaaaaaaaaaaaaaaaaaeeeeeeeeeeeduuuuuuuuuuuoooooooooooooooooiiiiiaeiiuuncyyyyygisGISGgKk"; - const split = accents.split("").join("|"); - const reg = new RegExp(`(${split})`, "g"); - - function replacement(a: string) { - return fixes[accents.indexOf(a)] || ""; - } - - return input.replace(reg, replacement); -}; - -const getSafeRegexpString = (input: string): string => - input +function getSafeRegexpString(input: string): string { + return input .split("") .map((char) => `\\${char}`) .join(""); +} /** - * Harmonize a string by removing spaces, non-alphabetical caracters and by + * Format a string by removing spaces, non-alphabetical caracters and by * adding delimiter */ -const harmonize = ( +function format( input: string, delimiter: string, ignoreInvalid = false -): string => { +): string { const harmonized = stripAccents(input).trim().toLowerCase(); + console.log(harmonized); const safeDelimiter = getSafeRegexpString(delimiter); if (ignoreInvalid) { @@ -55,10 +40,10 @@ interface SlugifyOptions { /** * Slugify a React node */ -const slugify = ( - node: React.ReactNode, +export default function slugify( + node: ReactNode, options: SlugifyOptions = { delimiter: "-", prefix: "" } -): string => { +): string { if (!options.delimiter) options.delimiter = "-"; if (!options.prefix) options.prefix = ""; @@ -75,8 +60,8 @@ const slugify = ( // string, number if (typeof node === "string" || typeof node === "number") { - const harmonizedPrefix = harmonize(prefix, delimiter, true); - const harmonizedNode = harmonize(String(node), delimiter); + const harmonizedPrefix = format(prefix, delimiter, true); + const harmonizedNode = format(String(node), delimiter); if (harmonizedPrefix) { return `${harmonizedPrefix}${delimiter}${harmonizedNode}`; @@ -106,5 +91,3 @@ const slugify = ( // unhandled case return ""; }; - -export default slugify; diff --git a/yarn.lock b/yarn.lock index 089151c..ca59092 100644 --- a/yarn.lock +++ b/yarn.lock @@ -214,6 +214,11 @@ resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.27.8.tgz#6667fac16c436b5434a387a34dedb013198f6e6e" integrity sha512-+Fj43pSMwJs4KRrH/938Uf+uAELIgVBmQzg/q1YG10djyfA3TnrU8N8XzqCh/okZdszqBQTZf96idMfE5lnwTA== +"@types/diacritics@^1.3.3": + version "1.3.3" + resolved "https://registry.yarnpkg.com/@types/diacritics/-/diacritics-1.3.3.tgz#80f59c779df10d3588527457f32805caaac3589b" + integrity sha512-wt0tBItmBsOUVZ8+MCrkBMoVfH/EUZeTXwYSekVVYilZlGDYssREUR+sX72mHvl2IrbdCKgpYARXKh3awD2how== + "@types/estree@1.0.5", "@types/estree@^1.0.0": version "1.0.5" resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.5.tgz#a6ce3e556e00fd9895dd872dd172ad0d4bd687f4" @@ -354,6 +359,11 @@ deep-eql@^4.1.3: dependencies: type-detect "^4.0.0" +diacritics@^1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/diacritics/-/diacritics-1.3.0.tgz#3efa87323ebb863e6696cebb0082d48ff3d6f7a1" + integrity sha512-wlwEkqcsaxvPJML+rDh/2iS824jbREk6DUMUKkEaSlxdYHeS43cClJtsWglvw2RfeXGm6ohKDqsXteJ5sP5enA== + diff-sequences@^29.6.3: version "29.6.3" resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-29.6.3.tgz#4deaf894d11407c51efc8418012f9e70b84ea921"