hadron-type-checker 6.7.0 → 6.10.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.
package/index.d.ts ADDED
@@ -0,0 +1,51 @@
1
+ import type {
2
+ ObjectId,
3
+ MinKey,
4
+ MaxKey,
5
+ Long,
6
+ Double,
7
+ Int32,
8
+ Decimal128,
9
+ Binary,
10
+ BSONRegExp,
11
+ Code,
12
+ BSONSymbol,
13
+ Timestamp
14
+ } from 'bson';
15
+
16
+ export type TypeCastMap = {
17
+ Array: unknown[];
18
+ Binary: Binary;
19
+ Boolean: boolean;
20
+ Code: Code;
21
+ Date: Date;
22
+ Decimal128: Decimal128;
23
+ Double: Double;
24
+ Int32: Int32;
25
+ Int64: Long;
26
+ MaxKey: MaxKey;
27
+ MinKey: MinKey;
28
+ Null: null;
29
+ Object: Record<string, unknown>;
30
+ ObjectId: ObjectId;
31
+ BSONRegExp: BSONRegExp;
32
+ String: string;
33
+ BSONSymbol: BSONSymbol;
34
+ Timestamp: Timestamp;
35
+ Undefined: undefined;
36
+ };
37
+
38
+ export type TypeCastTypes = keyof TypeCastMap;
39
+
40
+ declare class TypeChecker {
41
+ cast<O = unknown, T extends string = string>(
42
+ object: O,
43
+ type: T
44
+ ): T extends TypeCastTypes ? TypeCastMap[T] : O;
45
+ type(object: unknown): TypeCastTypes;
46
+ castableTypes(highPrecisionSupport?: boolean): TypeCastTypes[];
47
+ }
48
+
49
+ declare const typeCheckerInstance: TypeChecker;
50
+
51
+ export default typeCheckerInstance;
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "email": "compass@mongodb.com"
8
8
  },
9
9
  "homepage": "https://github.com/mongodb-js/compass",
10
- "version": "6.7.0",
10
+ "version": "6.10.0",
11
11
  "repository": {
12
12
  "type": "git",
13
13
  "url": "https://github.com/mongodb-js/compass.git"
@@ -17,10 +17,7 @@
17
17
  "mongodb-js"
18
18
  ],
19
19
  "main": "index.js",
20
- "exports": {
21
- "webpack": "./index.js",
22
- "require": "./index.js"
23
- },
20
+ "compass:main": "index.js",
24
21
  "scripts": {
25
22
  "test-check-ci": "npm test",
26
23
  "test": "mocha",
@@ -40,5 +37,5 @@
40
37
  "eslint-config-mongodb-js": "^5.0.3",
41
38
  "mocha": "^8.0.1"
42
39
  },
43
- "gitHead": "2bb63cdb1d4e825f280c0d2af14b7298c5769490"
40
+ "gitHead": "9f5498524c3e50d351f6f36fb015ff14163a39cf"
44
41
  }
@@ -23,7 +23,7 @@ const {
23
23
  Code,
24
24
  BSONSymbol,
25
25
  Timestamp,
26
- BSONMap
26
+ Map
27
27
  } = require('bson');
28
28
  /**
29
29
  * The object string.
@@ -57,6 +57,7 @@ const DECIMAL_128 = 'Decimal128';
57
57
  const OBJECT_TYPE = '[object Object]';
58
58
  const EMPTY = '';
59
59
  const OBJECT_ID = 'ObjectID';
60
+ const SYMBOL = 'Symbol';
60
61
 
61
62
  /**
62
63
  * The bson type field.
@@ -207,7 +208,8 @@ const toObjectID = (object) => {
207
208
  };
208
209
 
209
210
  const toBinary = (object) => {
210
- return new Binary('' + object, Binary.SUBTYPE_DEFAULT);
211
+ const buffer = ArrayBuffer.isView(object) ? Buffer.from(object) : Buffer.from(toString(object), 'utf8');
212
+ return new Binary(buffer, Binary.SUBTYPE_DEFAULT);
211
213
  };
212
214
 
213
215
  const toRegex = (object) => {
@@ -227,10 +229,6 @@ const toTimestamp = (object) => {
227
229
  return Timestamp.fromNumber(number);
228
230
  };
229
231
 
230
- const toMap = (object) => {
231
- return new BSONMap(object);
232
- };
233
-
234
232
  /**
235
233
  * The functions to cast to a type.
236
234
  */
@@ -249,10 +247,9 @@ const CASTERS = {
249
247
  Null: toNull,
250
248
  Object: toObject,
251
249
  ObjectId: toObjectID,
252
- BSONRegexp: toRegex,
250
+ BSONRegExp: toRegex,
253
251
  String: toString,
254
252
  BSONSymbol: toSymbol,
255
- BSONMap: toMap,
256
253
  Timestamp: toTimestamp,
257
254
  Undefined: toUndefined
258
255
  };
@@ -343,6 +340,9 @@ class TypeChecker {
343
340
  if (object._bsontype === OBJECT_ID) {
344
341
  return 'ObjectId';
345
342
  }
343
+ if (object._bsontype === SYMBOL) {
344
+ return 'BSONSymbol';
345
+ }
346
346
  return object._bsontype;
347
347
  }
348
348
  if (isNumber(object)) {