I want to split a string with accent with a query without accent .
This is my code for the moment :
const sanitizer = (text: string): string => {
return text
.normalize("NFD")
.replace(/\p{Diacritic}/gu, "")
.toLowerCase();
};
const splitter = (text: string, query: string): string[] => {
const regexWithQuery = new RegExp(`(${query})|(${sanitizer(query)})`, "gi");
return text.split(regexWithQuery).filter((value) => value);
};
And this is the test file :
import { splitter } from "@/utils/arrayHelpers";
describe("arrayHelpers", () => {
describe("splitter", () => {
const cases = [
{
text: "pepe dominguez",
query: "pepe",
expectedArray: ["pepe", " dominguez"],
},
{
text: "pépé dominguez",
query: "pepe",
expectedArray: ["pépé", " dominguez"],
},
{
text: "pepe dominguez",
query: "pépé",
expectedArray: ["pepe", " dominguez"],
},
{
text: "pepe dominguez",
query: "pe",
expectedArray: ["pe", " pe", " dominguez"],
},
{
text: "pepe DOMINGUEZ",
query: "DOMINGUEZ",
expectedArray: ["pepe ", "DOMINGUEZ"],
},
];
it.each(cases)(
"should return an array of strings with 2 elements [pepe, dominguez]",
({ text, query, expectedArray }) => {
// When I call the splitter function
const textSplitted = splitter(text, query);
// Then I must have an array of two elements
expect(textSplitted).toStrictEqual(expectedArray);
}
);
});
});
The problem is with the second case :
{
text: "pépé dominguez",
query: "pepe",
expectedArray: ["pépé", " dominguez"],
}
because the sanitized query pepe
is also pepe
, so not in Pépé dominguez
.
I don't know how to achieve in this case to make the splitter
function return ['pépé', 'dominguez']
.
I'm looking for a result with the original text , not a sanitize Text