Skip to content

Commit 1ead83d

Browse files
requested changes
1 parent 9e90f06 commit 1ead83d

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/long.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,11 @@ export class Long extends BSONValue {
252252
* @param str - The textual representation of the Long
253253
* @param radix - The radix in which the text is written (2-36), defaults to 10
254254
*/
255-
static validateStringCharacters(str: string, radix?: number): false | string {
255+
private static validateStringCharacters(str: string, radix?: number): false | string {
256256
radix = radix ?? 10;
257-
258-
let regexInputString = '';
259-
if (radix <= 10) {
260-
regexInputString = `[^-0-${radix - 1}+]`;
261-
} else {
262-
const validCharRangeEnd = String.fromCharCode('a'.charCodeAt(0) + (radix - 11));
263-
regexInputString = `[^-0-9+(a-${validCharRangeEnd})]`;
264-
}
265-
const regex = new RegExp(regexInputString, 'i');
257+
const validCharacters = '0123456789abcdefghijklmnopqrstuvwxyz'.slice(0, radix);
258+
// regex is case insensitive and checks that each character within the string is one of the validCharacters
259+
const regex = new RegExp(`[^-+${validCharacters}]`, 'i');
266260
return regex.test(str) ? false : str;
267261
}
268262

@@ -273,16 +267,16 @@ export class Long extends BSONValue {
273267
* - the string contains invalid characters for the given radix
274268
* - the string contains whitespace
275269
* @param str - The textual representation of the Long
270+
* @param validateStringCharacters - Whether or not invalid characters should throw an error
276271
* @param unsigned - Whether unsigned or not, defaults to signed
277272
* @param radix - The radix in which the text is written (2-36), defaults to 10
278-
* @param throwsError - Whether or not throwing an error is permitted
279273
* @returns The corresponding Long value
280274
*/
281-
static fromStringHelper(
275+
private static _fromString(
282276
str: string,
277+
validateStringCharacters: boolean,
283278
unsigned?: boolean,
284279
radix?: number,
285-
throwsError?: boolean
286280
): Long {
287281
if (str.length === 0) throw new BSONError('empty string');
288282
if (str === 'NaN' || str === 'Infinity' || str === '+Infinity' || str === '-Infinity')
@@ -297,17 +291,17 @@ export class Long extends BSONValue {
297291

298292
if (radix < 2 || 36 < radix) throw new BSONError('radix');
299293

300-
if (throwsError && !Long.validateStringCharacters(str, radix)) {
294+
if (validateStringCharacters && !Long.validateStringCharacters(str, radix)) {
301295
throw new BSONError(`Input: '${str}' contains invalid characters for radix: ${radix}`);
302296
}
303-
if (throwsError && str.trim() !== str) {
304-
throw new BSONError(`Input: '${str}' contains whitespace.`);
297+
if (validateStringCharacters && str.trim() !== str) {
298+
throw new BSONError(`Input: '${str}' contains leading and/or trailing whitespace.`);
305299
}
306300

307301
let p;
308302
if ((p = str.indexOf('-')) > 0) throw new BSONError('interior hyphen');
309303
else if (p === 0) {
310-
return Long.fromStringHelper(str.substring(1), unsigned, radix, throwsError).neg();
304+
return Long._fromString(str.substring(1), validateStringCharacters, unsigned, radix).neg();
311305
}
312306

313307
// Do several (8) digits each time through the loop, so as to
@@ -345,7 +339,7 @@ export class Long extends BSONValue {
345339
// remove leading zeros (for later string comparison and to make math faster)
346340
const cleanedStr = removeLeadingZeros(str);
347341
// doing this check outside of recursive function so cleanedStr value is consistent
348-
const result = Long.fromStringHelper(cleanedStr, unsigned, radix, true);
342+
const result = Long._fromString(cleanedStr, true, unsigned, radix);
349343
if (result.toString(radix).toLowerCase() !== cleanedStr.toLowerCase()) {
350344
throw new BSONError(
351345
`Input: ${str} is not representable as ${result.unsigned ? 'an unsigned' : 'a signed'} 64-bit Long with radix: ${radix}`
@@ -362,7 +356,7 @@ export class Long extends BSONValue {
362356
* @returns The corresponding Long value
363357
*/
364358
static fromString(str: string, unsigned?: boolean, radix?: number): Long {
365-
return Long.fromStringHelper(str, unsigned, radix, false);
359+
return Long._fromString(str, false, unsigned, radix);
366360
}
367361

368362
/**

test/node/long.test.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,13 @@ describe('Long', function () {
165165
});
166166

167167
describe('static fromStringStrict()', function () {
168-
const successInputs = [
168+
const successInputs: [
169+
name: string,
170+
input: string,
171+
unsigned: boolean,
172+
radix: number,
173+
expectedStr?: string
174+
][] = [
169175
['basic no alphabet low radix', '1236', true, 8],
170176
['negative basic no alphabet low radix', '-1236', false, 8],
171177
['valid upper and lower case letters in string with radix > 10', 'eEe', true, 15],
@@ -187,12 +193,12 @@ describe('Long', function () {
187193
['unsigned zero', '0', true, 10]
188194
];
189195

190-
const failureInputs = [
196+
const failureInputs: [name: string, input: string, unsigned: boolean, radix: number][] = [
191197
['empty string', '', true, 2],
192198
['invalid numbers in binary string', '234', true, 2],
193199
['non a-z or numeric string', '~~', true, 36],
194200
['alphabet in radix < 10', 'a', true, 9],
195-
['radix does not allow all alphabet letters', 'eee', 14],
201+
['radix does not allow all alphabet letters', 'eee', false, 14],
196202
['over max unsigned binary input', Long.MAX_UNSIGNED_VALUE.toString(2) + '1', true, 2],
197203
['over max unsigned decimal input', Long.MAX_UNSIGNED_VALUE.toString(10) + '1', true, 10],
198204
['over max unsigned hex input', Long.MAX_UNSIGNED_VALUE.toString(16) + '1', true, 16],
@@ -227,7 +233,8 @@ describe('Long', function () {
227233
const successInputs = [
228234
['radix does allows given alphabet letter', 'eEe', 15],
229235
['empty string', '', 2],
230-
['all possible hexadecimal characters', '12efabc689873dADCDEF', 16]
236+
['all possible hexadecimal characters', '12efabc689873dADCDEF', 16],
237+
['leading zeros', '0000000004567e', 16]
231238
];
232239

233240
const failureInputs = [

0 commit comments

Comments
 (0)