Skip to content

Commit

Permalink
added typescript definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ernst committed Sep 27, 2017
1 parent 51a6216 commit 96b338d
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
29 changes: 29 additions & 0 deletions objectid.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Type definitions for bson-objectid 1.1.5
// Project: bson-objectid
// Definitions by: Marcel Ernst <https://www.marcel-ernst.de>

export = 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;
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"version": "1.1.5",
"description": "Construct ObjectIDs without the mongodb driver or bson module",
"main": "objectid.js",
"typings": "./objectid.d.ts",
"directories": {
"test": "test"
},
"scripts": {
"typescript-test": "tsc --project typing-tests",
"test": "mocha"
},
"repository": {
Expand All @@ -28,6 +30,8 @@
},
"homepage": "https://github.com/williamkapke/bson-objectid",
"devDependencies": {
"should": "^4.1.0"
"@types/node": "^8.0.31",
"should": "^4.1.0",
"typescript": "^2.5.3"
}
}
94 changes: 94 additions & 0 deletions typing-tests/objectid-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/// <reference path="../objectid.d.ts" />
import ObjectID = require('../objectid');

// ----------------------------------------------------------------------------
// setup test data
const time:number = 1414093117;
const array:number[] = [ 84, 73, 90, 217, 76, 147, 71, 33, 237, 231, 109, 144 ];
const buffer:Buffer = new Buffer([84, 73, 90, 217, 76, 147, 71, 33, 237, 231, 109, 144 ]);
const hexString:string = "54495ad94c934721ede76d90";
const idString:string = "TIZÙL“G!íçm";


// ----------------------------------------------------------------------------
// should construct with no arguments
let oid:ObjectID = new ObjectID();

// ----------------------------------------------------------------------------
// should have an `id` property
oid.id;

// ----------------------------------------------------------------------------
// should have a `str` property
oid.str;

// ----------------------------------------------------------------------------
// should construct with a `time` argument
oid = new ObjectID(time);

// ----------------------------------------------------------------------------
// should construct with an `array` argument
oid = new ObjectID(array);

// ----------------------------------------------------------------------------
// should construct with a `buffer` argument
oid = new ObjectID(buffer);

// ----------------------------------------------------------------------------
// should construct with a `hexString` argument
oid = new ObjectID(hexString);

// ----------------------------------------------------------------------------
// should construct with a `idString` argument
oid = new ObjectID(idString);

// ----------------------------------------------------------------------------
// should construct with `ObjectID.createFromTime(time)` and should have 0's at the end
oid = ObjectID.createFromTime(time);

// ----------------------------------------------------------------------------
// should construct with `ObjectID.createFromHexString(hexString)`
oid = ObjectID.createFromHexString(hexString);

// ----------------------------------------------------------------------------
// should correctly retrieve timestamp
const timestamp:number = oid.getTimestamp();

// ----------------------------------------------------------------------------
// should validate valid hex strings
let isValid:boolean = ObjectID.isValid(hexString);

// ----------------------------------------------------------------------------
// should validate legit ObjectID objects
isValid = ObjectID.isValid(oid);

// ----------------------------------------------------------------------------
// should invalidate bad strings
// not necessary for typescript

// ----------------------------------------------------------------------------
// should evaluate equality with .equals()
const id1 = new ObjectID();
const id2 = new ObjectID(id1.str);
const equals:boolean = id1.equals(id2);

// ----------------------------------------------------------------------------
// should evaluate equality with via deepEqual
// not necessary for typescript

// ----------------------------------------------------------------------------
// should generate valid hex strings
const str1:string = ObjectID.generate();
const str2:string = ObjectID.generate(time);

// ----------------------------------------------------------------------------
// should convert to a hex string for JSON.stringify
// not necessary for typescript

// ----------------------------------------------------------------------------
// should convert to a hex string for ObjectID.toString()
const toStr:string = oid.toString();

// ----------------------------------------------------------------------------
// should throw and error if constructing with an invalid string
// not necessary for typescript
12 changes: 12 additions & 0 deletions typing-tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compileOnSave": false,
"compilerOptions": {
"module": "commonjs",
"noEmit": true,
"noImplicitAny": true
},
"files": [
"../objectid.d.ts",
"./objectid-tests.ts"
]
}

0 comments on commit 96b338d

Please sign in to comment.