- 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
/**
* a collection of string utility functions
* @namespace utils.string
*/
/**
* converts the first character of the given string to uppercase
* @public
* @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 true if the given string contains a numeric integer or float value
* @public
* @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) && /^[+-]?(\d+(\.\d+)?|\.\d+)$/.test(str);
}
/**
* returns true if the given string contains a true or false
* @public
* @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) {
const trimmed = str.trim();
return (trimmed === "true") || (trimmed === "false");
}
/**
* convert a string to the corresponding hexadecimal value
* @public
* @memberof utils.string
* @name toHex
* @param {string} str - the string to be converted
* @returns {string} the converted hexadecimal value
*/
export function toHex(str) {
let res = "", c = 0;
while (c < str.length) {
res += str.charCodeAt(c++).toString(16);
}
return res;
}
/**
* returns true if the given string is a data url in the `data:[<mediatype>][;base64],<data>` format.
* (this will not test the validity of the Data or Base64 encoding)
* @public
* @memberof utils.string
* @name isDataUrl
* @param {string} str - the string (url) to be tested
* @returns {boolean} true if the string is a data url
*/
export function isDataUrl(str) {
return /^data:(.+);base64,(.+)$/.test(str);
}