Skip to content

Commit

Permalink
Use node-diacritics to remove accents
Browse files Browse the repository at this point in the history
  • Loading branch information
martpie committed Apr 28, 2024
1 parent fb768e7 commit a84efe1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
45 changes: 14 additions & 31 deletions src/slugify.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 = "";

Expand All @@ -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}`;
Expand Down Expand Up @@ -106,5 +91,3 @@ const slugify = (
// unhandled case
return "";
};

export default slugify;
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]", "@types/estree@^1.0.0":
version "1.0.5"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.5.tgz#a6ce3e556e00fd9895dd872dd172ad0d4bd687f4"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit a84efe1

Please sign in to comment.