1 |
|
2 |
|
3 | const {
|
4 | isFunction,
|
5 | isUndefined,
|
6 | isPlainObject,
|
7 | isArray,
|
8 | isTypedArray,
|
9 | } = require('lodash');
|
10 | const { CLIENT_ALIASES } = require('./constants');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function normalizeArr() {
|
16 | const args = new Array(arguments.length);
|
17 | for (let i = 0; i < args.length; i++) {
|
18 | args[i] = arguments[i];
|
19 | }
|
20 | if (Array.isArray(args[0])) {
|
21 | return args[0];
|
22 | }
|
23 | return args;
|
24 | }
|
25 |
|
26 | function containsUndefined(mixed) {
|
27 | let argContainsUndefined = false;
|
28 |
|
29 | if (isTypedArray(mixed)) return false;
|
30 |
|
31 | if (mixed && isFunction(mixed.toSQL)) {
|
32 |
|
33 | return argContainsUndefined;
|
34 | }
|
35 |
|
36 | if (isArray(mixed)) {
|
37 | for (let i = 0; i < mixed.length; i++) {
|
38 | if (argContainsUndefined) break;
|
39 | argContainsUndefined = containsUndefined(mixed[i]);
|
40 | }
|
41 | } else if (isPlainObject(mixed)) {
|
42 | Object.keys(mixed).forEach((key) => {
|
43 | if (!argContainsUndefined) {
|
44 | argContainsUndefined = containsUndefined(mixed[key]);
|
45 | }
|
46 | });
|
47 | } else {
|
48 | argContainsUndefined = isUndefined(mixed);
|
49 | }
|
50 |
|
51 | return argContainsUndefined;
|
52 | }
|
53 |
|
54 | function getUndefinedIndices(mixed) {
|
55 | const indices = [];
|
56 |
|
57 | if (Array.isArray(mixed)) {
|
58 | mixed.forEach((item, index) => {
|
59 | if (containsUndefined(item)) {
|
60 | indices.push(index);
|
61 | }
|
62 | });
|
63 | } else if (isPlainObject(mixed)) {
|
64 | Object.keys(mixed).forEach((key) => {
|
65 | if (containsUndefined(mixed[key])) {
|
66 | indices.push(key);
|
67 | }
|
68 | });
|
69 | } else {
|
70 | indices.push(0);
|
71 | }
|
72 |
|
73 | return indices;
|
74 | }
|
75 |
|
76 | function addQueryContext(Target) {
|
77 |
|
78 |
|
79 | Target.prototype.queryContext = function(context) {
|
80 | if (isUndefined(context)) {
|
81 | return this._queryContext;
|
82 | }
|
83 | this._queryContext = context;
|
84 | return this;
|
85 | };
|
86 | }
|
87 |
|
88 | function resolveClientNameWithAliases(clientName) {
|
89 | return CLIENT_ALIASES[clientName] || clientName;
|
90 | }
|
91 |
|
92 | module.exports = {
|
93 | addQueryContext,
|
94 | containsUndefined,
|
95 | normalizeArr,
|
96 | resolveClientNameWithAliases,
|
97 | getUndefinedIndices,
|
98 | };
|