Enter polyfills.js

The `String.replaceAll` polyfill was borrowed (at 0% annual intereset
rate) from https://github.com/kiwix/kiwix-js/pull/1190/files.
This commit is contained in:
Veloman Yunkan
2024-02-14 18:49:22 +04:00
committed by Kelson
parent 1babbc0e4a
commit 3ac36e8ebd
5 changed files with 28 additions and 0 deletions

21
static/skin/polyfills.js Normal file
View File

@ -0,0 +1,21 @@
// A few browsers do not support the use of String.prototype.replaceAll method.
// Hence we define it once we verify that it isn't supported. For documentation
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function (pattern, replacement) {
// verify parameter: It must either be a string or a RegExp with a global flag.
if (typeof pattern[Symbol.replace] === 'function') {
// the pattern is a RegExp check for the presence of g flag.
if (pattern.global) {
return this.replace(pattern, replacement);
} else {
throw new TypeError('Global flag for regular expressions')
}
}
// the pattern is not a RegExp, hence it must be a string.
if (typeof pattern !== 'string') {
throw new TypeError('pattern must either be a string or a RegExp with a global (g) flag.')
}
return this.replace(new RegExp(pattern, 'g'), replacement);
}
}