Skip to content

Commit da05d20

Browse files
committed
address comments
1 parent e3c5996 commit da05d20

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

src/parser/on_demand/parse_to_elements.ts

+35-18
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { BSONOffsetError } from '../../error';
88
* - `minKey` is set to 255 so unsigned comparisons succeed
99
* - Modify with caution, double check the bundle contains literals
1010
*/
11-
const enum t {
11+
const enum BSONElementType {
1212
double = 1,
1313
string = 2,
1414
object = 3,
@@ -82,8 +82,11 @@ function findNull(bytes: Uint8Array, offset: number): number {
8282
* @public
8383
* @experimental
8484
*/
85-
export function parseToElements(bytes: Uint8Array, pOffset?: number | null): Iterable<BSONElement> {
86-
const startOffset = pOffset ?? 0;
85+
export function parseToElements(
86+
bytes: Uint8Array,
87+
startOffset: number | null = 0
88+
): Iterable<BSONElement> {
89+
startOffset ??= 0;
8790

8891
if (bytes.length < 5) {
8992
throw new BSONOffsetError(
@@ -125,37 +128,51 @@ export function parseToElements(bytes: Uint8Array, pOffset?: number | null): Ite
125128

126129
let length: number;
127130

128-
if (type === t.double || type === t.long || type === t.date || type === t.timestamp) {
131+
if (
132+
type === BSONElementType.double ||
133+
type === BSONElementType.long ||
134+
type === BSONElementType.date ||
135+
type === BSONElementType.timestamp
136+
) {
129137
length = 8;
130-
} else if (type === t.int) {
138+
} else if (type === BSONElementType.int) {
131139
length = 4;
132-
} else if (type === t.objectId) {
140+
} else if (type === BSONElementType.objectId) {
133141
length = 12;
134-
} else if (type === t.decimal) {
142+
} else if (type === BSONElementType.decimal) {
135143
length = 16;
136-
} else if (type === t.bool) {
144+
} else if (type === BSONElementType.bool) {
137145
length = 1;
138-
} else if (type === t.null || type === t.undefined || type === t.maxKey || type === t.minKey) {
146+
} else if (
147+
type === BSONElementType.null ||
148+
type === BSONElementType.undefined ||
149+
type === BSONElementType.maxKey ||
150+
type === BSONElementType.minKey
151+
) {
139152
length = 0;
140153
}
141154
// Needs a size calculation
142-
else if (type === t.regex) {
155+
else if (type === BSONElementType.regex) {
143156
length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
144-
} else if (type === t.object || type === t.array || type === t.javascriptWithScope) {
157+
} else if (
158+
type === BSONElementType.object ||
159+
type === BSONElementType.array ||
160+
type === BSONElementType.javascriptWithScope
161+
) {
145162
length = getSize(bytes, offset);
146163
} else if (
147-
type === t.string ||
148-
type === t.binData ||
149-
type === t.dbPointer ||
150-
type === t.javascript ||
151-
type === t.symbol
164+
type === BSONElementType.string ||
165+
type === BSONElementType.binData ||
166+
type === BSONElementType.dbPointer ||
167+
type === BSONElementType.javascript ||
168+
type === BSONElementType.symbol
152169
) {
153170
length = getSize(bytes, offset) + 4;
154-
if (type === t.binData) {
171+
if (type === BSONElementType.binData) {
155172
// binary subtype
156173
length += 1;
157174
}
158-
if (type === t.dbPointer) {
175+
if (type === BSONElementType.dbPointer) {
159176
// dbPointer's objectId
160177
length += 12;
161178
}

src/parser/on_demand/parse_to_structure.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -94,34 +94,36 @@ export function parseToStructure<
9494
};
9595

9696
/** BSONElement offsets: type indicator and value offset */
97-
const enum e {
97+
const enum BSONElementOffset {
9898
type = 0,
9999
offset = 3
100100
}
101101

102102
/** BSON Embedded types */
103-
const enum t {
103+
const enum BSONElementType {
104104
object = 3,
105105
array = 4,
106106
javascriptWithScope = 15
107107
}
108108

109109
embedded: while (ctx !== null) {
110110
for (
111-
let it: BSONElement | undefined = ctx.elements[ctx.elementOffset++];
112-
it != null;
113-
it = ctx.elements[ctx.elementOffset++]
111+
let bsonElement: BSONElement | undefined = ctx.elements[ctx.elementOffset++];
112+
bsonElement != null;
113+
bsonElement = ctx.elements[ctx.elementOffset++]
114114
) {
115-
const type = it[e.type];
116-
const offset = it[e.offset];
115+
const type = bsonElement[BSONElementOffset.type];
116+
const offset = bsonElement[BSONElementOffset.offset];
117117

118-
const container = reviver(bytes, ctx.container, it);
118+
const container = reviver(bytes, ctx.container, bsonElement);
119119
const isEmbeddedType =
120-
type === t.object || type === t.array || type === t.javascriptWithScope;
120+
type === BSONElementType.object ||
121+
type === BSONElementType.array ||
122+
type === BSONElementType.javascriptWithScope;
121123

122124
if (container != null && isEmbeddedType) {
123125
const docOffset: number =
124-
type !== t.javascriptWithScope
126+
type !== BSONElementType.javascriptWithScope
125127
? offset
126128
: // value offset + codeSize + value int + code int
127129
offset + getSize(bytes, offset + 4) + 4 + 4;

0 commit comments

Comments
 (0)