bson 6.8.0 → 6.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,31 +1,65 @@
1
+ const map = new WeakMap<object, string>();
2
+
3
+ const TYPES = {
4
+ ArrayBuffer: '[object ArrayBuffer]',
5
+ SharedArrayBuffer: '[object SharedArrayBuffer]',
6
+ Uint8Array: '[object Uint8Array]',
7
+ BigInt64Array: '[object BigInt64Array]',
8
+ BigUint64Array: '[object BigUint64Array]',
9
+ RegExp: '[object RegExp]',
10
+ Map: '[object Map]',
11
+ Date: '[object Date]'
12
+ };
13
+
14
+ /**
15
+ * Retrieves the prototype.toString() of a value.
16
+ * If the value is an object, it will cache the result in a WeakMap for future use.
17
+ */
18
+ function getPrototypeString(value: unknown): string {
19
+ let str = map.get(value as object);
20
+
21
+ if (!str) {
22
+ str = Object.prototype.toString.call(value);
23
+ if (value !== null && typeof value === 'object') {
24
+ map.set(value, str);
25
+ }
26
+ }
27
+ return str;
28
+ }
29
+
1
30
  export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
2
- return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(
3
- Object.prototype.toString.call(value)
4
- );
31
+ const type = getPrototypeString(value);
32
+ return type === TYPES.ArrayBuffer || type === TYPES.SharedArrayBuffer;
5
33
  }
6
34
 
7
35
  export function isUint8Array(value: unknown): value is Uint8Array {
8
- return Object.prototype.toString.call(value) === '[object Uint8Array]';
36
+ const type = getPrototypeString(value);
37
+ return type === TYPES.Uint8Array;
9
38
  }
10
39
 
11
40
  export function isBigInt64Array(value: unknown): value is BigInt64Array {
12
- return Object.prototype.toString.call(value) === '[object BigInt64Array]';
41
+ const type = getPrototypeString(value);
42
+ return type === TYPES.BigInt64Array;
13
43
  }
14
44
 
15
45
  export function isBigUInt64Array(value: unknown): value is BigUint64Array {
16
- return Object.prototype.toString.call(value) === '[object BigUint64Array]';
46
+ const type = getPrototypeString(value);
47
+ return type === TYPES.BigUint64Array;
17
48
  }
18
49
 
19
50
  export function isRegExp(d: unknown): d is RegExp {
20
- return Object.prototype.toString.call(d) === '[object RegExp]';
51
+ const type = getPrototypeString(d);
52
+ return type === TYPES.RegExp;
21
53
  }
22
54
 
23
55
  export function isMap(d: unknown): d is Map<unknown, unknown> {
24
- return Object.prototype.toString.call(d) === '[object Map]';
56
+ const type = getPrototypeString(d);
57
+ return type === TYPES.Map;
25
58
  }
26
59
 
27
60
  export function isDate(d: unknown): d is Date {
28
- return Object.prototype.toString.call(d) === '[object Date]';
61
+ const type = getPrototypeString(d);
62
+ return type === TYPES.Date;
29
63
  }
30
64
 
31
65
  export type InspectFn = (x: unknown, options?: unknown) => string;
package/src/timestamp.ts CHANGED
@@ -28,6 +28,8 @@ export interface TimestampExtended {
28
28
  /**
29
29
  * @public
30
30
  * @category BSONType
31
+ *
32
+ * A special type for _internal_ MongoDB use and is **not** associated with the regular Date type.
31
33
  */
32
34
  export class Timestamp extends LongWithoutOverridesClass {
33
35
  get _bsontype(): 'Timestamp' {
@@ -36,6 +38,20 @@ export class Timestamp extends LongWithoutOverridesClass {
36
38
 
37
39
  static readonly MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
38
40
 
41
+ /**
42
+ * An incrementing ordinal for operations within a given second.
43
+ */
44
+ get i(): number {
45
+ return this.low >>> 0;
46
+ }
47
+
48
+ /**
49
+ * A `time_t` value measuring seconds since the Unix epoch
50
+ */
51
+ get t(): number {
52
+ return this.high >>> 0;
53
+ }
54
+
39
55
  /**
40
56
  * @param int - A 64-bit bigint representing the Timestamp.
41
57
  */
@@ -127,7 +143,7 @@ export class Timestamp extends LongWithoutOverridesClass {
127
143
 
128
144
  /** @internal */
129
145
  toExtendedJSON(): TimestampExtended {
130
- return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
146
+ return { $timestamp: { t: this.t, i: this.i } };
131
147
  }
132
148
 
133
149
  /** @internal */
@@ -144,8 +160,8 @@ export class Timestamp extends LongWithoutOverridesClass {
144
160
 
145
161
  inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
146
162
  inspect ??= defaultInspect;
147
- const t = inspect(this.high >>> 0, options);
148
- const i = inspect(this.low >>> 0, options);
163
+ const t = inspect(this.t, options);
164
+ const i = inspect(this.i, options);
149
165
  return `new Timestamp({ t: ${t}, i: ${i} })`;
150
166
  }
151
167
  }