Skip to content

Commit d9f3455

Browse files
author
Eugene Tupikov
committed
don't extend string prototype to add replaceAll function
1 parent 2639ebd commit d9f3455

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/assets/src/js/jquery.multipleInput.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
(function ($) {
22
'use strict';
33

4-
String.prototype.replaceAll = function (search, replace) {
5-
return this.split(search).join(replace);
6-
};
7-
84
$.fn.multipleInput = function (method) {
95
if (methods[method]) {
106
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
@@ -275,7 +271,7 @@
275271
return;
276272
}
277273

278-
template = template.replaceAll('{' + settings.indexPlaceholder + '}', data.currentIndex);
274+
template = replaceAll('{' + settings.indexPlaceholder + '}', data.currentIndex, template);
279275
var $addedInput = $(template);
280276
var currentIndex = data.currentIndex;
281277

@@ -307,9 +303,9 @@
307303
var jsTemplate;
308304

309305
for (var i in settings.jsTemplates) {
310-
jsTemplate = settings.jsTemplates[i]
311-
.replaceAll('{' + settings.indexPlaceholder + '}', data.currentIndex)
312-
.replaceAll('%7B' + settings.indexPlaceholder + '%7D', data.currentIndex);
306+
jsTemplate = settings.jsTemplates[i];
307+
jsTemplate = replaceAll('{' + settings.indexPlaceholder + '}', data.currentIndex, jsTemplate);
308+
jsTemplate = replaceAll('%7B' + settings.indexPlaceholder + '%7D', data.currentIndex, jsTemplate);
313309

314310
window.eval(jsTemplate);
315311
}
@@ -420,9 +416,9 @@
420416
if (data.settings.attributes.hasOwnProperty(bareId)) {
421417
attributeOptions = data.settings.attributes[bareId];
422418
} else {
423-
// fallback in case of using flatten widget - just remove all digital indexes and check whether attribute
424-
// exists or not.
425-
bareId = bareId.replaceAll(/-\d-/, '-').replaceAll(/-\d/, '');
419+
// fallback in case of using flatten widget - just remove all digital indexes
420+
// and check whether attribute exists or not.
421+
bareId = replaceAll(/-\d-/, '-').replaceAll(/-\d/, '', bareId);
426422
if (data.settings.attributes.hasOwnProperty(bareId)) {
427423
attributeOptions = data.settings.attributes[bareId];
428424
}
@@ -486,4 +482,13 @@
486482
});
487483
return values;
488484
};
485+
486+
var replaceAll = function (search, replace, subject) {
487+
if (!subject instanceof String) {
488+
console.warn('Call replaceAll for non-string value: ' + subject);
489+
return subject;
490+
}
491+
492+
return subject.split(search).join(replace);
493+
};
489494
})(window.jQuery);

src/assets/src/js/jquery.multipleInput.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)