Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated types to support calling ObjectID() without the new keyword #41

Merged
merged 3 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 36 additions & 28 deletions objectid.d.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
// Type definitions for bson-objectid 1.3.1
// Project: bson-objectid
// Definitions by: Marcel Ernst <https://www.marcel-ernst.de>
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;
toString(): string;
}
32 changes: 32 additions & 0 deletions typing-tests/objectid-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down