@@ -8,7 +8,7 @@ import { BSONOffsetError } from '../../error';
8
8
* - `minKey` is set to 255 so unsigned comparisons succeed
9
9
* - Modify with caution, double check the bundle contains literals
10
10
*/
11
- const enum t {
11
+ const enum BSONElementType {
12
12
double = 1 ,
13
13
string = 2 ,
14
14
object = 3 ,
@@ -82,8 +82,11 @@ function findNull(bytes: Uint8Array, offset: number): number {
82
82
* @public
83
83
* @experimental
84
84
*/
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 ;
87
90
88
91
if ( bytes . length < 5 ) {
89
92
throw new BSONOffsetError (
@@ -125,37 +128,51 @@ export function parseToElements(bytes: Uint8Array, pOffset?: number | null): Ite
125
128
126
129
let length : number ;
127
130
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
+ ) {
129
137
length = 8 ;
130
- } else if ( type === t . int ) {
138
+ } else if ( type === BSONElementType . int ) {
131
139
length = 4 ;
132
- } else if ( type === t . objectId ) {
140
+ } else if ( type === BSONElementType . objectId ) {
133
141
length = 12 ;
134
- } else if ( type === t . decimal ) {
142
+ } else if ( type === BSONElementType . decimal ) {
135
143
length = 16 ;
136
- } else if ( type === t . bool ) {
144
+ } else if ( type === BSONElementType . bool ) {
137
145
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
+ ) {
139
152
length = 0 ;
140
153
}
141
154
// Needs a size calculation
142
- else if ( type === t . regex ) {
155
+ else if ( type === BSONElementType . regex ) {
143
156
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
+ ) {
145
162
length = getSize ( bytes , offset ) ;
146
163
} 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
152
169
) {
153
170
length = getSize ( bytes , offset ) + 4 ;
154
- if ( type === t . binData ) {
171
+ if ( type === BSONElementType . binData ) {
155
172
// binary subtype
156
173
length += 1 ;
157
174
}
158
- if ( type === t . dbPointer ) {
175
+ if ( type === BSONElementType . dbPointer ) {
159
176
// dbPointer's objectId
160
177
length += 12 ;
161
178
}
0 commit comments