Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Created by gavorhes on 10/30/2015.
*/
"use strict";
if (!String.prototype['format']) {
/**
* helper function for string replacement to keep code clean
* usage
* var aString = 'some{0}stuff{1}replaced';
* var c = 'cat';
* var b = 'bird';
* aString.format(c, b) returns 'somecatstuffbirdreplaced'
* prettier than
* 'some' + c + 'stuff' + b + 'replaced'
* but same effect
* adapted to take a single array that is used for replacement by position ie
* var arrReplacements = [c, b];
* aString.format(arrReplacements)
* @returns {string} converted string
*/
String.prototype['format'] = function () {
var args = arguments;
for (var i = 0; i < args.length; i++) {
args[i] = (args[i] !== null ? args[i] : '');
}
//if the first argument is an array, use that
if (args[0].constructor == Array) {
args = args[0];
}
return this.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined' ? args[number] : match;
});
};
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = undefined;
//# sourceMappingURL=formatString.js.map