Skip to content

Commit 6df3b4e

Browse files
committed
style: 💄 run Prettier
1 parent d5e51aa commit 6df3b4e

File tree

8 files changed

+168
-101
lines changed

8 files changed

+168
-101
lines changed

src/ejson/EjsonDecoder.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ export class EjsonDecoder extends JsonDecoder {
6161
}
6262
}
6363

64-
65-
6664
public readArr(): unknown[] {
6765
const reader = this.reader;
6866
if (reader.u8() !== 0x5b /* [ */) throw new Error('Invalid JSON');
@@ -105,7 +103,7 @@ export class EjsonDecoder extends JsonDecoder {
105103
this.skipWhitespace();
106104
if (reader.u8() !== 0x3a /* : */) throw new Error('Invalid JSON');
107105
this.skipWhitespace();
108-
106+
109107
// For EJSON type wrapper detection, we need to read nested objects as raw first
110108
obj[key] = this.readValue();
111109
first = false;
@@ -170,12 +168,12 @@ export class EjsonDecoder extends JsonDecoder {
170168
// Helper function to validate exact key match
171169
const hasExactKeys = (expectedKeys: string[]): boolean => {
172170
if (keys.length !== expectedKeys.length) return false;
173-
return expectedKeys.every(key => keys.includes(key));
171+
return expectedKeys.every((key) => keys.includes(key));
174172
};
175173

176174
// Check if object has any special $ keys that indicate a type wrapper
177-
const specialKeys = keys.filter(key => key.startsWith('$'));
178-
175+
const specialKeys = keys.filter((key) => key.startsWith('$'));
176+
179177
if (specialKeys.length > 0) {
180178
// ObjectId
181179
if (specialKeys.includes('$oid')) {
@@ -303,7 +301,10 @@ export class EjsonDecoder extends JsonDecoder {
303301
const code = obj.$code as string;
304302
const scope = obj.$scope;
305303
if (typeof code === 'string' && typeof scope === 'object' && scope !== null) {
306-
return new BsonJavascriptCodeWithScope(code, this.transformEjsonObject(scope as Record<string, unknown>) as Record<string, unknown>);
304+
return new BsonJavascriptCodeWithScope(
305+
code,
306+
this.transformEjsonObject(scope as Record<string, unknown>) as Record<string, unknown>,
307+
);
307308
}
308309
throw new Error('Invalid CodeWScope format');
309310
}
@@ -445,31 +446,31 @@ export class EjsonDecoder extends JsonDecoder {
445446
const ref = obj.$ref as string;
446447
const id = this.transformEjsonObject(obj.$id as Record<string, unknown>);
447448
const result: Record<string, unknown> = {$ref: ref, $id: id};
448-
449+
449450
if (keys.includes('$db')) {
450451
result.$db = obj.$db;
451452
}
452-
453+
453454
// Add any other fields
454455
for (const key of keys) {
455456
if (key !== '$ref' && key !== '$id' && key !== '$db') {
456457
result[key] = this.transformEjsonObject(obj[key] as Record<string, unknown>);
457458
}
458459
}
459-
460+
460461
return result;
461462
}
462463

463-
// Regular object - transform all properties
464+
// Regular object - transform all properties
464465
const result: Record<string, unknown> = {};
465466
for (const [key, val] of Object.entries(obj)) {
466467
if (typeof val === 'object' && val !== null && !Array.isArray(val)) {
467468
result[key] = this.transformEjsonObject(val as Record<string, unknown>);
468469
} else if (Array.isArray(val)) {
469-
result[key] = val.map(item =>
470-
typeof item === 'object' && item !== null && !Array.isArray(item)
470+
result[key] = val.map((item) =>
471+
typeof item === 'object' && item !== null && !Array.isArray(item)
471472
? this.transformEjsonObject(item as Record<string, unknown>)
472-
: item
473+
: item,
473474
);
474475
} else {
475476
result[key] = val;
@@ -512,4 +513,4 @@ export class EjsonDecoder extends JsonDecoder {
512513
}
513514
return bytes;
514515
}
515-
}
516+
}

src/ejson/EjsonEncoder.ts

Lines changed: 86 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface EjsonEncoderOptions {
2727
export class EjsonEncoder extends JsonEncoder {
2828
constructor(
2929
writer: IWriter & IWriterGrowable,
30-
private options: EjsonEncoderOptions = {}
30+
private options: EjsonEncoderOptions = {},
3131
) {
3232
super(writer);
3333
}
@@ -256,7 +256,9 @@ export class EjsonEncoder extends JsonEncoder {
256256
const writer = this.writer;
257257
writer.ensureCapacity(18);
258258
writer.u8(0x7b); // {
259-
writer.u32(0x2224756e); writer.u32(0x64656669); writer.u32(0x6e656422); // "$undefined"
259+
writer.u32(0x2224756e);
260+
writer.u32(0x64656669);
261+
writer.u32(0x6e656422); // "$undefined"
260262
writer.u8(0x3a); // :
261263
writer.u32(0x74727565); // true
262264
writer.u8(0x7d); // }
@@ -288,7 +290,9 @@ export class EjsonEncoder extends JsonEncoder {
288290
// Write {"$numberInt":"value"}
289291
const writer = this.writer;
290292
writer.u8(0x7b); // {
291-
writer.u32(0x22246e75); writer.u32(0x6d626572); writer.u32(0x496e7422); // "$numberInt"
293+
writer.u32(0x22246e75);
294+
writer.u32(0x6d626572);
295+
writer.u32(0x496e7422); // "$numberInt"
292296
writer.u8(0x3a); // :
293297
this.writeStr(value + '');
294298
writer.u8(0x7d); // }
@@ -298,7 +302,10 @@ export class EjsonEncoder extends JsonEncoder {
298302
// Write {"$numberLong":"value"}
299303
const writer = this.writer;
300304
writer.u8(0x7b); // {
301-
writer.u32(0x22246e75); writer.u32(0x6d626572); writer.u32(0x4c6f6e67); writer.u16(0x223a); // "$numberLong":
305+
writer.u32(0x22246e75);
306+
writer.u32(0x6d626572);
307+
writer.u32(0x4c6f6e67);
308+
writer.u16(0x223a); // "$numberLong":
302309
this.writeStr(value + '');
303310
writer.u8(0x7d); // }
304311
}
@@ -307,7 +314,11 @@ export class EjsonEncoder extends JsonEncoder {
307314
// Write {"$numberDouble":"value"}
308315
const writer = this.writer;
309316
writer.u8(0x7b); // {
310-
writer.u32(0x22246e75); writer.u32(0x6d626572); writer.u32(0x446f7562); writer.u16(0x6c65); writer.u16(0x223a); // "$numberDouble":
317+
writer.u32(0x22246e75);
318+
writer.u32(0x6d626572);
319+
writer.u32(0x446f7562);
320+
writer.u16(0x6c65);
321+
writer.u16(0x223a); // "$numberDouble":
311322
if (!isFinite(value)) {
312323
this.writeStr(this.formatNonFinite(value));
313324
} else {
@@ -322,15 +333,20 @@ export class EjsonEncoder extends JsonEncoder {
322333
if (isNaN(timestamp)) {
323334
throw new Error('Invalid Date');
324335
}
325-
336+
326337
const writer = this.writer;
327338
writer.u8(0x7b); // {
328-
writer.u32(0x22246461); writer.u16(0x7465); writer.u16(0x223a); // "$date":
329-
339+
writer.u32(0x22246461);
340+
writer.u16(0x7465);
341+
writer.u16(0x223a); // "$date":
342+
330343
if (this.options.canonical) {
331344
// Write {"$numberLong":"timestamp"}
332345
writer.u8(0x7b); // {
333-
writer.u32(0x22246e75); writer.u32(0x6d626572); writer.u32(0x4c6f6e67); writer.u16(0x223a); // "$numberLong":
346+
writer.u32(0x22246e75);
347+
writer.u32(0x6d626572);
348+
writer.u32(0x4c6f6e67);
349+
writer.u16(0x223a); // "$numberLong":
334350
this.writeStr(timestamp + '');
335351
writer.u8(0x7d); // }
336352
} else {
@@ -341,7 +357,10 @@ export class EjsonEncoder extends JsonEncoder {
341357
} else {
342358
// Write {"$numberLong":"timestamp"}
343359
writer.u8(0x7b); // {
344-
writer.u32(0x22246e75); writer.u32(0x6d626572); writer.u32(0x4c6f6e67); writer.u16(0x223a); // "$numberLong":
360+
writer.u32(0x22246e75);
361+
writer.u32(0x6d626572);
362+
writer.u32(0x4c6f6e67);
363+
writer.u16(0x223a); // "$numberLong":
345364
this.writeStr(timestamp + '');
346365
writer.u8(0x7d); // }
347366
}
@@ -353,12 +372,20 @@ export class EjsonEncoder extends JsonEncoder {
353372
// Write {"$regularExpression":{"pattern":"...","options":"..."}}
354373
const writer = this.writer;
355374
writer.u8(0x7b); // {
356-
writer.u32(0x22247265); writer.u32(0x67756c61); writer.u32(0x72457870); writer.u32(0x72657373); writer.u32(0x696f6e22); // "$regularExpression"
375+
writer.u32(0x22247265);
376+
writer.u32(0x67756c61);
377+
writer.u32(0x72457870);
378+
writer.u32(0x72657373);
379+
writer.u32(0x696f6e22); // "$regularExpression"
357380
writer.u16(0x3a7b); // :{
358-
writer.u32(0x22706174); writer.u32(0x7465726e); writer.u16(0x223a); // "pattern":
381+
writer.u32(0x22706174);
382+
writer.u32(0x7465726e);
383+
writer.u16(0x223a); // "pattern":
359384
this.writeStr(value.source);
360385
writer.u8(0x2c); // ,
361-
writer.u32(0x226f7074); writer.u32(0x696f6e73); writer.u16(0x223a); // "options":
386+
writer.u32(0x226f7074);
387+
writer.u32(0x696f6e73);
388+
writer.u16(0x223a); // "options":
362389
this.writeStr(value.flags);
363390
writer.u16(0x7d7d); // }}
364391
}
@@ -367,7 +394,8 @@ export class EjsonEncoder extends JsonEncoder {
367394
// Write {"$oid":"hexstring"}
368395
const writer = this.writer;
369396
writer.u8(0x7b); // {
370-
writer.u32(0x22246f69); writer.u16(0x6422); // "$oid"
397+
writer.u32(0x22246f69);
398+
writer.u16(0x6422); // "$oid"
371399
writer.u8(0x3a); // :
372400
this.writeStr(this.objectIdToHex(value));
373401
writer.u8(0x7d); // }
@@ -405,7 +433,10 @@ export class EjsonEncoder extends JsonEncoder {
405433
// Write {"$numberDecimal":"..."}
406434
const writer = this.writer;
407435
writer.u8(0x7b); // {
408-
writer.u32(0x22246e75); writer.u32(0x6d626572); writer.u32(0x44656369); writer.u32(0x6d616c22); // "$numberDecimal"
436+
writer.u32(0x22246e75);
437+
writer.u32(0x6d626572);
438+
writer.u32(0x44656369);
439+
writer.u32(0x6d616c22); // "$numberDecimal"
409440
writer.u8(0x3a); // :
410441
this.writeStr(this.decimal128ToString(value.data));
411442
writer.u8(0x7d); // }
@@ -415,13 +446,18 @@ export class EjsonEncoder extends JsonEncoder {
415446
// Write {"$binary":{"base64":"...","subType":"..."}}
416447
const writer = this.writer;
417448
writer.u8(0x7b); // {
418-
writer.u32(0x22246269); writer.u32(0x6e617279); writer.u16(0x223a); // "$binary":
449+
writer.u32(0x22246269);
450+
writer.u32(0x6e617279);
451+
writer.u16(0x223a); // "$binary":
419452
writer.u8(0x7b); // {
420-
writer.u32(0x22626173); writer.u32(0x65363422); // "base64"
453+
writer.u32(0x22626173);
454+
writer.u32(0x65363422); // "base64"
421455
writer.u8(0x3a); // :
422456
this.writeStr(this.uint8ArrayToBase64(value.data));
423457
writer.u8(0x2c); // ,
424-
writer.u32(0x22737562); writer.u32(0x54797065); writer.u16(0x223a); // "subType":
458+
writer.u32(0x22737562);
459+
writer.u32(0x54797065);
460+
writer.u16(0x223a); // "subType":
425461
this.writeStr(value.subtype.toString(16).padStart(2, '0'));
426462
writer.u16(0x7d7d); // }}
427463
}
@@ -430,7 +466,9 @@ export class EjsonEncoder extends JsonEncoder {
430466
// Write {"$code":"..."}
431467
const writer = this.writer;
432468
writer.u8(0x7b); // {
433-
writer.u32(0x2224636f); writer.u16(0x6465); writer.u16(0x223a); // "$code":
469+
writer.u32(0x2224636f);
470+
writer.u16(0x6465);
471+
writer.u16(0x223a); // "$code":
434472
this.writeStr(value.code);
435473
writer.u8(0x7d); // }
436474
}
@@ -439,10 +477,13 @@ export class EjsonEncoder extends JsonEncoder {
439477
// Write {"$code":"...","$scope":{...}}
440478
const writer = this.writer;
441479
writer.u8(0x7b); // {
442-
writer.u32(0x2224636f); writer.u16(0x6465); writer.u16(0x223a); // "$code":
480+
writer.u32(0x2224636f);
481+
writer.u16(0x6465);
482+
writer.u16(0x223a); // "$code":
443483
this.writeStr(value.code);
444484
writer.u8(0x2c); // ,
445-
writer.u32(0x22247363); writer.u32(0x6f706522); // "$scope"
485+
writer.u32(0x22247363);
486+
writer.u32(0x6f706522); // "$scope"
446487
writer.u8(0x3a); // :
447488
this.writeAny(value.scope);
448489
writer.u8(0x7d); // }
@@ -452,7 +493,9 @@ export class EjsonEncoder extends JsonEncoder {
452493
// Write {"$symbol":"..."}
453494
const writer = this.writer;
454495
writer.u8(0x7b); // {
455-
writer.u32(0x22247379); writer.u32(0x6d626f6c); writer.u16(0x223a); // "$symbol":
496+
writer.u32(0x22247379);
497+
writer.u32(0x6d626f6c);
498+
writer.u16(0x223a); // "$symbol":
456499
this.writeStr(value.symbol);
457500
writer.u8(0x7d); // }
458501
}
@@ -461,12 +504,16 @@ export class EjsonEncoder extends JsonEncoder {
461504
// Write {"$timestamp":{"t":...,"i":...}}
462505
const writer = this.writer;
463506
writer.u8(0x7b); // {
464-
writer.u32(0x22247469); writer.u32(0x6d657374); writer.u32(0x616d7022); // "$timestamp"
507+
writer.u32(0x22247469);
508+
writer.u32(0x6d657374);
509+
writer.u32(0x616d7022); // "$timestamp"
465510
writer.u16(0x3a7b); // :{
466-
writer.u16(0x2274); writer.u16(0x223a); // "t":
511+
writer.u16(0x2274);
512+
writer.u16(0x223a); // "t":
467513
this.writeNumber(value.timestamp);
468514
writer.u8(0x2c); // ,
469-
writer.u16(0x2269); writer.u16(0x223a); // "i":
515+
writer.u16(0x2269);
516+
writer.u16(0x223a); // "i":
470517
this.writeNumber(value.increment);
471518
writer.u16(0x7d7d); // }}
472519
}
@@ -475,13 +522,17 @@ export class EjsonEncoder extends JsonEncoder {
475522
// Write {"$dbPointer":{"$ref":"...","$id":{...}}}
476523
const writer = this.writer;
477524
writer.u8(0x7b); // {
478-
writer.u32(0x22246462); writer.u32(0x506f696e); writer.u32(0x74657222); // "$dbPointer"
525+
writer.u32(0x22246462);
526+
writer.u32(0x506f696e);
527+
writer.u32(0x74657222); // "$dbPointer"
479528
writer.u16(0x3a7b); // :{
480-
writer.u32(0x22247265); writer.u16(0x6622); // "$ref"
529+
writer.u32(0x22247265);
530+
writer.u16(0x6622); // "$ref"
481531
writer.u8(0x3a); // :
482532
this.writeStr(value.name);
483533
writer.u8(0x2c); // ,
484-
writer.u32(0x22246964); writer.u16(0x223a); // "$id":
534+
writer.u32(0x22246964);
535+
writer.u16(0x223a); // "$id":
485536
this.writeAny(value.id);
486537
writer.u16(0x7d7d); // }}
487538
}
@@ -490,7 +541,9 @@ export class EjsonEncoder extends JsonEncoder {
490541
// Write {"$minKey":1}
491542
const writer = this.writer;
492543
writer.u8(0x7b); // {
493-
writer.u32(0x22246d69); writer.u32(0x6e4b6579); writer.u16(0x223a); // "$minKey":
544+
writer.u32(0x22246d69);
545+
writer.u32(0x6e4b6579);
546+
writer.u16(0x223a); // "$minKey":
494547
this.writeNumber(1);
495548
writer.u8(0x7d); // }
496549
}
@@ -499,7 +552,9 @@ export class EjsonEncoder extends JsonEncoder {
499552
// Write {"$maxKey":1}
500553
const writer = this.writer;
501554
writer.u8(0x7b); // {
502-
writer.u32(0x22246d61); writer.u32(0x784b6579); writer.u16(0x223a); // "$maxKey":
555+
writer.u32(0x22246d61);
556+
writer.u32(0x784b6579);
557+
writer.u16(0x223a); // "$maxKey":
503558
this.writeNumber(1);
504559
writer.u8(0x7d); // }
505560
}
@@ -535,4 +590,4 @@ export class EjsonEncoder extends JsonEncoder {
535590
// For now, return a placeholder that indicates the format
536591
return '0'; // TODO: Implement proper decimal128 to string conversion
537592
}
538-
}
593+
}

0 commit comments

Comments
 (0)