|
5 | 5 | //=====================================================
|
6 | 6 |
|
7 | 7 | function parceFind(_levelA) {
|
8 |
| - |
9 |
| -//+++++++++++++++++++++++++++++++++++ work over Array |
10 |
| -//++++++++++++++++++++++++++++++++++++++++++++++++++++ |
11 |
| - |
12 |
| - let propsA = _levelA.map(function(currentValue, index) { |
13 |
| - |
14 |
| - let itemX = _levelA[index]; |
15 |
| - |
16 |
| - if( itemX instanceof Query){ |
17 |
| - return itemX.toString(); |
18 |
| - } else if ( ! Array.isArray(itemX) && "object" === typeof itemX ) { |
19 |
| - let propsA = Object.keys(itemX); |
20 |
| - if ( 1 !== propsA.length) { |
21 |
| - throw new RangeError("Alias objects should only have one value. was passed: "+JSON.stringify(itemX)); |
22 |
| - } |
23 |
| - let propS = propsA[0]; |
24 |
| - let item = itemX[propS]; |
25 |
| - // contributor: https://github.com/charlierudolph/graphql-query-builder/commit/878328e857e92d140f5ba6f7cfe07837620ec490 |
26 |
| - if (Array.isArray(item)) { |
27 |
| - return new Query(propS).find(item) |
28 |
| - } |
29 |
| - return `${propS} : ${item} `; |
30 |
| - } else if ( "string" === typeof itemX ) { |
31 |
| - return itemX; |
32 |
| - } else { |
33 |
| - throw new RangeError("cannot handle Find value of "+itemX); |
34 |
| - } |
35 |
| - }); |
36 |
| - |
37 |
| - return propsA.join(","); |
| 8 | + //+++++++++++++++++++++++++++++++++++ work over Array |
| 9 | + //++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 10 | + |
| 11 | + let propsA = _levelA.map(function(currentValue, index) { |
| 12 | + let itemX = _levelA[index]; |
| 13 | + |
| 14 | + if (itemX instanceof Query) { |
| 15 | + return itemX.toString(); |
| 16 | + } else if (!Array.isArray(itemX) && "object" === typeof itemX) { |
| 17 | + let propsA = Object.keys(itemX); |
| 18 | + if (1 !== propsA.length) { |
| 19 | + throw new RangeError( |
| 20 | + "Alias objects should only have one value. was passed: " + |
| 21 | + JSON.stringify(itemX) |
| 22 | + ); |
| 23 | + } |
| 24 | + let propS = propsA[0]; |
| 25 | + let item = itemX[propS]; |
| 26 | + // contributor: https://github.com/charlierudolph/graphql-query-builder/commit/878328e857e92d140f5ba6f7cfe07837620ec490 |
| 27 | + if (Array.isArray(item)) { |
| 28 | + return new Query(propS).find(item); |
| 29 | + } |
| 30 | + return `${propS} : ${item} `; |
| 31 | + } else if ("string" === typeof itemX) { |
| 32 | + return itemX; |
| 33 | + } else { |
| 34 | + throw new RangeError("cannot handle Find value of " + itemX); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + return propsA.join(","); |
38 | 39 | }
|
39 | 40 |
|
40 | 41 | //=====================================================
|
41 | 42 | //=================================== get GraphQL Value
|
42 | 43 | //=====================================================
|
43 | 44 |
|
44 | 45 | function getGraphQLValue(value) {
|
45 |
| - if ("string" === typeof value) { |
46 |
| - value = JSON.stringify(value); |
47 |
| - } else if (Array.isArray(value)) { |
48 |
| - value = value.map(item => { |
49 |
| - return getGraphQLValue(item); |
50 |
| - }).join(); |
51 |
| - value = `[${value}]`; |
52 |
| - } else if ("object" === typeof value) { |
53 |
| - /*if (value.toSource) |
| 46 | + if ("string" === typeof value) { |
| 47 | + value = JSON.stringify(value); |
| 48 | + } else if (Array.isArray(value)) { |
| 49 | + value = value |
| 50 | + .map(item => { |
| 51 | + return getGraphQLValue(item); |
| 52 | + }) |
| 53 | + .join(); |
| 54 | + value = `[${value}]`; |
| 55 | + } else if ("object" === typeof value) { |
| 56 | + /*if (value.toSource) |
54 | 57 | value = value.toSource().slice(2,-2);
|
55 | 58 | else*/
|
56 |
| - value = objectToString(value); |
57 |
| - //console.error("No toSource!!",value); |
58 |
| - } |
59 |
| - return value; |
| 59 | + value = objectToString(value); |
| 60 | + //console.error("No toSource!!",value); |
| 61 | + } |
| 62 | + return value; |
60 | 63 | }
|
61 | 64 |
|
62 | 65 | function objectToString(obj) {
|
63 |
| - |
64 | 66 | let sourceA = [];
|
65 |
| - |
66 |
| - for(let prop in obj){ |
| 67 | + |
| 68 | + for (let prop in obj) { |
67 | 69 | if ("function" === typeof obj[prop]) {
|
68 | 70 | continue;
|
69 | 71 | }
|
70 |
| - // if ("object" === typeof obj[prop]) { |
71 |
| - sourceA.push(`${prop}:${getGraphQLValue(obj[prop])}`); |
72 |
| - // } else { |
73 |
| - // sourceA.push(`${prop}:${obj[prop]}`); |
74 |
| - // } |
| 72 | + // if ("object" === typeof obj[prop]) { |
| 73 | + sourceA.push(`${prop}:${getGraphQLValue(obj[prop])}`); |
| 74 | + // } else { |
| 75 | + // sourceA.push(`${prop}:${obj[prop]}`); |
| 76 | + // } |
75 | 77 | }
|
76 | 78 | return `{${sourceA.join()}}`;
|
77 | 79 | }
|
78 | 80 |
|
79 |
| - |
80 |
| - |
81 |
| - |
82 | 81 | //=====================================================
|
83 | 82 | //========================================= Query Class
|
84 | 83 | //=====================================================
|
85 | 84 |
|
86 |
| -function Query(_fnNameS, _aliasS_OR_Filter){ |
87 |
| - |
88 |
| - this.fnNameS = _fnNameS; |
89 |
| - this.headA = []; |
90 |
| - |
91 |
| - this.filter = (filtersO) => { |
92 |
| - |
93 |
| - for(let propS in filtersO){ |
94 |
| - if ("function" === typeof filtersO[propS]) { |
95 |
| - continue; |
96 |
| - } |
97 |
| - let val = getGraphQLValue(filtersO[propS]); |
98 |
| - if ("{}" === val) { |
99 |
| - continue; |
100 |
| - } |
101 |
| - this.headA.push( `${propS}:${val}` ); |
102 |
| - } |
103 |
| - return this; |
104 |
| - }; |
105 |
| - |
106 |
| - if ("string" === typeof _aliasS_OR_Filter) { |
107 |
| - this.aliasS = _aliasS_OR_Filter; |
108 |
| - } else if ("object" === typeof _aliasS_OR_Filter) { |
109 |
| - this.filter(_aliasS_OR_Filter); |
110 |
| - } else if (undefined === _aliasS_OR_Filter && 2 === arguments.length){ |
111 |
| - throw new TypeError("You have passed undefined as Second argument to 'Query'"); |
112 |
| - } else if (undefined !== _aliasS_OR_Filter){ |
113 |
| - throw new TypeError("Second argument to 'Query' should be an alias name(String) or filter arguments(Object). was passed "+_aliasS_OR_Filter); |
| 85 | +function Query(_fnNameS, _aliasS_OR_Filter) { |
| 86 | + this.fnNameS = _fnNameS; |
| 87 | + this.headA = []; |
| 88 | + |
| 89 | + this.filter = filtersO => { |
| 90 | + for (let propS in filtersO) { |
| 91 | + if ("function" === typeof filtersO[propS]) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + let val = getGraphQLValue(filtersO[propS]); |
| 95 | + if ("{}" === val) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + this.headA.push(`${propS}:${val}`); |
114 | 99 | }
|
| 100 | + return this; |
| 101 | + }; |
| 102 | + |
| 103 | + if ("string" === typeof _aliasS_OR_Filter) { |
| 104 | + this.aliasS = _aliasS_OR_Filter; |
| 105 | + } else if ("object" === typeof _aliasS_OR_Filter) { |
| 106 | + this.filter(_aliasS_OR_Filter); |
| 107 | + } else if (undefined === _aliasS_OR_Filter && 2 === arguments.length) { |
| 108 | + throw new TypeError( |
| 109 | + "You have passed undefined as Second argument to 'Query'" |
| 110 | + ); |
| 111 | + } else if (undefined !== _aliasS_OR_Filter) { |
| 112 | + throw new TypeError( |
| 113 | + "Second argument to 'Query' should be an alias name(String) or filter arguments(Object). was passed " + |
| 114 | + _aliasS_OR_Filter |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + this.setAlias = _aliasS => { |
| 119 | + this.aliasS = _aliasS; |
| 120 | + return this; |
| 121 | + }; |
115 | 122 |
|
116 |
| - this.setAlias = (_aliasS) =>{ |
117 |
| - this.aliasS = _aliasS; |
118 |
| - return this; |
119 |
| - }; |
120 |
| - |
121 |
| - this.find = function(findA) { // THIS NEED TO BE A "FUNCTION" to scope 'arguments' |
122 |
| - if( ! findA){ |
123 |
| - throw new TypeError("find value can not be >>falsy<<"); |
124 |
| - } |
125 |
| - // if its a string.. it may have other values |
126 |
| - // else it sould be an Object or Array of maped values |
127 |
| - this.bodyS = parceFind((Array.isArray(findA)) ? findA : Array.from(arguments)); |
128 |
| - return this; |
129 |
| - }; |
| 123 | + this.find = function(findA) { |
| 124 | + // THIS NEED TO BE A "FUNCTION" to scope 'arguments' |
| 125 | + if (!findA) { |
| 126 | + throw new TypeError("find value can not be >>falsy<<"); |
| 127 | + } |
| 128 | + // if its a string.. it may have other values |
| 129 | + // else it sould be an Object or Array of maped values |
| 130 | + this.bodyS = parceFind( |
| 131 | + Array.isArray(findA) ? findA : Array.from(arguments) |
| 132 | + ); |
| 133 | + return this; |
| 134 | + }; |
130 | 135 | }
|
131 | 136 |
|
132 | 137 | //=====================================================
|
133 | 138 | //===================================== Query prototype
|
134 | 139 | //=====================================================
|
135 | 140 |
|
136 | 141 | Query.prototype = {
|
137 |
| - |
138 |
| - toString : function(){ |
139 |
| - if (undefined === this.bodyS) { |
140 |
| - throw new ReferenceError("return properties are not defined. use the 'find' function to defined them"); |
141 |
| - } |
142 |
| - |
143 |
| - return `${ (this.aliasS) ? (this.aliasS + ":") : "" } ${this.fnNameS } ${ (0 < this.headA.length)?"("+this.headA.join(",")+")":"" } { ${ this.bodyS } }`; |
| 142 | + toString: function() { |
| 143 | + if (undefined === this.bodyS) { |
| 144 | + throw new ReferenceError( |
| 145 | + "return properties are not defined. use the 'find' function to defined them" |
| 146 | + ); |
144 | 147 | }
|
| 148 | + |
| 149 | + return `${this.aliasS ? this.aliasS + ":" : ""} ${this.fnNameS} ${ |
| 150 | + 0 < this.headA.length ? "(" + this.headA.join(",") + ")" : "" |
| 151 | + } { ${this.bodyS} }`; |
| 152 | + } |
145 | 153 | };
|
146 | 154 |
|
147 | 155 | module.exports = Query;
|
0 commit comments