Skip to content

Commit 63beeea

Browse files
optimize removeLeadingZerosAndExplicitPlus function and fix name typo
1 parent 9f308aa commit 63beeea

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

src/int_32.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { BSON_INT32_MAX, BSON_INT32_MIN } from './constants';
33
import { BSONError } from './error';
44
import type { EJSONOptions } from './extended_json';
55
import { type InspectFn, defaultInspect } from './parser/utils';
6-
import { removeLeadingZerosandExplicitPlus } from './utils/string_utils';
6+
import { removeLeadingZerosAndExplicitPlus } from './utils/string_utils';
77

88
/** @public */
99
export interface Int32Extended {
@@ -49,7 +49,7 @@ export class Int32 extends BSONValue {
4949
* @param value - the string we want to represent as an int32.
5050
*/
5151
static fromString(value: string): Int32 {
52-
const cleanedValue = removeLeadingZerosandExplicitPlus(value);
52+
const cleanedValue = removeLeadingZerosAndExplicitPlus(value);
5353

5454
const coercedValue = Number(value);
5555

src/long.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ export class Long extends BSONValue {
357357
}
358358

359359
// remove leading zeros (for later string comparison and to make math faster)
360-
const cleanedStr = StringUtils.removeLeadingZerosandExplicitPlus(str);
360+
const cleanedStr = StringUtils.removeLeadingZerosAndExplicitPlus(str);
361361

362362
// check roundtrip result
363363
const result = Long._fromString(cleanedStr, unsigned, radix);

src/utils/string_utils.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,32 @@
22
* @internal
33
* Removes leading zeros and explicit plus from textual representation of a number.
44
*/
5-
export function removeLeadingZerosandExplicitPlus(str: string): string {
6-
return !/[^+?0]+/.test(str)
7-
? str.replace(/^\+?0+/, '0') // all zeros case (remove explicit plus if it exists)
8-
: str[0] === '-'
9-
? str.replace(/^-0+/, '-') // negative number with leading zeros
10-
: str.replace(/^\+?0*/, ''); // remove explicit plus
5+
export function removeLeadingZerosAndExplicitPlus(str: string): string {
6+
if (str === '') {
7+
return str;
8+
}
9+
10+
let startIndex = 0;
11+
12+
const isNegative = str[startIndex] === '-';
13+
const isExplicitlyPositive = str[startIndex] === '+';
14+
15+
if (isExplicitlyPositive || isNegative) {
16+
startIndex += 1;
17+
}
18+
19+
let foundInsignificantZero = false;
20+
21+
while (str[startIndex] === '0') {
22+
foundInsignificantZero = true;
23+
startIndex += 1;
24+
}
25+
26+
if (!foundInsignificantZero) {
27+
return isExplicitlyPositive ? str.slice(1) : str;
28+
}
29+
30+
return `${isNegative ? '-' : ''}${str.length === startIndex ? '0' : str.slice(startIndex)}`;
1131
}
1232

1333
/**

test/node/utils/string_utils.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { expect } from 'chai';
12
import * as StringUtils from '../../../src/utils/string_utils';
23

3-
describe('removeLeadingZerosandExplicitPlus()', function () {
4+
describe('removeLeadingZerosAndExplicitPlus()', function () {
45
const inputs: [testName: string, str: string, expectedStr: string][] = [
56
['a string with zero with leading zeros', '000000', '0'],
67
['a string with positive leading zeros', '000000867', '867'],
@@ -14,7 +15,7 @@ describe('removeLeadingZerosandExplicitPlus()', function () {
1415
for (const [testName, str, expectedStr] of inputs) {
1516
context(`when the input is ${testName}`, () => {
1617
it(`should return a input string`, () => {
17-
expect(StringUtils.removeLeadingZerosandExplicitPlus(str)).to.equal(expectedStr);
18+
expect(StringUtils.removeLeadingZerosAndExplicitPlus(str)).to.equal(expectedStr);
1819
});
1920
});
2021
}

0 commit comments

Comments
 (0)