From ed33cc962bb21fe03d6b05f0660f59ecaa2766e4 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 24 Aug 2021 17:44:28 +0200 Subject: [PATCH 1/3] Added failing tests This adds type tests for calling ObjectID without new --- typing-tests/objectid-tests.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/typing-tests/objectid-tests.ts b/typing-tests/objectid-tests.ts index 8188b8d..25c1c8f 100644 --- a/typing-tests/objectid-tests.ts +++ b/typing-tests/objectid-tests.ts @@ -50,6 +50,38 @@ oid = ObjectID.createFromTime(time); // should construct with `ObjectID.createFromHexString(hexString)` oid = ObjectID.createFromHexString(hexString); +// ---------------------------------------------------------------------------- +// should construct with no arguments +oid = ObjectID(); + +// ---------------------------------------------------------------------------- +// should have an `id` property +oid.id; + +// ---------------------------------------------------------------------------- +// should have a `str` property +oid.str; + +// ---------------------------------------------------------------------------- +// should construct with a `time` argument +oid = ObjectID(time); + +// ---------------------------------------------------------------------------- +// should construct with an `array` argument +oid = ObjectID(array); + +// ---------------------------------------------------------------------------- +// should construct with a `buffer` argument +oid = ObjectID(buffer); + +// ---------------------------------------------------------------------------- +// should construct with a `hexString` argument +oid = ObjectID(hexString); + +// ---------------------------------------------------------------------------- +// should construct with a `idString` argument +oid = ObjectID(idString); + // ---------------------------------------------------------------------------- // should correctly retrieve timestamp const timestamp:number = oid.getTimestamp(); From d7d3c5fbf5df34922675ac0fc32d4b5fb29c9580 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 24 Aug 2021 17:45:21 +0200 Subject: [PATCH 2/3] Update types to handle calling ObjectID without new We use interfaces here to allow us to define an object as both "newable" and callable. We have one interface for the class constructor, this defines how to call it (new & as a function) as well as the static methods. We then have another interface for the instance, this defines the members and methods. We then have to export a const ObjectID so that typescript knows we are dealing with values, rather than types. ref https://discord.com/channels/508357248330760243/765583369345302570/879753593320796230 --- objectid.d.ts | 66 +++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/objectid.d.ts b/objectid.d.ts index f164893..5933101 100644 --- a/objectid.d.ts +++ b/objectid.d.ts @@ -1,29 +1,39 @@ -// Type definitions for bson-objectid 1.3.1 -// Project: bson-objectid -// Definitions by: Marcel Ernst -import { Buffer } from 'buffer'; -export default ObjectID; - -declare class ObjectID { - static createFromTime(time: number): ObjectID; - static createFromHexString(hexString: string): ObjectID; - static isValid(hexString: string):boolean; - static isValid(ObjectID: ObjectID):boolean; - static generate(): string; - static generate(time: number): string; - static toString():string; - - constructor(); - constructor(time: number); - constructor(hexString: string); - constructor(idString: string); - constructor(array: number[]); - constructor(buffer: Buffer); - - readonly id: string; - readonly str: string; - - toHexString(): string; - equals(other: ObjectID): boolean; - getTimestamp(): number; +import {Buffer} from 'buffer'; + +export default ObjectID + +declare const ObjectID: ObjectIDCtor; + +declare interface ObjectID { + readonly id: string; + readonly str: string; + + toHexString(): string; + equals(other: ObjectID): boolean; + getTimestamp(): number; +} + +declare interface ObjectIDCtor { + (): ObjectID + (time: number): ObjectID + (hexString: string): ObjectID + (idString: string): ObjectID + (array: number[]): ObjectID + (buffer: Buffer): ObjectID + + new(): ObjectID + new(time: number): ObjectID + new(hexString: string): ObjectID + new(idString: string): ObjectID + new(array: number[]): ObjectID + new(buffer: Buffer): ObjectID + + + createFromTime(time: number): ObjectID; + createFromHexString(hexString: string): ObjectID; + isValid(hexString: string): boolean; + isValid(ObjectID: ObjectID): boolean; + generate(): string; + generate(time: number): string; + toString(): string; } From cad382ef87e3fdcce6da950e9d6957252eb049bd Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Wed, 25 Aug 2021 13:27:31 +0200 Subject: [PATCH 3/3] Removed types for generate method closes #38 Since 2.0.0 the `generate` method has been removed from the ObjectID class, this updates the types to reflect that. refs https://github.com/williamkapke/bson-objectid/commit/a518e352 --- objectid.d.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/objectid.d.ts b/objectid.d.ts index 5933101..6e0c78a 100644 --- a/objectid.d.ts +++ b/objectid.d.ts @@ -33,7 +33,5 @@ declare interface ObjectIDCtor { createFromHexString(hexString: string): ObjectID; isValid(hexString: string): boolean; isValid(ObjectID: ObjectID): boolean; - generate(): string; - generate(time: number): string; toString(): string; }