Skip to content

Commit 191998e

Browse files
committed
Including String.prototype.replaceAll polyfill to support Node 14.x
1 parent 0d2b273 commit 191998e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lib/common.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,23 @@ class Data {
144144
}
145145

146146
module.exports = { Hit, Miss, Data, Status, Type };
147+
148+
/**
149+
* String.prototype.replaceAll() polyfill
150+
* https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/
151+
* @author Chris Ferdinandi
152+
* @license MIT
153+
*/
154+
if (!String.prototype.replaceAll) {
155+
String.prototype.replaceAll = function(str, newStr){
156+
157+
// If a regex pattern
158+
if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') {
159+
return this.replace(str, newStr);
160+
}
161+
162+
// If a string
163+
return this.replace(new RegExp(str, 'g'), newStr);
164+
165+
};
166+
}

0 commit comments

Comments
 (0)