UNPKG

2.24 kBJavaScriptView Raw
1/* eslint no-console:0 */
2
3const {
4 isFunction,
5 isUndefined,
6 isPlainObject,
7 isArray,
8 isTypedArray,
9} = require('lodash');
10const { CLIENT_ALIASES } = require('./constants');
11
12// Check if the first argument is an array, otherwise uses all arguments as an
13// array.
14
15function 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
26function containsUndefined(mixed) {
27 let argContainsUndefined = false;
28
29 if (isTypedArray(mixed)) return false;
30
31 if (mixed && isFunction(mixed.toSQL)) {
32 //Any QueryBuilder or Raw will automatically be validated during compile.
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
54function 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
76function addQueryContext(Target) {
77 // Stores or returns (if called with no arguments) context passed to
78 // wrapIdentifier and postProcessResponse hooks
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
88function resolveClientNameWithAliases(clientName) {
89 return CLIENT_ALIASES[clientName] || clientName;
90}
91
92module.exports = {
93 addQueryContext,
94 containsUndefined,
95 normalizeArr,
96 resolveClientNameWithAliases,
97 getUndefinedIndices,
98};