1
const location = places({
  ...this.appKeyId,
  container: document.querySelector('#location') as HTMLInputElement,
  });

How do I replace the accents/diacritic in algolia places? cause What I want is to replace the accents/diacritic.

1 Answers1

0

Using String.prototype.normalize(), you can remove diacritics as needed. Using your example string of Vũng Tàu, the NFD and NFKD removed the diacritics in the tests below. I wanted to see and show how each step changed the string based on the accepted answer in that question. Considering the canonical equivalence (not sure if this is a requirement), I included tests to show whether each change was equivalent to the others.

const str = "Vũng Tàu";
const nfdStr = str.normalize("NFD");
const nfcStr = str.normalize("NFC");
const nfkcStr = str.normalize("NFKC");
const nfkdStr = str.normalize("NFKD");
console.log("nfdStr   ", nfdStr);
console.log("nfcStr   ", nfcStr);
console.log("nfkcStr  ", nfkcStr);
console.log("nfkdStr  ", nfkdStr);
console.log("");

const nfdStr1 = nfdStr.replace(/[\u0300-\u036f]/g, "");
const nfcStr1 = nfcStr.replace(/[\u0300-\u036f]/g, "");
const nfkcStr1 = nfkcStr.replace(/[\u0300-\u036f]/g, "");
const nfkdStr1 = nfkdStr.replace(/[\u0300-\u036f]/g, "");
console.log("nfdStr1  ", nfdStr1);
console.log("nfcStr1  ", nfcStr1);
console.log("nfkcStr1 ", nfkcStr1);
console.log("nfkdStr1 ", nfkdStr1);
console.log("");

const nfdStr2 = nfdStr.replace(/\p{Diacritic}/gu, "");
const nfcStr2 = nfcStr.replace(/\p{Diacritic}/gu, "");
const nfkcStr2 = nfkcStr.replace(/\p{Diacritic}/gu, "");
const nfkdStr2 = nfkdStr.replace(/\p{Diacritic}/gu, "");
console.log("nfdStr2  ", nfdStr2);
console.log("nfcStr2  ", nfcStr2);
console.log("nfkcStr2 ", nfkcStr2);
console.log("nfkdStr2 ", nfkdStr2);
console.log("");

console.log(nfdStr === nfdStr1, nfdStr == nfdStr2, nfdStr1 === nfdStr2);
console.log(nfcStr === nfcStr1, nfcStr == nfcStr2, nfcStr1 === nfcStr2);
console.log(nfkcStr === nfkcStr1, nfkcStr == nfkcStr2, nfkcStr1 === nfkcStr2);
console.log(nfkdStr === nfkdStr1, nfkdStr == nfkdStr2, nfkdStr1 === nfkdStr2);
phentnil
  • 2,195
  • 2
  • 14
  • 22