forked from melonjs/melonJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.js
More file actions
90 lines (84 loc) · 2.14 KB
/
Copy pathstring.js
File metadata and controls
90 lines (84 loc) · 2.14 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
/**
* a collection of string utility functions
* @namespace utils.string
*/
/**
* converts the first character of the given string to uppercase
* @public
* @function
* @memberof utils.string
* @name capitalize
* @param {string} str the string to be capitalized
* @returns {string} the capitalized string
*/
export function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
};
/**
* returns the string stripped of whitespace from the left.
* @public
* @function
* @memberof utils.string
* @name trimLeft
* @param {string} str the string to be trimmed
* @returns {string} trimmed string
*/
export function trimLeft(str) {
return str.replace(/^\s+/, "");
};
/**
* returns the string stripped of whitespace from the right.
* @public
* @function
* @memberof utils.string
* @name trimRight
* @param {string} str the string to be trimmed
* @returns {string} trimmed string
*/
export function trimRight(str) {
return str.replace(/\s+$/, "");
};
/**
* returns true if the given string contains a numeric integer or float value
* @public
* @function
* @memberof utils.string
* @name isNumeric
* @param {string} str the string to be tested
* @returns {boolean} true if string contains only digits
*/
export function isNumeric(str) {
if (typeof str === "string") {
str = str.trim();
}
return !isNaN(str) && /[+-]?([0-9]*[.])?[0-9]+/.test(str);
};
/**
* returns true if the given string contains a true or false
* @public
* @function
* @memberof utils.string
* @name isBoolean
* @param {string} str the string to be tested
* @returns {boolean} true if the string is either true or false
*/
export function isBoolean(str) {
var trimmed = str.trim();
return (trimmed === "true") || (trimmed === "false");
};
/**
* convert a string to the corresponding hexadecimal value
* @public
* @function
* @memberof utils.string
* @name toHex
* @param {string} str the string to be converted
* @returns {string} the converted hexadecimal value
*/
export function toHex(str) {
var res = "", c = 0;
while (c < str.length) {
res += str.charCodeAt(c++).toString(16);
}
return res;
};