Skip to content

Commit 19c74cd

Browse files
committed
Refactor to avoid need for hasPeriod property
1 parent 6553ca8 commit 19c74cd

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

lib/node_modules/@stdlib/string/base/format-interpolate/lib/main.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ function initialize( token ) {
4848
out.specifier = token.specifier;
4949
out.precision = ( token.precision === void 0 ) ? 1 : token.precision;
5050
out.width = token.width;
51-
out.hasPeriod = token.hasPeriod || false;
5251
out.flags = token.flags || '';
5352
out.mapping = token.mapping;
5453
return out;
@@ -72,6 +71,7 @@ function initialize( token ) {
7271
* // returns 'beep boop'
7372
*/
7473
function formatInterpolate( tokens ) {
74+
var hasPeriod;
7575
var flags;
7676
var token;
7777
var flag;
@@ -91,14 +91,15 @@ function formatInterpolate( tokens ) {
9191
if ( isString( token ) ) {
9292
out += token;
9393
} else {
94+
hasPeriod = token.precision !== void 0;
9495
token = initialize( token );
9596
if ( !token.specifier ) {
9697
throw new TypeError( 'invalid argument. Token is missing `specifier` property. Index: `'+ i +'`. Value: `' + token + '`.' );
9798
}
9899
if ( token.mapping ) {
99100
pos = token.mapping;
100101
}
101-
flags = token.flags || '';
102+
flags = token.flags;
102103
for ( j = 0; j < flags.length; j++ ) {
103104
flag = flags.charAt( j );
104105
switch ( flag ) {
@@ -133,7 +134,7 @@ function formatInterpolate( tokens ) {
133134
token.width = -token.width;
134135
}
135136
}
136-
if ( token.hasPeriod ) {
137+
if ( hasPeriod ) {
137138
if ( token.precision === '*' ) {
138139
token.precision = parseInt( arguments[ pos ], 10 );
139140
pos += 1;
@@ -142,7 +143,7 @@ function formatInterpolate( tokens ) {
142143
}
143144
if ( token.precision < 0 ) {
144145
token.precision = 1;
145-
token.hasPeriod = false;
146+
hasPeriod = false;
146147
}
147148
}
148149
}
@@ -156,14 +157,14 @@ function formatInterpolate( tokens ) {
156157
case 'i':
157158
case 'u':
158159
// Case: %b (binary), %o (octal), %x, %X (hexadecimal), %d, %i (decimal), %u (unsigned decimal)
159-
if ( token.hasPeriod ) {
160+
if ( hasPeriod ) {
160161
token.padZeros = false;
161162
}
162163
token.arg = formatInteger( token );
163164
break;
164165
case 's':
165166
// Case: %s (string)
166-
token.maxWidth = ( token.hasPeriod ) ? token.precision : -1;
167+
token.maxWidth = ( hasPeriod ) ? token.precision : -1;
167168
break;
168169
case 'c':
169170
// Case: %c (character)
@@ -184,7 +185,7 @@ function formatInterpolate( tokens ) {
184185
case 'g':
185186
case 'G':
186187
// Case: %e, %E (scientific notation), %f, %F (decimal floating point), %g, %G (uses the shorter of %e/E or %f/F)
187-
if ( !token.hasPeriod ) {
188+
if ( !hasPeriod ) {
188189
token.precision = 6;
189190
}
190191
token.arg = formatDouble( token );

lib/node_modules/@stdlib/string/base/format-tokenize/lib/main.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ var RE = /%(?:([1-9]\d*)\$)?([0 +\-#]*)(\*|\d+)?(?:(\.)(\*|\d+)?)?[hlL]?([%A-Za-
3333
* @returns {Object} delimiter token object
3434
*/
3535
function parse( match ) {
36-
return {
36+
var token = {
3737
'mapping': ( match[ 1 ] ) ? parseInt( match[ 1 ], 10 ) : void 0,
3838
'flags': match[ 2 ],
3939
'width': match[ 3 ],
40-
'hasPeriod': match[ 4 ] === '.',
4140
'precision': match[ 5 ],
4241
'specifier': match[ 6 ]
4342
};
43+
if ( match[ 4 ] === '.' && match[ 5 ] === void 0 ) {
44+
token.precision = '1';
45+
}
46+
return token;
4447
}
4548

4649

lib/node_modules/@stdlib/string/base/format-tokenize/test/test.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ tape( 'the function tokenizes a string', function test( t ) {
4444
'flags': '',
4545
'mapping': void 0,
4646
'width': void 0,
47-
'hasPeriod': false,
4847
'precision': void 0,
4948
'specifier': 's'
5049
},
@@ -60,7 +59,6 @@ tape( 'the function tokenizes a string', function test( t ) {
6059
'flags': '',
6160
'mapping': void 0,
6261
'width': void 0,
63-
'hasPeriod': false,
6462
'precision': void 0,
6563
'specifier': 'd'
6664
}
@@ -83,7 +81,6 @@ tape( 'the function tokenizes a string (extracting flags)', function test( t ) {
8381
'flags': '+',
8482
'mapping': void 0,
8583
'width': void 0,
86-
'hasPeriod': false,
8784
'precision': void 0,
8885
'specifier': 's'
8986
},
@@ -99,7 +96,6 @@ tape( 'the function tokenizes a string (extracting flags)', function test( t ) {
9996
'flags': '-+',
10097
'mapping': void 0,
10198
'width': void 0,
102-
'hasPeriod': false,
10399
'precision': void 0,
104100
'specifier': 's'
105101
},
@@ -115,7 +111,6 @@ tape( 'the function tokenizes a string (extracting flags)', function test( t ) {
115111
'flags': '#+',
116112
'mapping': void 0,
117113
'width': void 0,
118-
'hasPeriod': false,
119114
'precision': void 0,
120115
'specifier': 's'
121116
},
@@ -139,7 +134,6 @@ tape( 'the function tokenizes a string (extracting positional arguments)', funct
139134
'flags': '',
140135
'mapping': 1,
141136
'width': void 0,
142-
'hasPeriod': false,
143137
'precision': void 0,
144138
'specifier': 's'
145139
},
@@ -155,7 +149,6 @@ tape( 'the function tokenizes a string (extracting positional arguments)', funct
155149
'flags': '',
156150
'mapping': 1,
157151
'width': void 0,
158-
'hasPeriod': false,
159152
'precision': void 0,
160153
'specifier': 's'
161154
},
@@ -164,7 +157,6 @@ tape( 'the function tokenizes a string (extracting positional arguments)', funct
164157
'flags': '',
165158
'mapping': 2,
166159
'width': void 0,
167-
'hasPeriod': false,
168160
'precision': void 0,
169161
'specifier': 's'
170162
},
@@ -188,7 +180,6 @@ tape( 'the function tokenizes a string (extracting width)', function test( t ) {
188180
'flags': '',
189181
'mapping': void 0,
190182
'width': '5',
191-
'hasPeriod': false,
192183
'precision': void 0,
193184
'specifier': 's'
194185
},
@@ -204,7 +195,6 @@ tape( 'the function tokenizes a string (extracting width)', function test( t ) {
204195
'flags': '0',
205196
'mapping': void 0,
206197
'width': '5',
207-
'hasPeriod': false,
208198
'precision': void 0,
209199
'specifier': 's'
210200
},
@@ -220,7 +210,6 @@ tape( 'the function tokenizes a string (extracting width)', function test( t ) {
220210
'flags': '',
221211
'mapping': void 0,
222212
'width': '5',
223-
'hasPeriod': false,
224213
'precision': void 0,
225214
'specifier': 'd'
226215
},
@@ -236,7 +225,6 @@ tape( 'the function tokenizes a string (extracting width)', function test( t ) {
236225
'flags': '0',
237226
'mapping': void 0,
238227
'width': '5',
239-
'hasPeriod': false,
240228
'precision': void 0,
241229
'specifier': 'd'
242230
},
@@ -260,7 +248,6 @@ tape( 'the function tokenizes a string (extracting precision)', function test( t
260248
'flags': '',
261249
'mapping': void 0,
262250
'width': void 0,
263-
'hasPeriod': true,
264251
'precision': '3',
265252
'specifier': 'f'
266253
}
@@ -275,7 +262,6 @@ tape( 'the function tokenizes a string (extracting precision)', function test( t
275262
'flags': '',
276263
'mapping': void 0,
277264
'width': void 0,
278-
'hasPeriod': true,
279265
'precision': '3',
280266
'specifier': 'g'
281267
}

0 commit comments

Comments
 (0)