-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.extends.js
46 lines (44 loc) · 1.37 KB
/
string.extends.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
(function(document, $){
var padding = function( s, len, chr ){
chr = ( chr || '0' ) + '';
len = len || 0;
while( s.length < len ){
s = chr + s;
}
return s.substr( 0, len );
},
format = function( s ){
arr = Array.prototype.slice.call( arguments, 1 );
var i = 1;
s = s.replace( /%s/g, function(){
return '{%' + i++ + '%}';
});
var ii = arr.length;
i = 0;
while( i < ii ){
s = s.replace(
new RegExp( '{%' + (i+1) + '%}', 'g' ),
arr[ i++ ]
);
}
return s.replace( /\{%\d%\}/g, '' );
};
[
[ 'padding' , padding ],
[ 'sprintf' , format ],
[ 'format' , format ]
].forEach( function( items, index ){
var method = items[0],
func = items[1];
String.prototype[ method ] || (
String.prototype[ method ] = function(){
return func.apply(
null,
[ this ].concat(
Array.prototype.slice.call( arguments, 0 )
)
);
}
);
} );
})();