-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstrings.js
More file actions
110 lines (102 loc) · 3.3 KB
/
strings.js
File metadata and controls
110 lines (102 loc) · 3.3 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
var binary = require("binary");
var Binary = binary.Binary, ByteString = binary.ByteString;
/**
* Returns true if string starts with the given substring
*
* @param {String}
* string the string to search in
* @param {String}
* substring pattern to search for
* @returns {Boolean} true in case it matches the beginning of the string, false
* otherwise
*/
var startsWith = exports.startsWith = function(string, substring) {
return string.indexOf(substring) == 0;
};
/**
* Returns true if string ends with the given substring
*
* @param {String}
* string the string to search in
* @param {String}
* substring pattern to search for
* @returns Boolean true in case it matches the end of the string, false
* otherwise
*/
var endsWith = exports.endsWith = function(string, substring) {
var diff = string.length - substring.length;
return diff > -1 && string.lastIndexOf(substring) == diff;
};
/**
* Encode a string or binary to a Base16 encoded string
*
* @param {String|Binary}
* str a string or binary
* @param {String}
* encoding optional encoding to use if first argument is a string.
* Defaults to 'utf8'.
* @returns the Base16 encoded string
*/
var b16encode = exports.b16encode = function(str, encoding) {
encoding = encoding || 'utf8';
var input = str instanceof Binary ? str : String(str).toByteString(encoding);
var length = input.length;
var result = [];
var chars = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
'C', 'D', 'E', 'F' ];
for( var i = 0; i < length; i++) {
var n = input[i];
result.push(chars[n >>> 4], chars[n & 0xf]);
}
return result.join('');
};
/**
* Escape the string to make it safe for use within an HTML document.
*
* @param {String}
* string the string to escape
* @return {String} the escaped string
*/
var escapeHtml = exports.escapeHtml = function(string) {
return string.replace(/&/g, '&').replace(/"/g, '"').replace(/>/g,
'>').replace(/</g, '<');
};
/**
* Calculates a message digest of a string. If no
* argument is passed, the MD5 algorithm is used.
* @param {String} string the string to digest
* @param {String} algorithm the name of the algorithm to use
* @returns {String} base16-encoded message digest of the string
*/
var digest = exports.digest = function(string, algorithm) {
var algorithms = {
md5: require("./md5").md5,
sha1: require("./sha1").sha1
};
var md = algorithms[algorithm || 'md5'];
return md(string).toUpperCase();
};
/**
* Fills the string provided with the arguments.
* @param {String} string the string to fill
* @param {Object} args the arguments used to fill the string
*/
var fill = exports.fill = function(string, args) {
if(!string) return;
for(var key in args) {
string = string.replace(new RegExp('\\{' + key + '\\}','g'), args[key]);
}
return string;
}
/**
* Returns true if string contains substring.
* @param {String} string the string to search in
* @param {String} substring the string to search for
* @param {Number} fromIndex optional index to start searching
* @returns true if str is contained in this string
* @type Boolean
*/
var contains = exports.contains = function(string, substring, fromIndex) {
fromIndex = fromIndex || 0;
return string.indexOf(substring, fromIndex) > -1;
}