This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (66 loc) · 2.29 KB
/
index.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
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
function capitalize( item ) {
return item.charAt( 0 ).toUpperCase() + item.substring( 1 ).toLowerCase();
}
function guid_replace( match ) {
var num = ( Math.random() * 16 ) | 0;
return ( match == 'x' ? num : ( num & 0x3 | 0x8 ) ).toString( 16 );
}
function id_create( prefix ) {
return ( prefix || id_prefix ) + '-' + ( ++id_count );
}
function interpolate( str, o, pattern ) {
return String( str ).replace( ( pattern || re_gsub ), function( m, p ) {
return o.hasOwnProperty(p) ? o[p] : '';
} );
}
// so we don't lose any chars on split
function _splitString( m, p ) { return p + p.toLowerCase(); }
function splitString( s ) {
s = s.trim();
var s0 = s.charAt( 0 ), s1 = s.charAt( 1 ),
i = s0.toLowerCase() == s0 && s1 != ' ' && s1.toUpperCase() == s1 ? 2 : 1,
o = s.substring( i ).replace( re_caps, _splitString ).split( re_split_string );
o[0] = s.substring( 0, i ) + o[0];
return o;
}
var id_count = 999,
id_prefix = 'anon',
re_caps = /([A-Z])/g,
re_gsub = /\$?\{([^\}'"]+)\}/g,
re_guid = /[xy]/g,
re_split_string = /[\sA-Z_-]+/g,
tpl_guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
module.exports = {
capitalize : capitalize,
format : function format( item ) {
return interpolate( item, Array.prototype.slice.call( arguments, 1 ) );
},
guid : function guid() { // credit for guid goes here: gist.github.com/2295777
return tpl_guid.replace( re_guid, guid_replace );
},
hyphenate : function hyphenate( item ) {
return splitString( item ).join( '-' ).toLowerCase();
},
id : function id( item, prefix ) {
return item ? 'id' in Object( item ) && item.id ? item.id : ( item.id = id_create( prefix ) ) : id_create( prefix );
},
interpolate : interpolate,
pad : function( num, len, radix ) {
var s = Number( num ).toString( radix || 10 ),
i = -1,
l = len - s.length;
while ( ++i < l )
s = '0' + s;
return s;
},
toCamelCase : function toCamelCase( item ) {
var parts = splitString( item );
return parts.reduce( function( res, val ) {
res.push( capitalize( val ) );
return res;
}, [parts.shift()] ).join( '' );
},
underscore : function underscore( item ) {
return splitString( item ).join( '_' ).toLowerCase();
}
};