hadron-type-checker 7.4.21 → 7.4.23

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/.depcheckrc CHANGED
@@ -1 +1,6 @@
1
- ignores: []
1
+ ignores: [
2
+ '@mongodb-js/prettier-config-compass',
3
+ '@mongodb-js/tsconfig-compass',
4
+ "@babel/register",
5
+ "babel-loader"
6
+ ]
package/.eslintignore ADDED
@@ -0,0 +1,2 @@
1
+ lib/
2
+ dist/
package/.eslintrc.js ADDED
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+ module.exports = {
3
+ root: true,
4
+ extends: ['@mongodb-js/eslint-config-compass'],
5
+ parserOptions: {
6
+ tsconfigRootDir: __dirname,
7
+ project: ['./tsconfig.json'],
8
+ },
9
+ };
package/.mocharc.js ADDED
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+ module.exports = require('@mongodb-js/mocha-config-compass');
@@ -0,0 +1,3 @@
1
+ export { default } from './type-checker';
2
+ export type { TypeCastMap, TypeCastTypes } from './type-checker';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzC,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.default = void 0;
7
+ var type_checker_1 = require("./type-checker");
8
+ Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(type_checker_1).default; } });
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,+CAAyC;AAAhC,wHAAA,OAAO,OAAA"}
@@ -0,0 +1,43 @@
1
+ import { ObjectId, MinKey, MaxKey, Long, Double, Int32, Decimal128, Binary, BSONRegExp, Code, BSONSymbol, Timestamp } from 'bson';
2
+ export type TypeCastMap = {
3
+ Array: unknown[];
4
+ Binary: Binary;
5
+ Boolean: boolean;
6
+ Code: Code;
7
+ Date: Date;
8
+ Decimal128: Decimal128;
9
+ Double: Double;
10
+ Int32: Int32;
11
+ Int64: Long;
12
+ MaxKey: MaxKey;
13
+ MinKey: MinKey;
14
+ Null: null;
15
+ Object: Record<string, unknown>;
16
+ ObjectId: ObjectId;
17
+ BSONRegExp: BSONRegExp;
18
+ String: string;
19
+ BSONSymbol: BSONSymbol;
20
+ Timestamp: Timestamp;
21
+ Undefined: undefined;
22
+ };
23
+ export type TypeCastTypes = keyof TypeCastMap;
24
+ /**
25
+ * Checks the types of objects and returns them as readable strings.
26
+ */
27
+ declare class TypeChecker {
28
+ /**
29
+ * Cast the provided object to the desired type.
30
+ */
31
+ cast<O = unknown, T extends string = string>(object: O, type: T): T extends TypeCastTypes ? TypeCastMap[T] : O;
32
+ /**
33
+ * Get the type for the object.
34
+ */
35
+ type(object: unknown): TypeCastTypes;
36
+ /**
37
+ * Get a list of types the object can be cast to.
38
+ */
39
+ castableTypes(highPrecisionSupport?: boolean): TypeCastTypes[];
40
+ }
41
+ declare const _default: TypeChecker;
42
+ export default _default;
43
+ //# sourceMappingURL=type-checker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type-checker.d.ts","sourceRoot":"","sources":["../src/type-checker.ts"],"names":[],"mappings":"AAYA,OAAO,EACL,QAAQ,EACR,MAAM,EACN,MAAM,EACN,IAAI,EACJ,MAAM,EACN,KAAK,EACL,UAAU,EACV,MAAM,EACN,UAAU,EACV,IAAI,EACJ,UAAU,EACV,SAAS,EACV,MAAM,MAAM,CAAC;AAEd,MAAM,MAAM,WAAW,GAAG;IACxB,KAAK,EAAE,OAAO,EAAE,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,IAAI,CAAC;IACX,IAAI,EAAE,IAAI,CAAC;IACX,UAAU,EAAE,UAAU,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,KAAK,CAAC;IACb,KAAK,EAAE,IAAI,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,IAAI,CAAC;IACX,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,QAAQ,EAAE,QAAQ,CAAC;IACnB,UAAU,EAAE,UAAU,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,UAAU,CAAC;IACvB,SAAS,EAAE,SAAS,CAAC;IACrB,SAAS,EAAE,SAAS,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,MAAM,WAAW,CAAC;AAyS9C;;GAEG;AACH,cAAM,WAAW;IACf;;OAEG;IACH,IAAI,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,SAAS,MAAM,GAAG,MAAM,EACzC,MAAM,EAAE,CAAC,EACT,IAAI,EAAE,CAAC,GACN,CAAC,SAAS,aAAa,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC;IAW/C;;OAEG;IACH,IAAI,CAAC,MAAM,EAAE,OAAO,GAAG,aAAa;IA4BpC;;OAEG;IACH,aAAa,CAAC,oBAAoB,UAAQ,GAAG,aAAa,EAAE;CAM7D;;AAED,wBAAiC"}
@@ -0,0 +1,306 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const lodash_1 = require("lodash");
4
+ const bson_1 = require("bson");
5
+ /**
6
+ * The object string.
7
+ */
8
+ const OBJECT = 'Object';
9
+ /**
10
+ * The array type string.
11
+ */
12
+ const ARRAY = 'Array';
13
+ /**
14
+ * True constant.
15
+ */
16
+ const TRUE = 'true';
17
+ /**
18
+ * False constant.
19
+ */
20
+ const FALSE = 'false';
21
+ /**
22
+ * Long constant.
23
+ */
24
+ const LONG = 'Long';
25
+ const INT_32 = 'Int32';
26
+ const INT_64 = 'Int64';
27
+ const DOUBLE = 'Double';
28
+ const DECIMAL_128 = 'Decimal128';
29
+ const OBJECT_TYPE = '[object Object]';
30
+ const EMPTY = '';
31
+ const OBJECT_ID = 'ObjectID';
32
+ const SYMBOL = 'Symbol';
33
+ /**
34
+ * The bson type field.
35
+ */
36
+ const BSON_TYPE = '_bsontype';
37
+ /**
38
+ * The match regex.
39
+ */
40
+ const MATCH = /\[object (\w+)\]/;
41
+ /**
42
+ * The max int 32 value.
43
+ */
44
+ const BSON_INT32_MAX = 0x7fffffff;
45
+ /**
46
+ * The min int 32 value.
47
+ */
48
+ const BSON_INT32_MIN = -0x80000000;
49
+ const BSON_INT64_MAX = Math.pow(2, 63) - 1;
50
+ const BSON_INT64_MIN = -BSON_INT64_MAX;
51
+ /**
52
+ * The number regex.
53
+ */
54
+ const NUMBER_REGEX = /^-?\d+$/;
55
+ /**
56
+ * All bson types that are numbers.
57
+ */
58
+ const NUMBER_TYPES = ['Long', 'Int32', 'Double', 'Decimal128'];
59
+ const toDate = (object) => {
60
+ return new Date(object);
61
+ };
62
+ const toMinKey = () => {
63
+ return new bson_1.MinKey();
64
+ };
65
+ const toMaxKey = () => {
66
+ return new bson_1.MaxKey();
67
+ };
68
+ const toUndefined = () => {
69
+ return undefined;
70
+ };
71
+ const toNull = () => {
72
+ return null;
73
+ };
74
+ const toBoolean = (object) => {
75
+ if ((0, lodash_1.isString)(object)) {
76
+ if (object.toLowerCase() === TRUE) {
77
+ return true;
78
+ }
79
+ else if (object.toLowerCase() === FALSE) {
80
+ return false;
81
+ }
82
+ throw new Error(`'${object}' is not a valid boolean string`);
83
+ }
84
+ if (object) {
85
+ return true;
86
+ }
87
+ return false;
88
+ };
89
+ const toObject = (object) => {
90
+ if ((0, lodash_1.isPlainObject)(object)) {
91
+ return object;
92
+ }
93
+ return {};
94
+ };
95
+ const toArray = (object) => {
96
+ if ((0, lodash_1.isArray)(object)) {
97
+ return object;
98
+ }
99
+ if ((0, lodash_1.isPlainObject)(object)) {
100
+ return [];
101
+ }
102
+ return [object];
103
+ };
104
+ const toInt32 = (object) => {
105
+ if (object === '-' || object === '') {
106
+ throw new Error(`Value '${object}' is not a valid Int32 value`);
107
+ }
108
+ const number = (0, lodash_1.toNumber)(object);
109
+ if (number >= BSON_INT32_MIN && number <= BSON_INT32_MAX) {
110
+ return new bson_1.Int32(number);
111
+ }
112
+ throw new Error(`Value ${number} is outside the valid Int32 range`);
113
+ };
114
+ const toInt64 = (object) => {
115
+ if (object === '-' || object === '') {
116
+ throw new Error(`Value '${object}' is not a valid Int64 value`);
117
+ }
118
+ const number = (0, lodash_1.toNumber)(object);
119
+ if (number >= BSON_INT64_MIN && number <= BSON_INT64_MAX) {
120
+ // when casting from int32 object(this will have object.value) or literal
121
+ // (it will a typeof number) we can safely create object fromNumber, as it
122
+ // will not be greater than JS's max value
123
+ if (object?.value || typeof object === 'number') {
124
+ return bson_1.Long.fromNumber(number);
125
+ }
126
+ else if (typeof object === 'object' &&
127
+ object !== null &&
128
+ 'toString' in object) {
129
+ // to make sure we are still displaying Very Large numbers properly, convert
130
+ // the current 'object' to a string
131
+ return bson_1.Long.fromString(object.toString());
132
+ }
133
+ return bson_1.Long.fromString((0, lodash_1.toString)(object));
134
+ }
135
+ throw new Error(`Value ${(0, lodash_1.toString)(object)} is outside the valid Int64 range`);
136
+ };
137
+ const toDouble = (object) => {
138
+ if (object === '-' || object === '') {
139
+ throw new Error(`Value '${object}' is not a valid Double value`);
140
+ }
141
+ if ((0, lodash_1.isString)(object) && object.endsWith('.')) {
142
+ throw new Error('Please enter at least one digit after the decimal');
143
+ }
144
+ const number = (0, lodash_1.toNumber)(object);
145
+ return new bson_1.Double(number);
146
+ };
147
+ const toDecimal128 = (object) => {
148
+ /*
149
+ If converting a BSON Object, extract the value before converting to a string.
150
+ */
151
+ if ((0, lodash_1.hasIn)(object, BSON_TYPE) &&
152
+ NUMBER_TYPES.includes(object._bsontype)) {
153
+ const bsonObj = object;
154
+ object =
155
+ bsonObj._bsontype === LONG ? bsonObj.toString() : bsonObj.valueOf();
156
+ }
157
+ return bson_1.Decimal128.fromString((0, lodash_1.toString)(object));
158
+ };
159
+ const toObjectID = (object) => {
160
+ if (!(0, lodash_1.isString)(object) || object === '') {
161
+ return new bson_1.ObjectId();
162
+ }
163
+ return bson_1.ObjectId.createFromHexString(object);
164
+ };
165
+ const toBinary = (object) => {
166
+ const buffer = ArrayBuffer.isView(object)
167
+ ? Buffer.from(object)
168
+ : Buffer.from((0, lodash_1.toString)(object), 'utf8');
169
+ return new bson_1.Binary(buffer, bson_1.Binary.SUBTYPE_DEFAULT);
170
+ };
171
+ const toRegex = (object) => {
172
+ return new bson_1.BSONRegExp((0, lodash_1.toString)(object));
173
+ };
174
+ const toCode = (object) => {
175
+ return new bson_1.Code((0, lodash_1.toString)(object));
176
+ };
177
+ const toSymbol = (object) => {
178
+ return new bson_1.BSONSymbol((0, lodash_1.toString)(object));
179
+ };
180
+ const toTimestamp = (object) => {
181
+ const number = (0, lodash_1.toNumber)(object);
182
+ return bson_1.Timestamp.fromNumber(number);
183
+ };
184
+ /**
185
+ * The functions to cast to a type.
186
+ */
187
+ const CASTERS = {
188
+ Array: toArray,
189
+ Binary: toBinary,
190
+ Boolean: toBoolean,
191
+ Code: toCode,
192
+ Date: toDate,
193
+ Decimal128: toDecimal128,
194
+ Double: toDouble,
195
+ Int32: toInt32,
196
+ Int64: toInt64,
197
+ MaxKey: toMaxKey,
198
+ MinKey: toMinKey,
199
+ Null: toNull,
200
+ Object: toObject,
201
+ ObjectId: toObjectID,
202
+ BSONRegExp: toRegex,
203
+ String: lodash_1.toString,
204
+ BSONSymbol: toSymbol,
205
+ Timestamp: toTimestamp,
206
+ Undefined: toUndefined,
207
+ };
208
+ /**
209
+ * An array of all bson types.
210
+ */
211
+ const TYPES = (0, lodash_1.keys)(CASTERS);
212
+ /**
213
+ * Checks if a string is an int32.
214
+ */
215
+ class Int32Check {
216
+ test(string) {
217
+ if (NUMBER_REGEX.test(string)) {
218
+ const value = (0, lodash_1.toNumber)(string);
219
+ return value >= BSON_INT32_MIN && value <= BSON_INT32_MAX;
220
+ }
221
+ return false;
222
+ }
223
+ }
224
+ /**
225
+ * Checks if a string is an int64.
226
+ */
227
+ class Int64Check {
228
+ test(string) {
229
+ if (NUMBER_REGEX.test(string)) {
230
+ return Number.isSafeInteger((0, lodash_1.toNumber)(string));
231
+ }
232
+ return false;
233
+ }
234
+ }
235
+ const INT32_CHECK = new Int32Check();
236
+ const INT64_CHECK = new Int64Check();
237
+ /**
238
+ * Gets the BSON type for a JS number.
239
+ */
240
+ const numberToBsonType = (number) => {
241
+ const string = (0, lodash_1.toString)(number);
242
+ if (INT32_CHECK.test(string)) {
243
+ return INT_32;
244
+ }
245
+ else if (INT64_CHECK.test(string)) {
246
+ return INT_64;
247
+ }
248
+ return DOUBLE;
249
+ };
250
+ /**
251
+ * Checks the types of objects and returns them as readable strings.
252
+ */
253
+ class TypeChecker {
254
+ /**
255
+ * Cast the provided object to the desired type.
256
+ */
257
+ cast(object, type) {
258
+ const caster = CASTERS[type];
259
+ let result = object;
260
+ if (caster) {
261
+ result = caster(object);
262
+ }
263
+ return (result === OBJECT_TYPE && result !== object ? EMPTY : result);
264
+ }
265
+ /**
266
+ * Get the type for the object.
267
+ */
268
+ type(object) {
269
+ if ((0, lodash_1.hasIn)(object, BSON_TYPE)) {
270
+ const bsonObj = object;
271
+ if (bsonObj._bsontype === LONG) {
272
+ return INT_64;
273
+ }
274
+ if (bsonObj._bsontype === OBJECT_ID) {
275
+ return 'ObjectId';
276
+ }
277
+ if (bsonObj._bsontype === SYMBOL) {
278
+ return 'BSONSymbol';
279
+ }
280
+ return bsonObj._bsontype;
281
+ }
282
+ if ((0, lodash_1.isNumber)(object)) {
283
+ return numberToBsonType(object);
284
+ }
285
+ if ((0, lodash_1.isPlainObject)(object)) {
286
+ return OBJECT;
287
+ }
288
+ if ((0, lodash_1.isArray)(object)) {
289
+ return ARRAY;
290
+ }
291
+ return Object.prototype.toString
292
+ .call(object)
293
+ .replace(MATCH, '$1');
294
+ }
295
+ /**
296
+ * Get a list of types the object can be cast to.
297
+ */
298
+ castableTypes(highPrecisionSupport = false) {
299
+ if (highPrecisionSupport === true) {
300
+ return TYPES;
301
+ }
302
+ return (0, lodash_1.without)(TYPES, DECIMAL_128);
303
+ }
304
+ }
305
+ exports.default = new TypeChecker();
306
+ //# sourceMappingURL=type-checker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type-checker.js","sourceRoot":"","sources":["../src/type-checker.ts"],"names":[],"mappings":";;AAAA,mCAUgB;AAEhB,+BAac;AAyBd;;GAEG;AACH,MAAM,MAAM,GAAG,QAAQ,CAAC;AAExB;;GAEG;AACH,MAAM,KAAK,GAAG,OAAO,CAAC;AAEtB;;GAEG;AACH,MAAM,IAAI,GAAG,MAAM,CAAC;AAEpB;;GAEG;AACH,MAAM,KAAK,GAAG,OAAO,CAAC;AAEtB;;GAEG;AACH,MAAM,IAAI,GAAG,MAAM,CAAC;AAEpB,MAAM,MAAM,GAAG,OAAO,CAAC;AACvB,MAAM,MAAM,GAAG,OAAO,CAAC;AACvB,MAAM,MAAM,GAAG,QAAQ,CAAC;AACxB,MAAM,WAAW,GAAG,YAAY,CAAC;AACjC,MAAM,WAAW,GAAG,iBAAiB,CAAC;AACtC,MAAM,KAAK,GAAG,EAAE,CAAC;AACjB,MAAM,SAAS,GAAG,UAAU,CAAC;AAC7B,MAAM,MAAM,GAAG,QAAQ,CAAC;AAExB;;GAEG;AACH,MAAM,SAAS,GAAG,WAAW,CAAC;AAE9B;;GAEG;AACH,MAAM,KAAK,GAAG,kBAAkB,CAAC;AAEjC;;GAEG;AACH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C7B,MAAM,OAAO,GAAG,MAA+B,CAAC;YAChD,IAAI,OAAO,CAAC,SAAS,KAAK,IAAI,EAAE,CAAC;gBAC/B,OAAO,MAAM,CAAC;YAChB,CAAC;YACD,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS,EAAE,CAAC;gBACpC,OAAO,UAAU,CAAC;YACpB,CAAC;YACD,IAAI,OAAO,CAAC,SAAS,KAAK,MAAM,EAAE,CAAC;gBACjC,OAAO,YAAY,CAAC;YACtB,CAAC;YACD,OAAO,OAAO,CAAC,SAA0B,CAAC;QAC5C,CAAC;QACD,IAAI,IAAA,iBAAQ,EAAC,MAAM,CAAC,EAAE,CAAC;YACrB,OAAO,gBAAgB,CAAC,MAAM,CAAC,CAAC;QAClC,CAAC;QACD,IAAI,IAAA,sBAAa,EAAC,MAAM,CAAC,EAAE,CAAC;YAC1B,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,IAAI,IAAA,gBAAO,EAAC,MAAM,CAAC,EAAE,CAAC;YACpB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,OAAO,MAAM,CAAC,SAAS,CAAC,QAAQ;aAC7B,IAAI,CAAC,MAAM,CAAC;aACZ,OAAO,CAAC,KAAK,EAAE,IAAI,CAAkB,CAAC;IAC3C,CAAC;IAED;;OAEG;IACH,aAAa,CAAC,oBAAoB,GAAG,KAAK;QACxC,IAAI,oBAAoB,KAAK,IAAI,EAAE,CAAC;YAClC,OAAO,KAAwB,CAAC;QAClC,CAAC;QACD,OAAO,IAAA,gBAAO,EAAC,KAAK,EAAE,WAAW,CAAoB,CAAC;IACxD,CAAC;CACF;AAED,kBAAe,IAAI,WAAW,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,40 +1,66 @@
1
1
  {
2
2
  "name": "hadron-type-checker",
3
3
  "description": "Hadron Type Checker",
4
- "author": "Durran Jordan <durran@gmail.com>",
4
+ "author": {
5
+ "name": "MongoDB Inc",
6
+ "email": "compass@mongodb.com"
7
+ },
5
8
  "bugs": {
6
9
  "url": "https://jira.mongodb.org/projects/COMPASS/issues",
7
10
  "email": "compass@mongodb.com"
8
11
  },
9
12
  "homepage": "https://github.com/mongodb-js/compass",
10
- "version": "7.4.21",
13
+ "version": "7.4.23",
11
14
  "repository": {
12
15
  "type": "git",
13
16
  "url": "https://github.com/mongodb-js/compass.git"
14
17
  },
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
15
21
  "license": "SSPL",
16
22
  "keywords": [
17
23
  "mongodb-js"
18
24
  ],
19
- "main": "index.js",
20
- "compass:main": "index.js",
25
+ "main": "dist/index.js",
26
+ "compass:main": "src/index.ts",
27
+ "compass:exports": {
28
+ ".": "./src/index.ts"
29
+ },
30
+ "types": "./dist/index.d.ts",
21
31
  "scripts": {
22
- "test-check-ci": "npm test",
23
- "test": "mocha",
24
- "check": "npm run lint && npm run depcheck",
25
- "lint": "eslint-compass \"./{src,lib,test,bin}/**/*.{js,jsx,ts,tsx}\" \"./*.js\" --no-error-on-unmatched-pattern",
32
+ "bootstrap": "npm run compile",
33
+ "prepublishOnly": "npm run compile && compass-scripts check-exports-exist",
34
+ "compile": "tsc -p tsconfig-build.json",
35
+ "typecheck": "tsc -p tsconfig.json --noEmit",
36
+ "eslint": "eslint-compass",
37
+ "prettier": "prettier-compass",
38
+ "lint": "npm run eslint . && npm run prettier -- --check .",
26
39
  "depcheck": "compass-scripts check-peer-deps && depcheck",
27
- "test-ci": "npm run test"
40
+ "check": "npm run typecheck && npm run lint && npm run depcheck",
41
+ "check-ci": "npm run check",
42
+ "test": "mocha",
43
+ "test-cov": "nyc --compact=false --produce-source-map=false -x \"**/*.spec.*\" --reporter=lcov --reporter=text --reporter=html npm run test",
44
+ "test-watch": "npm run test -- --watch",
45
+ "test-ci": "npm run test-cov",
46
+ "reformat": "npm run eslint . -- --fix && npm run prettier -- --write ."
28
47
  },
29
48
  "dependencies": {
30
49
  "bson": "^6.10.4",
31
50
  "lodash": "^4.17.21"
32
51
  },
33
52
  "devDependencies": {
34
- "@mongodb-js/eslint-config-compass": "^1.4.10",
53
+ "@mongodb-js/eslint-config-compass": "^1.4.12",
54
+ "@mongodb-js/mocha-config-compass": "^1.7.2",
55
+ "@mongodb-js/prettier-config-compass": "^1.2.9",
56
+ "@mongodb-js/tsconfig-compass": "^1.2.12",
57
+ "@types/chai": "^4.2.21",
58
+ "@types/mocha": "^9.0.0",
35
59
  "chai": "^4.2.0",
36
60
  "depcheck": "^1.4.1",
37
- "mocha": "^10.2.0"
61
+ "mocha": "^10.2.0",
62
+ "nyc": "^15.1.0",
63
+ "typescript": "^5.9.3"
38
64
  },
39
- "gitHead": "cf65a2d551fe9b3d56d54464d510e605a47a3b11"
65
+ "gitHead": "00593e305433f42b3bb9249a7afcb9924874d7af"
40
66
  }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { default } from './type-checker';
