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-4874): support EJSON parse for BigInt from $numberLong #552

Merged
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
75a6207
feat(NODE-4874): WIP
W-A-James Jan 5, 2023
7cd7f6c
test(NODE-4874): Start table test
W-A-James Jan 5, 2023
df97552
fix(NODE-4874): Get canonical mode parsing to work
W-A-James Jan 6, 2023
07904f9
style(NODE-4874): Style
W-A-James Jan 6, 2023
d0dd23d
style(NODE-4874): eslint
W-A-James Jan 6, 2023
5120ca5
fix(NODE-4874): Fix handling of flags
W-A-James Jan 6, 2023
d67757d
test(NODE-4874): Finish tests
W-A-James Jan 6, 2023
4f15e42
style(NODE-4874): fixup condition
W-A-James Jan 6, 2023
2f5db05
fix(NODE-4874): Remove dead code
W-A-James Jan 6, 2023
822dff8
fix(NODE-4874): Add fix and test for double bug
W-A-James Jan 11, 2023
5666255
fix(NODE-4874): Implement requested fixes
W-A-James Jan 11, 2023
79f0c81
test(NODE-4874): Add new table tests
W-A-James Jan 11, 2023
260b667
test(NODE-4874): Add test to check that legacy flag doesn't affect ou…
W-A-James Jan 11, 2023
6a5364a
test(NODE-4874): Add another assertion
W-A-James Jan 11, 2023
7eca3ea
style(NODE-4874): Update comment
W-A-James Jan 11, 2023
f6aed3d
test(NODE-4874): Remove 'only' annotation
W-A-James Jan 11, 2023
b0e67aa
fix(NODE-4874): Update Long.fromExtendedJSON
W-A-James Jan 11, 2023
4361871
test(NODE-4874): Add new tests
W-A-James Jan 11, 2023
92ae357
fix(NODE-4874): Add fixes
W-A-James Jan 12, 2023
fff3d32
test(NODE-4874): Update tests
W-A-James Jan 12, 2023
a86a942
style(NODE-4874): Change variable name
W-A-James Jan 12, 2023
d9f539f
fix(NODE-4874): Fix MAX_INT64_STRING_LENGTH constant
W-A-James Jan 13, 2023
0acf248
test(NODE-4874): Test fixups
W-A-James Jan 17, 2023
cdf0334
fix(NODE-4874): Fix fromExtendedJSON checks and errors
W-A-James Jan 17, 2023
027b5df
fix(NODE-4874): Update fromExtendedJSON
W-A-James Jan 17, 2023
a02236f
test(NODE-4874): New tests
W-A-James Jan 17, 2023
31f8bf7
test(NODE-4874): Add tests for current validation behaviour
W-A-James Jan 18, 2023
f22ce03
fix(NODE-4874): Small style fix
W-A-James Jan 18, 2023
8b0a741
test(NODE-4874): Add comments and fix test value
W-A-James Jan 18, 2023
e0049b3
fix(NODE-4874): Make error messages more verbose
W-A-James Jan 18, 2023
47cdb65
fix(NODE-4874): Fix error messages
W-A-James Jan 18, 2023
d502ddb
Merge branch 'main' into NODE-4874/support_ejson_parse_for_BigInt_fro…
W-A-James Jan 18, 2023
f80b707
fix(NODE-4874): merge conflict fix
W-A-James Jan 18, 2023
c90b783
fix(NODE-4874): Update fromExtendedJSON behaviour to be more consiste…
W-A-James Jan 19, 2023
43b39c1
test(NODE-4874): move EJSON.parse tests back under the correct descri…
W-A-James Jan 19, 2023
b99a141
test(NODE-4874): Unnest EJSON.stringify fron EJSON.parse
W-A-James Jan 19, 2023
a0b5cf0
test(NODE-4874): Remove todo
W-A-James Jan 19, 2023
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
23 changes: 7 additions & 16 deletions src/long.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,6 @@ const UINT_CACHE: { [key: number]: Long } = {};

const MAX_INT64_STRING_LENGTH = 20;

/**
* @internal
* Checks that an int64 string is of the correct length and is represented in
* base 10
*
* Fails on strings longer than MAX_INT64_STRING_LENGTH characters
* Fails on non-decimal strings */
function isInt64StrDecimalWithCorrectLength(input: string): boolean {
if (input.length > MAX_INT64_STRING_LENGTH) {
return false;
}
return /^(\+|-)?(0|[1-9][0-9]*)$/.test(input);
}

/** @public */
export interface LongExtended {
$numberLong: string;
Expand Down Expand Up @@ -1043,9 +1029,14 @@ export class Long {
): number | Long | bigint {
const defaults = { useBigInt64: false, relaxed: true };
const ejsonOptions = { ...defaults, ...options };
const decimalRegEx = /^(\+?0|(\+|-)?[1-9][0-9]*)$/;
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved

if (doc.$numberLong.length > MAX_INT64_STRING_LENGTH) {
throw new BSONError('int64 string is too long');
}

if (!isInt64StrDecimalWithCorrectLength(doc.$numberLong)) {
throw new BSONError('Invalid int64 string');
if (!decimalRegEx.test(doc.$numberLong)) {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
throw new BSONError('int64 string is not in decimal');
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
}

if (ejsonOptions.useBigInt64) {
Expand Down