Skip to content

Commit 891cee3

Browse files
committed
refactor to use switch
1 parent 0e08c22 commit 891cee3

File tree

1 file changed

+63
-88
lines changed

1 file changed

+63
-88
lines changed

src/mongo_logger.ts

Lines changed: 63 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -468,96 +468,71 @@ export function stringifyWithMaxLen(
468468
// +4 accounts for 2 quotation marks, colon and comma after value
469469
currentLength += key.length + 4;
470470

471-
if (typeof value === 'string') {
472-
// +2 accounts for quotes
473-
currentLength += value.length + 2;
474-
} else if (typeof value === 'number' || typeof value === 'bigint') {
475-
currentLength += String(value).length;
476-
} else if (typeof value === 'boolean') {
477-
currentLength += value ? 4 : 5;
478-
} else if (
479-
value != null &&
480-
typeof value === 'object' &&
481-
'buffer' in value &&
482-
isUint8Array(value.buffer)
483-
) {
484-
// Handle binData
485-
currentLength += (value.buffer.byteLength + value.buffer.byteLength * 0.5) | 0;
486-
} else if (value != null && typeof value === 'object' && '_bsontype' in value) {
487-
const v = value as BSONObject;
488-
if (v._bsontype === 'Binary') {
489-
// '{"$binary":{"base64":"<base64 string>","subType":"XX"}}'
490-
// This is an estimate based on the fact that the base64 is approximately 1.33x the length of
491-
// the actual binary sequence https://en.wikipedia.org/wiki/Base64
492-
currentLength += (22 + value.position + value.position * 0.33 + 18) | 0;
493-
} else if (v._bsontype === 'Code') {
494-
// '{"$code":"<code>"}' or '{"$code":"<code>","$scope":<scope>}'
495-
if (v.scope == null) {
496-
currentLength += v.code.length + 10 + 2;
497-
} else {
498-
// Ignoring actual scope object, so this undercounts
499-
currentLength += v.code.length + 10 + 11;
500-
}
501-
} else if (v._bsontype === 'Decimal128') {
502-
currentLength += value.toExtendedJSON().length;
503-
} else if (v._bsontype === 'Double') {
504-
// Doesn't account for representing integers as <value>.0
505-
if ('value' in v && typeof v.value === 'number') currentLength += String(v.value).length;
506-
} else if (v._bsontype === 'Int32') {
507-
if ('value' in v && typeof v.value === 'number') currentLength += String(v.value).length;
508-
} else if (v._bsontype === 'Long') {
509-
if ('toString' in v && typeof v.toString === 'function') {
510-
currentLength += v.toString().length;
511-
}
512-
} else if (v._bsontype === 'MaxKey' || v._bsontype === 'MinKey') {
513-
// '{"$maxKey":1}' or '{"$minKey":1}'
514-
currentLength += 13;
515-
} else if (v._bsontype === 'ObjectId') {
516-
// '{"$oid":"XXXXXXXXXXXXXXXXXXXXXXXX"}'
517-
currentLength += 35;
518-
} else if (
519-
v._bsontype === 'BSONRegExp' &&
520-
'pattern' in v &&
521-
typeof v.pattern === 'string' &&
522-
'options' in v &&
523-
typeof v.options === 'string'
524-
) {
525-
// '{"$regularExpression":{"pattern":"<pattern>","options":"<options>"}}'
526-
currentLength += 34 + v.pattern.length + 13 + v.options.length + 3;
527-
} else if (v._bsontype === 'BSONSymbol' && 'value' in v && v.value === 'string') {
528-
// '{"$symbol": "<value>"}'
529-
currentLength += 12 + v.value.length + 2;
530-
} else if (
531-
v._bsontype === 'Timestamp' &&
532-
't' in v &&
533-
typeof v.t === 'string' &&
534-
'i' in v &&
535-
typeof v.i === 'string'
536-
) {
537-
currentLength += 19 + String(v.t).length + 5 + String(v.i).length + 2;
538-
} else if (v._bsontype === 'DBRef') {
539-
// TODO: Handle fields property; currently undercounts
540-
// '{"$ref":"<collection>","$id":<stringified oid>}' or '{"$ref":"<collection>","$id":<stringified oid>,"$db":"test"}'
541-
currentLength += 9;
542-
// account for collection
543-
if ('collection' in v) {
544-
currentLength += v.collection.length + 1;
545-
}
546-
547-
// account for db if present
548-
if ('db' in v && typeof v.db === 'string') {
549-
currentLength += 8 + v.db.length + 2;
550-
}
551-
552-
// account for oid if present
553-
if ('oid' in v) {
554-
currentLength += 35;
471+
if (value == null) return value;
472+
473+
switch (typeof value) {
474+
case 'string':
475+
// +2 accounts for quotes
476+
currentLength += value.length + 2;
477+
break;
478+
case 'number':
479+
case 'bigint':
480+
currentLength += String(value).length;
481+
break;
482+
case 'boolean':
483+
currentLength += value ? 4 : 5;
484+
break;
485+
case 'object':
486+
if ('buffer' in value && isUint8Array(value.buffer)) {
487+
// Handle binData
488+
currentLength += (value.buffer.byteLength + value.buffer.byteLength * 0.5) | 0;
489+
} else if ('_bsontype' in value) {
490+
const v = value as BSONObject;
491+
switch (v._bsontype) {
492+
case 'Int32':
493+
currentLength += String(v.value).length;
494+
break;
495+
case 'Double':
496+
// Doesn't account for representing integers as <value>.0
497+
currentLength += String(v.value).length;
498+
break;
499+
case 'Long':
500+
currentLength += v.toString().length;
501+
break;
502+
case 'ObjectId':
503+
// '{"$oid":"XXXXXXXXXXXXXXXXXXXXXXXX"}'
504+
currentLength += 35;
505+
break;
506+
case 'MaxKey':
507+
case 'MinKey':
508+
// '{"$maxKey":1}' or '{"$minKey":1}'
509+
currentLength += 13;
510+
break;
511+
case 'Binary':
512+
// '{"$binary":{"base64":"<base64 string>","subType":"XX"}}'
513+
// This is an estimate based on the fact that the base64 is approximately 1.33x the length of
514+
// the actual binary sequence https://en.wikipedia.org/wiki/Base64
515+
currentLength += (22 + value.position + value.position * 0.33 + 18) | 0;
516+
break;
517+
case 'Timestamp':
518+
currentLength += 19 + String(v.t).length + 5 + String(v.i).length + 2;
519+
break;
520+
case 'Code':
521+
// '{"$code":"<code>"}' or '{"$code":"<code>","$scope":<scope>}'
522+
if (v.scope == null) {
523+
currentLength += v.code.length + 10 + 2;
524+
} else {
525+
// Ignoring actual scope object, so this undercounts by a significant amount
526+
currentLength += v.code.length + 10 + 11;
527+
}
528+
break;
529+
case 'BSONRegExp':
530+
// '{"$regularExpression":{"pattern":"<pattern>","options":"<options>"}}'
531+
currentLength += 34 + v.pattern.length + 13 + v.options.length + 3;
532+
break;
533+
}
555534
}
556-
} else {
557-
// Unknown BSON type, handle same as plain objects
558-
}
559535
}
560-
561536
return value;
562537
};
563538

0 commit comments

Comments
 (0)