From 56d654b0540ab464013dd6ff7cc980ec7a1d7f6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fro=CC=88mmel?= Date: Thu, 8 Apr 2021 22:15:14 +0000 Subject: [PATCH] Replace diacriticless-Module with optimized internal alternative As the npm-Module "diacriticless" is highly inefficient when replacing dicritics (it iterates thousands of times multiple arrays) and the author does not merge the proposed pull-request with a very optimized routine of this, this optimization is now implemented locally in vue-good-table. As https://github.com/xaksis/vue-good-table/issues/545 states, there is a performanceproblem because of the diacriticless-implementation. So this could help fixing this issue as well. --- package.json | 1 - src/components/types/default.js | 2 +- src/components/utils/diacritics.js | 420 +++++++++++++++++++++++++++++ 3 files changed, 421 insertions(+), 2 deletions(-) create mode 100644 src/components/utils/diacritics.js diff --git a/package.json b/package.json index 7fd78930..1fe4433a 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,6 @@ }, "dependencies": { "date-fns": "^2.17.0", - "diacriticless": "1.0.1", "lodash.isequal": "^4.5.0" }, "devDependencies": { diff --git a/src/components/types/default.js b/src/components/types/default.js index f3d56d13..7da0b747 100644 --- a/src/components/types/default.js +++ b/src/components/types/default.js @@ -1,4 +1,4 @@ -import diacriticless from 'diacriticless'; +import { diacriticless } from '../utils/diacritics'; const escapeRegExp = str => str.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&'); diff --git a/src/components/utils/diacritics.js b/src/components/utils/diacritics.js new file mode 100644 index 00000000..5feb38ef --- /dev/null +++ b/src/components/utils/diacritics.js @@ -0,0 +1,420 @@ +// all diacritics +let diacritics = { + a: [ + "a", + "à", + "á", + "â", + "ã", + "ä", + "å", + "æ", + "ā", + "ă", + "ą", + "ǎ", + "ǟ", + "ǡ", + "ǻ", + "ȁ", + "ȃ", + "ȧ", + "ɐ", + "ɑ", + "ɒ", + "ͣ", + "а", + "ӑ", + "ӓ", + "ᵃ", + "ᵄ", + "ᶏ", + "ḁ", + "ẚ", + "ạ", + "ả", + "ấ", + "ầ", + "ẩ", + "ẫ", + "ậ", + "ắ", + "ằ", + "ẳ", + "ẵ", + "ặ", + "ₐ", + "ⱥ", + "a", + ], + b: ["b", "ƀ", "ƃ", "ɓ", "ᖯ", "ᵇ", "ᵬ", "ᶀ", "ḃ", "ḅ", "ḇ", "b"], + c: ["c", "ç", "ć", "ĉ", "ċ", "č", "ƈ", "ȼ", "ɕ", "ͨ", "ᴄ", "ᶜ", "ḉ", "ↄ", "c"], + d: [ + "d", + "ď", + "đ", + "Ƌ", + "ƌ", + "ȡ", + "ɖ", + "ɗ", + "ͩ", + "ᵈ", + "ᵭ", + "ᶁ", + "ᶑ", + "ḋ", + "ḍ", + "ḏ", + "ḑ", + "ḓ", + "d", + ], + e: [ + "e", + "è", + "é", + "ê", + "ë", + "ē", + "ĕ", + "ė", + "ę", + "ě", + "ǝ", + "ȅ", + "ȇ", + "ȩ", + "ɇ", + "ɘ", + "ͤ", + "ᵉ", + "ᶒ", + "ḕ", + "ḗ", + "ḙ", + "ḛ", + "ḝ", + "ẹ", + "ẻ", + "ẽ", + "ế", + "ề", + "ể", + "ễ", + "ệ", + "ₑ", + "e", + ], + f: ["f", "ƒ", "ᵮ", "ᶂ", "ᶠ", "ḟ", "f"], + g: ["g", "ĝ", "ğ", "ġ", "ģ", "ǥ", "ǧ", "ǵ", "ɠ", "ɡ", "ᵍ", "ᵷ", "ᵹ", "ᶃ", "ᶢ", "ḡ", "g"], + h: [ + "h", + "ĥ", + "ħ", + "ƕ", + "ȟ", + "ɥ", + "ɦ", + "ʮ", + "ʯ", + "ʰ", + "ʱ", + "ͪ", + "Һ", + "һ", + "ᑋ", + "ᶣ", + "ḣ", + "ḥ", + "ḧ", + "ḩ", + "ḫ", + "ⱨ", + "h", + ], + i: [ + "i", + "ì", + "í", + "î", + "ï", + "ĩ", + "ī", + "ĭ", + "į", + "ǐ", + "ȉ", + "ȋ", + "ɨ", + "ͥ", + "ᴉ", + "ᵎ", + "ᵢ", + "ᶖ", + "ᶤ", + "ḭ", + "ḯ", + "ỉ", + "ị", + "i", + ], + j: ["j", "ĵ", "ǰ", "ɉ", "ʝ", "ʲ", "ᶡ", "ᶨ", "j"], + k: ["k", "ķ", "ƙ", "ǩ", "ʞ", "ᵏ", "ᶄ", "ḱ", "ḳ", "ḵ", "ⱪ", "k"], + l: [ + "l", + "ĺ", + "ļ", + "ľ", + "ŀ", + "ł", + "ƚ", + "ȴ", + "ɫ", + "ɬ", + "ɭ", + "ˡ", + "ᶅ", + "ᶩ", + "ᶪ", + "ḷ", + "ḹ", + "ḻ", + "ḽ", + "ℓ", + "ⱡ", + ], + m: ["m", "ɯ", "ɰ", "ɱ", "ͫ", "ᴟ", "ᵐ", "ᵚ", "ᵯ", "ᶆ", "ᶬ", "ᶭ", "ḿ", "ṁ", "ṃ", "㎡", "㎥", "m"], + n: [ + "n", + "ñ", + "ń", + "ņ", + "ň", + "ʼn", + "ƞ", + "ǹ", + "ȵ", + "ɲ", + "ɳ", + "ᵰ", + "ᶇ", + "ᶮ", + "ᶯ", + "ṅ", + "ṇ", + "ṉ", + "ṋ", + "ⁿ", + "n", + ], + o: [ + "o", + "ò", + "ó", + "ô", + "õ", + "ö", + "ø", + "ō", + "ŏ", + "ő", + "ơ", + "ǒ", + "ǫ", + "ǭ", + "ǿ", + "ȍ", + "ȏ", + "ȫ", + "ȭ", + "ȯ", + "ȱ", + "ɵ", + "ͦ", + "о", + "ӧ", + "ө", + "ᴏ", + "ᴑ", + "ᴓ", + "ᴼ", + "ᵒ", + "ᶱ", + "ṍ", + "ṏ", + "ṑ", + "ṓ", + "ọ", + "ỏ", + "ố", + "ồ", + "ổ", + "ỗ", + "ộ", + "ớ", + "ờ", + "ở", + "ỡ", + "ợ", + "ₒ", + "o", + "𐐬", + ], + p: ["p", "ᵖ", "ᵱ", "ᵽ", "ᶈ", "ṕ", "ṗ", "p"], + q: ["q", "ɋ", "ʠ", "ᛩ", "q"], + r: [ + "r", + "ŕ", + "ŗ", + "ř", + "ȑ", + "ȓ", + "ɍ", + "ɹ", + "ɻ", + "ʳ", + "ʴ", + "ʵ", + "ͬ", + "ᵣ", + "ᵲ", + "ᶉ", + "ṙ", + "ṛ", + "ṝ", + "ṟ", + ], + s: ["s", "ś", "ŝ", "ş", "š", "ș", "ʂ", "ᔆ", "ᶊ", "ṡ", "ṣ", "ṥ", "ṧ", "ṩ", "s"], + t: [ + "t", + "ţ", + "ť", + "ŧ", + "ƫ", + "ƭ", + "ț", + "ʇ", + "ͭ", + "ᵀ", + "ᵗ", + "ᵵ", + "ᶵ", + "ṫ", + "ṭ", + "ṯ", + "ṱ", + "ẗ", + "t", + ], + u: [ + "u", + "ù", + "ú", + "û", + "ü", + "ũ", + "ū", + "ŭ", + "ů", + "ű", + "ų", + "ư", + "ǔ", + "ǖ", + "ǘ", + "ǚ", + "ǜ", + "ȕ", + "ȗ", + "ͧ", + "ߎ", + "ᵘ", + "ᵤ", + "ṳ", + "ṵ", + "ṷ", + "ṹ", + "ṻ", + "ụ", + "ủ", + "ứ", + "ừ", + "ử", + "ữ", + "ự", + "u", + ], + v: ["v", "ʋ", "ͮ", "ᵛ", "ᵥ", "ᶹ", "ṽ", "ṿ", "ⱱ", "v", "ⱴ"], + w: ["w", "ŵ", "ʷ", "ᵂ", "ẁ", "ẃ", "ẅ", "ẇ", "ẉ", "ẘ", "ⱳ", "w"], + x: ["x", "̽", "͓", "ᶍ", "ͯ", "ẋ", "ẍ", "ₓ", "x"], + y: ["y", "ý", "ÿ", "ŷ", "ȳ", "ɏ", "ʸ", "ẏ", "ỳ", "ỵ", "ỷ", "ỹ", "y"], + z: [ + "z", + "ź", + "ż", + "ž", + "ƶ", + "ȥ", + "ɀ", + "ʐ", + "ʑ", + "ᙆ", + "ᙇ", + "ᶻ", + "ᶼ", + "ᶽ", + "ẑ", + "ẓ", + "ẕ", + "ⱬ", + "z", + ], +}; + +// Precompiled Object with { key = Diacritic, value = real-Character } +const compiledDiactitics = (function() { + let x = {}; + + for (let key in diacritics) { + let ok = diacritics[key]; + + for (let rval in ok) { + let val = ok[rval]; + + // Do not replace the char with itself + if (val !== key) { + x[val] = key; + } + } + } + + return x; +})(); + +// Regex for detecting non-ASCII-Characters in String +const regexNonASCII = /[^a-z0-9\s,.-]/; + +/* + * Main function of the module which removes all diacritics from the received text + */ +const diacriticless = (text) => { + // When there are only ascii-Characters in the string, skip processing and return text right away + if (text.search(regexNonASCII) === -1) { + return text; + } + + let result = ""; + + let len = text.length; + for (var i = 0; i < len; i++) { + let searchChar = text.charAt(i); + + // If applicable replace the diacritic character with the real one or use the original value + result += searchChar in compiledDiactitics ? compiledDiactitics[searchChar] : searchChar; + } + + return result; +}; + +export {diacriticless};