Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-6086): add Double.fromString() method #671

Merged
merged 6 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/double.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import type { EJSONOptions } from './extended_json';
import { type InspectFn, defaultInspect } from './parser/utils';

Expand Down Expand Up @@ -32,6 +33,34 @@ export class Double extends BSONValue {
this.value = +value;
}

/**
* Attempt to create an double type from string.
*
* This method will throw a BSONError on any string input that is not representable as a IEEE-754 64-bit double.
* Notably, this method will also throw on the following string formats:
* - Strings in non-decimal formats (exponent notation, binary, hex, or octal digits)
* - Strings with characters other than numeric, floating point, or leading sign characters (Note: 'Infinity', '-Infinity', and 'NaN' input strings are still allowed)
* - Strings with leading and/or trailing whitespace
*
* Strings with leading zeros, however, are also allowed
*
* @param value - the string we want to represent as an double.
*/
static fromString(value: string): Double {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment here as on the Int32.fromString() PR: can we throw descriptive errors for each case we're checking for in the if statement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, on it

const coercedValue = Number(value);
const nonFiniteValidInputs = ['Infinity', '-Infinity', 'NaN'];
if (
(!Number.isFinite(coercedValue) && !nonFiniteValidInputs.includes(value)) ||
(Number.isNaN(coercedValue) && value !== 'NaN') ||
value === '' ||
(/[^-0-9.]/.test(value) && !nonFiniteValidInputs.includes(value)) ||
value.trim() !== value
) {
throw new BSONError(`Input: '${value}' is not a valid Double string`);
}
return new Double(coercedValue);
}

/**
* Access the number value.
*
Expand Down
53 changes: 53 additions & 0 deletions test/node/double.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,59 @@ describe('BSON Double Precision', function () {
});
});
});

describe('fromString', () => {
const acceptedInputs = [
['zero', '0', 0],
['non-leading zeros', '45000000', 45000000],
['zero with leading zeros', '000000.0000', 0],
['positive leading zeros', '000000867.1', 867.1],
['negative leading zeros', '-00007.980', -7.98],
['positive integer with decimal', '2.0', 2],
['zero with decimal', '0.0', 0.0],
['Infinity', 'Infinity', Infinity],
['-Infinity', '-Infinity', -Infinity],
['NaN', 'NaN', NaN],
['basic floating point', '-4.556000', -4.556],
['negative zero', '-0', -0]
];

const errorInputs = [
['commas', '34,450'],
['exponentiation notation', '1.34e16'],
['octal', '0o1'],
['binary', '0b1'],
['hex', '0x1'],
['empty string', ''],
['leading and trailing whitespace', ' 89 ', 89],
['fake positive infinity', '2e308'],
['fake negative infinity', '-2e308'],
['fraction', '3/4'],
['foo', 'foo']
];

for (const [testName, value, expectedDouble] of acceptedInputs) {
context(`when case is ${testName}`, () => {
it(`should return Double that matches expected value`, () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here about test titles.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just changed them, going to push after my tests run!

if (value === 'NaN') {
expect(isNaN(Double.fromString(value))).to.be.true;
} else {
expect(Double.fromString(value).value).to.equal(expectedDouble);
}
});
});
}
for (const [testName, value] of errorInputs) {
context(`when case is ${testName}`, () => {
it(`should throw correct error`, () => {
expect(() => Double.fromString(value)).to.throw(
BSON.BSONError,
/not a valid Double string/
);
});
});
}
});
});

function serializeThenDeserialize(value) {
Expand Down