2
+ export type { TypeCastMap, TypeCastTypes } from './type-checker';
@@ -1,5 +1,4 @@
1
- 'use strict';
2
- const {
1
+ import {
3
2
  isPlainObject,
4
3
  isArray,
5
4
  isString,
@@ -9,9 +8,9 @@ const {
9
8
  without,
10
9
  toNumber,
11
10
  toString,
12
- } = require('lodash');
11
+ } from 'lodash';
13
12
 
14
- const {
13
+ import {
15
14
  ObjectId,
16
15
  MinKey,
17
16
  MaxKey,
@@ -24,7 +23,31 @@ const {
24
23
  Code,
25
24
  BSONSymbol,
26
25
  Timestamp,
27
- } = require('bson');
26
+ } from 'bson';
27
+
28
+ export type TypeCastMap = {
29
+ Array: unknown[];
30
+ Binary: Binary;
31
+ Boolean: boolean;
32
+ Code: Code;
33
+ Date: Date;
34
+ Decimal128: Decimal128;
35
+ Double: Double;
36
+ Int32: Int32;
37
+ Int64: Long;
38
+ MaxKey: MaxKey;
39
+ MinKey: MinKey;
40
+ Null: null;
41
+ Object: Record<string, unknown>;
42
+ ObjectId: ObjectId;
43
+ BSONRegExp: BSONRegExp;
44
+ String: string;
45
+ BSONSymbol: BSONSymbol;
46
+ Timestamp: Timestamp;
47
+ Undefined: undefined;
48
+ };
49
+
50
+ export type TypeCastTypes = keyof TypeCastMap;
28
51
  /**
29
52
  * The object string.
30
53
  */
@@ -90,29 +113,29 @@ const NUMBER_REGEX = /^-?\d+$/;
90
113
  /**
91
114
  * All bson types that are numbers.
92
115
  */
93
- const NUMBER_TYPES = ['Long', 'Int32', 'Double', 'Decimal128'];
116
+ const NUMBER_TYPES = ['Long', 'Int32', 'Double', 'Decimal128'] as const;
94
117
 
95
- const toDate = (object) => {
96
- return new Date(object);
118
+ const toDate = (object: unknown): Date => {
119
+ return new Date(object as string | number | Date);
97
120
  };
98
121
 
99
- const toMinKey = () => {
122
+ const toMinKey = (): MinKey => {
100
123
  return new MinKey();
101
124
  };
102
125
 
103
- const toMaxKey = () => {
126
+ const toMaxKey = (): MaxKey => {
104
127
  return new MaxKey();
105
128
  };
106
129
 
107
- const toUndefined = () => {
130
+ const toUndefined = (): undefined => {
108
131
  return undefined;
109
132
  };
110
133
 
111
- const toNull = () => {
134
+ const toNull = (): null => {
112
135
  return null;
113
136
  };
114
137
 
115
- const toBoolean = (object) => {
138
+ const toBoolean = (object: unknown): boolean => {
116
139
  if (isString(object)) {
117
140
  if (object.toLowerCase() === TRUE) {
118
141
  return true;
@@ -127,14 +150,14 @@ const toBoolean = (object) => {
127
150
  return false;
128
151
  };
129
152
 
130
- const toObject = (object) => {
153
+ const toObject = (object: unknown): Record<string, unknown> => {
131
154
  if (isPlainObject(object)) {
132
- return object;
155
+ return object as Record<string, unknown>;
133
156
  }
134
157
  return {};
135
158
  };
136
159
 
137
- const toArray = (object) => {
160
+ const toArray = (object: unknown): unknown[] => {
138
161
  if (isArray(object)) {
139
162
  return object;
140
163
  }
@@ -144,7 +167,7 @@ const toArray = (object) => {
144
167
  return [object];
145
168
  };
146
169
 
147
- const toInt32 = (object) => {
170
+ const toInt32 = (object: unknown): Int32 => {
148
171
  if (object === '-' || object === '') {
149
172
  throw new Error(`Value '${object}' is not a valid Int32 value`);
150
173
  }
@@ -155,7 +178,7 @@ const toInt32 = (object) => {
155
178
  throw new Error(`Value ${number} is outside the valid Int32 range`);
156
179
  };
157
180
 
158
- const toInt64 = (object) => {
181
+ const toInt64 = (object: unknown): Long => {
159
182
  if (object === '-' || object === '') {
160
183
  throw new Error(`Value '${object}' is not a valid Int64 value`);
161
184
  }
@@ -166,22 +189,24 @@ const toInt64 = (object) => {
166
189
  // when casting from int32 object(this will have object.value) or literal
167
190
  // (it will a typeof number) we can safely create object fromNumber, as it
168
191
  // will not be greater than JS's max value
169
- if (object.value || typeof object === 'number') {
192
+ if ((object as { value?: number })?.value || typeof object === 'number') {
170
193
  return Long.fromNumber(number);
171
- } else if (typeof object === 'object') {
194
+ } else if (
195
+ typeof object === 'object' &&
196
+ object !== null &&
197
+ 'toString' in object
198
+ ) {
172
199
  // to make sure we are still displaying Very Large numbers properly, convert
173
200
  // the current 'object' to a string
174
- return Long.fromString(object.toString());
201
+ return Long.fromString((object as { toString(): string }).toString());
175
202
  }
176
203
 
177
- return Long.fromString(object);
204
+ return Long.fromString(toString(object));
178
205
  }
179
- throw new Error(
180
- `Value ${object.toString()} is outside the valid Int64 range`
181
- );
206
+ throw new Error(`Value ${toString(object)} is outside the valid Int64 range`);
182
207
  };
183
208
 
184
- const toDouble = (object) => {
209
+ const toDouble = (object: unknown): Double => {
185
210
  if (object === '-' || object === '') {
186
211
  throw new Error(`Value '${object}' is not a valid Double value`);
187
212
  }
@@ -192,43 +217,56 @@ const toDouble = (object) => {
192
217
  return new Double(number);
193
218
  };
194
219
 
195
- const toDecimal128 = (object) => {
220
+ type BSONObject = {
221
+ _bsontype: string;
222
+ toString(): string;
223
+ valueOf(): number | string;
224
+ };
225
+
226
+ const toDecimal128 = (object: unknown): Decimal128 => {
196
227
  /*
197
228
  If converting a BSON Object, extract the value before converting to a string.
198
229
  */
199
- if (hasIn(object, BSON_TYPE) && NUMBER_TYPES.includes(object._bsontype)) {
200
- object = object._bsontype === LONG ? object.toString() : object.valueOf();
230
+ if (
231
+ hasIn(object, BSON_TYPE) &&
232
+ NUMBER_TYPES.includes(
233
+ (object as BSONObject)._bsontype as (typeof NUMBER_TYPES)[number]
234
+ )
235
+ ) {
236
+ const bsonObj = object as BSONObject;
237
+ object =
238
+ bsonObj._bsontype === LONG ? bsonObj.toString() : bsonObj.valueOf();
201
239
  }
202
- return Decimal128.fromString('' + object);
240
+ return Decimal128.fromString(toString(object));
203
241
  };
204
242
 
205
- const toObjectID = (object) => {
243
+ const toObjectID = (object: unknown): ObjectId => {
206
244
  if (!isString(object) || object === '') {
207
245
  return new ObjectId();
208
246
  }
209
247
  return ObjectId.createFromHexString(object);
210
248
  };
211
249
 
212
- const toBinary = (object) => {
250
+ const toBinary = (object: unknown): Binary => {
213
251
  const buffer = ArrayBuffer.isView(object)
214
- ? Buffer.from(object)
252
+ ? Buffer.from(object as Uint8Array)
215
253
  : Buffer.from(toString(object), 'utf8');
216
254
  return new Binary(buffer, Binary.SUBTYPE_DEFAULT);
217
255
  };
218
256
 
219
- const toRegex = (object) => {
220
- return new BSONRegExp('' + object);
257
+ const toRegex = (object: unknown): BSONRegExp => {
258
+ return new BSONRegExp(toString(object));
221
259
  };
222
260
 
223
- const toCode = (object) => {
224
- return new Code('' + object);
261
+ const toCode = (object: unknown): Code => {
262
+ return new Code(toString(object));
225
263
  };
226
264
 
227
- const toSymbol = (object) => {
228
- return new BSONSymbol('' + object);
265
+ const toSymbol = (object: unknown): BSONSymbol => {
266
+ return new BSONSymbol(toString(object));
229
267
  };
230
268
 
231
- const toTimestamp = (object) => {
269
+ const toTimestamp = (object: unknown): Timestamp => {
232
270
  const number = toNumber(object);
233
271
  return Timestamp.fromNumber(number);
234
272
  };
@@ -236,7 +274,9 @@ const toTimestamp = (object) => {
236
274
  /**
237
275
  * The functions to cast to a type.
238
276
  */
239
- const CASTERS = {
277
+ const CASTERS: {
278
+ [K in TypeCastTypes]: (object: unknown) => TypeCastMap[K];
279
+ } = {
240
280
  Array: toArray,
241
281
  Binary: toBinary,
242
282
  Boolean: toBoolean,
@@ -267,9 +307,9 @@ const TYPES = keys(CASTERS);
267
307
  * Checks if a string is an int32.
268
308
  */
269
309
  class Int32Check {
270
- test(string) {
310
+ test(string: string): boolean {
271
311
  if (NUMBER_REGEX.test(string)) {
272
- var value = toNumber(string);
312
+ const value = toNumber(string);
273
313
  return value >= BSON_INT32_MIN && value <= BSON_INT32_MAX;
274
314
  }
275
315
  return false;
@@ -280,7 +320,7 @@ class Int32Check {
280
320
  * Checks if a string is an int64.
281
321
  */
282
322
  class Int64Check {
283
- test(string) {
323
+ test(string: string): boolean {
284
324
  if (NUMBER_REGEX.test(string)) {
285
325
  return Number.isSafeInteger(toNumber(string));
286
326
  }
@@ -293,13 +333,9 @@ const INT64_CHECK = new Int64Check();
293
333
 
294
334
  /**
295
335
  * Gets the BSON type for a JS number.
296
- *
297
- * @param {Number} number - The number.
298
- *
299
- * @returns {String} The BSON type.
300
336
  */
301
- const numberToBsonType = (number) => {
302
- var string = toString(number);
337
+ const numberToBsonType = (number: number): TypeCastTypes => {
338
+ const string = toString(number);
303
339
  if (INT32_CHECK.test(string)) {
304
340
  return INT_32;
305
341
  } else if (INT64_CHECK.test(string)) {
@@ -314,40 +350,37 @@ const numberToBsonType = (number) => {
314
350
  class TypeChecker {
315
351
  /**
316
352
  * Cast the provided object to the desired type.
317
- *
318
- * @param {Object} object - The object to cast.
319
- * @param {String} type - The type.
320
- *
321
- * @returns {Object} The cast object.
322
353
  */
323
- cast(object, type) {
324
- var caster = CASTERS[type];
325
- var result = object;
354
+ cast<O = unknown, T extends string = string>(
355
+ object: O,
356
+ type: T
357
+ ): T extends TypeCastTypes ? TypeCastMap[T] : O {
358
+ const caster = CASTERS[type as keyof typeof CASTERS];
359
+ let result: unknown = object;
326
360
  if (caster) {
327
361
  result = caster(object);
328
362
  }
329
- return result === OBJECT_TYPE && result !== object ? EMPTY : result;
363
+ return (
364
+ result === OBJECT_TYPE && result !== object ? EMPTY : result
365
+ ) as T extends TypeCastTypes ? TypeCastMap[T] : O;
330
366
  }
331
367
 
332
368
  /**
333
369
  * Get the type for the object.
334
- *
335
- * @param {Object} object - The object.
336
- *
337
- * @returns {String} The object type.
338
370
  */
339
- type(object) {
371
+ type(object: unknown): TypeCastTypes {
340
372
  if (hasIn(object, BSON_TYPE)) {
341
- if (object._bsontype === LONG) {
373
+ const bsonObj = object as { _bsontype: string };
374
+ if (bsonObj._bsontype === LONG) {
342
375
  return INT_64;
343
376
  }
344
- if (object._bsontype === OBJECT_ID) {
377
+ if (bsonObj._bsontype === OBJECT_ID) {
345
378
  return 'ObjectId';
346
379
  }
347
- if (object._bsontype === SYMBOL) {
380
+ if (bsonObj._bsontype === SYMBOL) {
348
381
  return 'BSONSymbol';
349
382
  }
350
- return object._bsontype;
383
+ return bsonObj._bsontype as TypeCastTypes;
351
384
  }
352
385
  if (isNumber(object)) {
353
386
  return numberToBsonType(object);
@@ -358,22 +391,20 @@ class TypeChecker {
358
391
  if (isArray(object)) {
359
392
  return ARRAY;
360
393
  }
361
- return Object.prototype.toString.call(object).replace(MATCH, '$1');
394
+ return Object.prototype.toString
395
+ .call(object)
396
+ .replace(MATCH, '$1') as TypeCastTypes;
362
397
  }
363
398
 
364
399
  /**
365
400
  * Get a list of types the object can be cast to.
366
- *
367
- * @param {Boolean} highPrecisionSupport - If Decimal128 is supported or not.
368
- *
369
- * @returns {Array} The available types.
370
401
  */
371
- castableTypes(highPrecisionSupport = false) {
402
+ castableTypes(highPrecisionSupport = false): TypeCastTypes[] {
372
403
  if (highPrecisionSupport === true) {
373
- return TYPES;
404
+ return TYPES as TypeCastTypes[];
374
405
  }
375
- return without(TYPES, DECIMAL_128);
406
+ return without(TYPES, DECIMAL_128) as TypeCastTypes[];
376
407
  }
377
408
  }
378
409
 
379
- module.exports = new TypeChecker();
410
+ export default new TypeChecker();
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "include": ["src/**/*"],
4
+ "exclude": ["./src/**/*.spec.*"]
5
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "@mongodb-js/tsconfig-compass/tsconfig.common.json",
3
+ "compilerOptions": {
4
+ "allowJs": true,
5
+ "lib": ["ES2020"],
6
+ "outDir": "dist"
7
+ },
8
+ "include": ["**/*"],
9
+ "exclude": ["node_modules", "lib"]
10
+ }
package/index.d.ts DELETED
@@ -1,51 +0,0 @@
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/index.js DELETED
@@ -1,2 +0,0 @@
1
- 'use strict';
2
- module.exports = require('./src/type-checker');