forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.js
More file actions
116 lines (105 loc) · 3.68 KB
/
string.js
File metadata and controls
116 lines (105 loc) · 3.68 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* @namespace
* @name pc.string
* @description Extended String API
*/
pc.string = function () {
return {
/**
* @name pc.string.ASCII_LOWERCASE
* @description All lowercase letters
* @type String
*/
ASCII_LOWERCASE: "abcdefghijklmnopqrstuvwxyz",
/**
* @name pc.string.ASCII_UPPERCASE
* @description All uppercase letters
* @type String
*/
ASCII_UPPERCASE: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
/**
* @name pc.string.ASCII_LETTERS
* @description All ASCII letters
* @type String
*/
ASCII_LETTERS: this.ASCII_LOWERCASE + this.ASCII_UPPERCASE,
/**
* @function
* @name pc.string.format
* @description Return a string with {n} replaced with the n-th argument
* @param {String} s The string to format
* @param {Object} [arguments] All other arguments are substituted into the string
* @returns {String} The formatted string
* @example
* var s = pc.string.format("Hello {0}", "world");
* console.log(s); // Prints "Hello world"
*/
format: function (s) {
var i = 0,
regexp,
args = pc.makeArray(arguments);
// drop first argument
args.shift();
for (i = 0; i < args.length; i++) {
regexp = new RegExp('\\{' + i + '\\}', 'gi');
s = s.replace(regexp, args[i]);
}
return s;
},
/**
* @private
* @function
* @name pc.string.startsWith
* @description Check if a string s starts with another string subs
* @param {String} s The string to look in
* @param {String} subs The string to look for
* @returns {Boolean} True if s starts with subs
* @deprecated
* @example
* var s = "abc";
* if (pc.string.startsWith(s, "a")) {
* console.log('Starts with a');
* }
*/
startsWith: function (s, subs) {
console.warn("WARNING: startsWith: Function is deprecated. Use String.startsWith instead.");
return s.startsWith(subs);
},
/**
* @private
* @function
* @name pc.string.endsWith
* @description Check if a string s ends with another string subs
* @param {String} s The string to look in
* @param {String} subs The string to look for
* @returns {Boolean} True if s ends with subs
* @deprecated
*/
endsWith: function (s, subs) {
console.warn("WARNING: endsWith: Function is deprecated. Use String.endsWith instead.");
return s.endsWith(subs);
},
/**
* @function
* @name pc.string.toBool
* @description Convert a string value to a boolean. In non-strict mode (the default), 'true' is converted to true, all other values
* are converted to false. In strict mode, 'true' is converted to true, 'false' is converted to false, all other values will throw
* an Exception.
* @param {String} s The string to convert
* @param {Boolean} [strict] In strict mode an Exception is thrown if s is not an accepted string value. Defaults to false
* @returns {Boolean} The converted value
*/
toBool: function (s, strict) {
if (s === 'true') {
return true;
}
if (strict) {
if (s === 'false') {
return false;
}
throw new Error('Not a boolean string');
}
return false;
}
};
} ();