[BSON] add types for missing calculateObjectSize method (#24901)

This commit is contained in:
ramlez
2018-04-11 21:08:06 +02:00
committed by Mohamed Hegazy
parent aca7b39f77
commit 775ade8fa6
2 changed files with 24 additions and 1 deletions

View File

@@ -6,7 +6,7 @@ bson.ObjectID.cacheHexString = true
let BSON = new bson.BSON();
let Long = bson.Long;
let doc = {long: Long.fromNumber(100)}
let doc = { long: Long.fromNumber(100) }
// Serialize a document
let data = BSON.serialize(doc, false, true, false);
@@ -19,3 +19,10 @@ console.log("doc_2:", doc_2);
BSON = new bson.BSON();
data = BSON.serialize(doc);
doc_2 = BSON.deserialize(data);
// Calculate Object Size
BSON = new bson.BSON();
console.log("Calculated Object size - no options object:", BSON.calculateObjectSize(doc));
console.log("Calculated Object size - empty options object:", BSON.calculateObjectSize(doc, {}));
console.log("Calculated Object size - custom options object:", BSON.calculateObjectSize(doc, { ignoreUndefined: false, serializeFunctions: true }));

16
types/bson/index.d.ts vendored
View File

@@ -16,6 +16,14 @@ export interface DeserializeOptions {
/** {Boolean, default:false}, deserialize Binary data directly into node.js Buffer object. */
promoteBuffers?: boolean;
}
export interface CalculateObjectSizeOptions {
/** {Boolean, default:false}, serialize the javascript functions */
serializeFunctions?: boolean;
/** {Boolean, default:true}, ignore undefined fields. */
ignoreUndefined?: boolean;
}
export class BSON {
/**
* @param {Object} object the Javascript object to serialize.
@@ -26,6 +34,14 @@ export class BSON {
*/
serialize(object: any, checkKeys?: boolean, asBuffer?: boolean, serializeFunctions?: boolean): Buffer;
deserialize(buffer: Buffer, options?: DeserializeOptions, isArray?: boolean): any;
/**
* Calculate the bson size for a passed in Javascript object.
*
* @param {Object} object the Javascript object to calculate the BSON byte size for.
* @param {CalculateObjectSizeOptions} Options
* @return {Number} returns the number of bytes the BSON object will take up.
*/
calculateObjectSize(object: any, options?: CalculateObjectSizeOptions): number;
}
export class Binary {