@@ -64,58 +64,6 @@ const Utils = {
64
64
} ,
65
65
66
66
67
- /**
68
- * Adds leading zeros to strings
69
- *
70
- * @param {string } str - String to add leading characters to.
71
- * @param {number } max - Maximum width of the string.
72
- * @param {char } [chr='0'] - The character to pad with.
73
- * @returns {string }
74
- *
75
- * @example
76
- * // returns "0a"
77
- * Utils.padLeft("a", 2);
78
- *
79
- * // returns "000a"
80
- * Utils.padLeft("a", 4);
81
- *
82
- * // returns "xxxa"
83
- * Utils.padLeft("a", 4, "x");
84
- *
85
- * // returns "bcabchello"
86
- * Utils.padLeft("hello", 10, "abc");
87
- */
88
- padLeft : function ( str , max , chr ) {
89
- chr = chr || "0" ;
90
- let startIndex = chr . length - ( max - str . length ) ;
91
- startIndex = startIndex < 0 ? 0 : startIndex ;
92
- return str . length < max ?
93
- Utils . padLeft ( chr . slice ( startIndex , chr . length ) + str , max , chr ) : str ;
94
- } ,
95
-
96
-
97
- /**
98
- * Adds trailing spaces to strings.
99
- *
100
- * @param {string } str - String to add trailing characters to.
101
- * @param {number } max - Maximum width of the string.
102
- * @param {char } [chr='0'] - The character to pad with.
103
- * @returns {string }
104
- *
105
- * @example
106
- * // returns "a "
107
- * Utils.padRight("a", 4);
108
- *
109
- * // returns "axxx"
110
- * Utils.padRight("a", 4, "x");
111
- */
112
- padRight : function ( str , max , chr ) {
113
- chr = chr || " " ;
114
- return str . length < max ?
115
- Utils . padRight ( str + chr . slice ( 0 , max - str . length ) , max , chr ) : str ;
116
- } ,
117
-
118
-
119
67
/**
120
68
* Adds trailing bytes to a byteArray.
121
69
*
@@ -152,14 +100,6 @@ const Utils = {
152
100
} ,
153
101
154
102
155
- /**
156
- * @alias Utils.padLeft
157
- */
158
- pad : function ( str , max , chr ) {
159
- return Utils . padLeft ( str , max , chr ) ;
160
- } ,
161
-
162
-
163
103
/**
164
104
* Truncates a long string to max length and adds suffix.
165
105
*
@@ -201,7 +141,7 @@ const Utils = {
201
141
hex : function ( c , length ) {
202
142
c = typeof c == "string" ? Utils . ord ( c ) : c ;
203
143
length = length || 2 ;
204
- return Utils . pad ( c . toString ( 16 ) , length ) ;
144
+ return c . toString ( 16 ) . padStart ( length , "0" ) ;
205
145
} ,
206
146
207
147
@@ -222,7 +162,7 @@ const Utils = {
222
162
bin : function ( c , length ) {
223
163
c = typeof c == "string" ? Utils . ord ( c ) : c ;
224
164
length = length || 8 ;
225
- return Utils . pad ( c . toString ( 2 ) , length ) ;
165
+ return c . toString ( 2 ) . padStart ( length , "0" ) ;
226
166
} ,
227
167
228
168
@@ -656,7 +596,7 @@ const Utils = {
656
596
/**
657
597
* Convert a byte array into a hex string.
658
598
*
659
- * @param {byteArray } data
599
+ * @param {Uint8Array| byteArray } data
660
600
* @param {string } [delim=" "]
661
601
* @param {number } [padding=2]
662
602
* @returns {string }
@@ -676,7 +616,7 @@ const Utils = {
676
616
let output = "" ;
677
617
678
618
for ( let i = 0 ; i < data . length ; i ++ ) {
679
- output += Utils . pad ( data [ i ] . toString ( 16 ) , padding ) + delim ;
619
+ output += data [ i ] . toString ( 16 ) . padStart ( padding , "0" ) + delim ;
680
620
}
681
621
682
622
// Add \x or 0x to beginning
@@ -1379,3 +1319,45 @@ Array.prototype.equals = function(other) {
1379
1319
String . prototype . count = function ( chr ) {
1380
1320
return this . split ( chr ) . length - 1 ;
1381
1321
} ;
1322
+
1323
+
1324
+ /*
1325
+ * Polyfills
1326
+ */
1327
+
1328
+ // https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
1329
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
1330
+ if ( ! String . prototype . padStart ) {
1331
+ String . prototype . padStart = function padStart ( targetLength , padString ) {
1332
+ targetLength = targetLength >> 0 ; //floor if number or convert non-number to 0;
1333
+ padString = String ( ( typeof padString !== "undefined" ? padString : " " ) ) ;
1334
+ if ( this . length > targetLength ) {
1335
+ return String ( this ) ;
1336
+ } else {
1337
+ targetLength = targetLength - this . length ;
1338
+ if ( targetLength > padString . length ) {
1339
+ padString += padString . repeat ( targetLength / padString . length ) ; //append to original to ensure we are longer than needed
1340
+ }
1341
+ return padString . slice ( 0 , targetLength ) + String ( this ) ;
1342
+ }
1343
+ } ;
1344
+ }
1345
+
1346
+
1347
+ // https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
1348
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd
1349
+ if ( ! String . prototype . padEnd ) {
1350
+ String . prototype . padEnd = function padEnd ( targetLength , padString ) {
1351
+ targetLength = targetLength >> 0 ; //floor if number or convert non-number to 0;
1352
+ padString = String ( ( typeof padString !== "undefined" ? padString : " " ) ) ;
1353
+ if ( this . length > targetLength ) {
1354
+ return String ( this ) ;
1355
+ } else {
1356
+ targetLength = targetLength - this . length ;
1357
+ if ( targetLength > padString . length ) {
1358
+ padString += padString . repeat ( targetLength / padString . length ) ; //append to original to ensure we are longer than needed
1359
+ }
1360
+ return String ( this ) + padString . slice ( 0 , targetLength ) ;
1361
+ }
1362
+ } ;
1363
+ }
0 commit comments