Skip to content

Commit

Permalink
feat(NODE-3504): add unambiguous Timestamp() constructor overload (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax authored Aug 2, 2021
1 parent d1d8566 commit 0298dd8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
19 changes: 13 additions & 6 deletions src/timestamp.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Long } from './long';
import { isObjectLike } from './parser/utils';

/** @public */
export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect';
/** @public */
export type LongWithoutOverrides = new (low: number | Long, high?: number, unsigned?: boolean) => {
export type LongWithoutOverrides = new (low: unknown, high?: number, unsigned?: boolean) => {
[P in Exclude<keyof Long, TimestampOverrides>]: Long[P];
};
/** @public */
Expand All @@ -27,20 +28,26 @@ export class Timestamp extends LongWithoutOverridesClass {
/**
* @param low - A 64-bit Long representing the Timestamp.
*/
constructor(low: Long);
constructor(long: Long);
/**
* @param value - A pair of two values indicating timestamp and increment.
*/
constructor(value: { t: number; i: number });
/**
* @param low - the low (signed) 32 bits of the Timestamp.
* @param high - the high (signed) 32 bits of the Timestamp.
* @deprecated Please use `Timestamp({ t: high, i: low })` or `Timestamp(Long(low, high))` instead.
*/
constructor(low: Long);
constructor(low: number, high: number);
constructor(low: number | Long, high?: number) {
constructor(low: number | Long | { t: number; i: number }, high?: number) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
///@ts-expect-error
if (!(this instanceof Timestamp)) return new Timestamp(low, high);

if (Long.isLong(low)) {
super(low.low, low.high, true);
} else if (isObjectLike(low) && typeof low.t !== 'undefined' && typeof low.i !== 'undefined') {
super(low.i, low.t, true);
} else {
super(low, high, true);
}
Expand Down Expand Up @@ -95,7 +102,7 @@ export class Timestamp extends LongWithoutOverridesClass {

/** @internal */
static fromExtendedJSON(doc: TimestampExtended): Timestamp {
return new Timestamp(doc.$timestamp.i, doc.$timestamp.t);
return new Timestamp(doc.$timestamp);
}

/** @internal */
Expand All @@ -104,6 +111,6 @@ export class Timestamp extends LongWithoutOverridesClass {
}

inspect(): string {
return `new Timestamp(${this.getLowBits().toString()}, ${this.getHighBits().toString()})`;
return `new Timestamp({ t: ${this.getHighBits()}, i: ${this.getLowBits()} })`;
}
}
4 changes: 2 additions & 2 deletions test/node/bson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2120,8 +2120,8 @@ describe('BSON', function () {
* @ignore
*/
it('Timestamp', function () {
const timestamp = new Timestamp(1, 100);
expect(inspect(timestamp)).to.equal('new Timestamp(1, 100)');
const timestamp = new Timestamp({ t: 100, i: 1 });
expect(inspect(timestamp)).to.equal('new Timestamp({ t: 100, i: 1 })');
});
});
});
17 changes: 16 additions & 1 deletion test/node/timestamp_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ describe('Timestamp', function () {
new BSON.Timestamp(-1, -1),
new BSON.Timestamp(new BSON.Timestamp(0xffffffff, 0xffffffff)),
new BSON.Timestamp(new BSON.Long(0xffffffff, 0xfffffffff, false)),
new BSON.Timestamp(new BSON.Long(0xffffffff, 0xfffffffff, true))
new BSON.Timestamp(new BSON.Long(0xffffffff, 0xfffffffff, true)),
new BSON.Timestamp({ t: 0xffffffff, i: 0xfffffffff }),
new BSON.Timestamp({ t: -1, i: -1 }),
new BSON.Timestamp({ t: new BSON.Int32(0xffffffff), i: new BSON.Int32(0xffffffff) })
].forEach(timestamp => {
expect(timestamp).to.have.property('unsigned', true);
});
Expand All @@ -29,4 +32,16 @@ describe('Timestamp', function () {
$timestamp: { t: 4294967295, i: 4294967295 }
});
});

it('should accept a { t, i } object as constructor input', function () {
const input = { t: 89, i: 144 };
const timestamp = new BSON.Timestamp(input);
expect(timestamp.toExtendedJSON()).to.deep.equal({ $timestamp: input });
});

it('should accept a { t, i } object as constructor input and coerce to integer', function () {
const input = { t: new BSON.Int32(89), i: new BSON.Int32(144) };
const timestamp = new BSON.Timestamp(input);
expect(timestamp.toExtendedJSON()).to.deep.equal({ $timestamp: { t: 89, i: 144 } });
});
});

0 comments on commit 0298dd8

Please sign in to comment.