UNPKG

1.92 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
3// Node module: @loopback/repository
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.resolveType = exports.isBuiltinType = exports.Null = exports.isTypeResolver = void 0;
8/**
9 * A function that checks whether a function is a TypeResolver or not.
10 * @param fn - The value to check.
11 */
12function isTypeResolver(
13// eslint-disable-next-line @typescript-eslint/no-explicit-any
14fn) {
15 // 1. A type provider must be a function
16 if (typeof fn !== 'function')
17 return false;
18 // 2. A class constructor is not a type provider
19 if (/^class/.test(fn.toString()))
20 return false;
21 // 3. Built-in types like Date & Array are not type providers
22 if (isBuiltinType(fn))
23 return false;
24 // TODO(bajtos): support model classes defined via ES5 constructor function
25 return true;
26}
27exports.isTypeResolver = isTypeResolver;
28/**
29 * A boxed type for `null`
30 */
31// eslint-disable-next-line @typescript-eslint/naming-convention
32function Null() {
33 return null;
34}
35exports.Null = Null;
36/**
37 * Check if the provided function is a built-in type provided by JavaScript
38 * and/or Node.js. E.g. `Number`, `Array`, `Buffer`, etc.
39 */
40function isBuiltinType(fn) {
41 return (
42 // scalars
43 fn === Number ||
44 fn === Boolean ||
45 fn === String ||
46 // objects
47 fn === Object ||
48 fn === Array ||
49 fn === Date ||
50 fn === RegExp ||
51 fn === Buffer ||
52 fn === Null ||
53 // function as a type
54 fn === Function);
55}
56exports.isBuiltinType = isBuiltinType;
57function resolveType(fn) {
58 return isTypeResolver(fn) ? fn() : fn;
59}
60exports.resolveType = resolveType;
61//# sourceMappingURL=type-resolver.js.map
\No newline at end of file