mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-11 10:59:55 +08:00
[BSON] add types for missing calculateObjectSize method (#24901)
This commit is contained in:
@@ -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
16
types/bson/index.d.ts
vendored
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user