forked from gregory/meteor-simple-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string-humanize.js
45 lines (35 loc) · 1.01 KB
/
string-humanize.js
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
/*
Code source:
https://github.com/jxson/string-humanize
https://github.com/jxson/string-capitalize
*/
function capitalize(string){
string = string || '';
string = string.trim();
if (string[0]) {
string = string[0].toUpperCase() + string.substr(1).toLowerCase();
}
return string;
}
humanize = function humanize(string){
string = string || '';
string = string.toString(); // might be a number
string = string.trim();
string = string.replace(extname(string), '');
string = underscore(string);
string = string.replace(/[\W_]+/g, ' ');
return capitalize(string);
}
function underscore(string){
string = string || '';
string = string.toString(); // might be a number
string = string.trim();
string = string.replace(/([a-z\d])([A-Z]+)/g, '$1_$2');
string = string.replace(/[-\s]+/g, '_').toLowerCase();
return string;
}
function extname(string){
var index = string.lastIndexOf('.');
var ext = string.substring(index, string.length);
return (index === -1) ? '' : ext;
}