1
1
2
- import { SelectStatement , ISelector , Selector , ISelectBuilder , Operators } from './Selector'
2
+ import { SelectStatement , ISelector , Selector , ISelectBuilder , Operators , whereClauseArray } from './Selector'
3
3
import * as util from './utils'
4
4
5
5
export class SelectQL implements ISelectBuilder {
@@ -15,107 +15,109 @@ export class SelectQL implements ISelectBuilder {
15
15
this . selector = new Selector ( ) ;
16
16
if ( util . checkArray ( selectorInput ) ) {
17
17
this . isArray = true ;
18
- this . selectType = new ArraySelector ( selectorInput ) ;
19
- }
18
+ // this.selectType = new ArraySelector(selectorInput);
19
+ }
20
20
} else {
21
21
throw new Error ( 'Provide array or object as an input to select' ) ;
22
22
}
23
23
}
24
24
25
- where ( whereClause : any ) : this {
26
- return ! util . isEmpty ( whereClause ) ? this . selectType . where ( whereClause ) : this ;
27
- }
28
-
29
- concat ( concatWith : any ) : this {
30
- return ! util . isEmpty ( concatWith ) ? this . selectType . concat ( concatWith ) : this ;
31
- }
32
-
33
- and ( andOption : any ) : this {
34
- return ! util . isEmpty ( andOption ) ? this . selectType . where ( andOption ) : this ;
35
- }
36
-
37
- uniqueByKey ( key : string ) : this {
38
- return ! util . isEmpty ( key ) ? this . selectType . uniqueByKey ( key ) : this ;
39
- }
40
-
41
- build ( ) : Selector {
42
- return this . selector ;
43
- }
44
-
45
- }
46
-
47
-
48
- class ArraySelector implements ISelector {
49
- selectorArray : any ;
50
- constructor ( select : any ) {
51
- this . selectorArray = select ;
52
- }
53
25
54
26
/**
55
- * Like a map to iterate and pick an item by key
56
- * @param whereClause any type
57
- * @returns this
27
+ * Like a map to iterate and pick an item by key and conditionally change array like object
28
+ * @param expressionKey string or any key
29
+ * @param expressionOperator Operator type operator
30
+ * @param expressionValue string or any value
31
+ * @returns extracted array like object
58
32
*/
59
- where ( key : any , operator : Operators , value : any , notOption ?: boolean ) : this {
60
- let returned = this . selectorArray ;
33
+ where ( expressionKey : any , expressionOperator : Operators , expressionValue : any ) {
34
+ let returned = this . selectType ;
61
35
62
- if ( operator == Operators . EQUAL ) {
36
+ // checks if any of the where clause is key/operator or value is empty
37
+ if ( util . isEmpty ( expressionKey ) || util . isEmpty ( expressionOperator ) || util . isEmpty ( expressionValue ) ) {
38
+ throw new Error ( 'WHERE expression not provided correctly!' ) ;
39
+ }
40
+
41
+ if ( expressionOperator == Operators . EQUAL ) {
63
42
// Returned only one item where condition met
64
- returned = this . selectorArray . splice ( this . selectorArray . findIndex ( ( o ) => {
65
- return o [ key ] == value ;
43
+ returned = this . selectType . splice ( this . selectType . findIndex ( ( o ) => {
44
+ return o [ expressionKey ] == expressionValue ;
66
45
} ) , 1 ) ;
67
- } else if ( operator == Operators . NOT_EQUAL ) {
46
+ } else if ( expressionOperator == Operators . NOT_EQUAL ) {
68
47
// Remove all element except the key mentioned
69
- returned = this . selectorArray . filter ( o => o [ key ] !== value ) ;
70
- } else if ( operator == Operators . GREATER_THAN ) {
71
- returned = this . selectorArray . filter ( o => o [ key ] > value ) ;
72
- } else if ( operator == Operators . GREATER_EQUAL ) {
73
- returned = this . selectorArray . filter ( o => o [ key ] >= value ) ;
74
- } else if ( operator == Operators . LESS_THEN ) {
75
- returned = this . selectorArray . filter ( o => o [ key ] < value ) ;
76
- } else if ( operator == Operators . LESS_THEN_EQUAL ) {
77
- returned = this . selectorArray . filter ( o => o [ key ] <= value ) ;
48
+ returned = this . selectType . filter ( o => o [ expressionKey ] !== expressionValue ) ;
49
+ } else if ( expressionOperator == Operators . GREATER_THAN ) {
50
+ returned = this . selectType . filter ( o => o [ expressionKey ] > expressionValue ) ;
51
+ } else if ( expressionOperator == Operators . GREATER_EQUAL ) {
52
+ returned = this . selectType . filter ( o => o [ expressionKey ] >= expressionValue ) ;
53
+ } else if ( expressionOperator == Operators . LESS_THEN ) {
54
+ returned = this . selectType . filter ( o => o [ expressionKey ] < expressionValue ) ;
55
+ } else if ( expressionOperator == Operators . LESS_THEN_EQUAL ) {
56
+ returned = this . selectType . filter ( o => o [ expressionKey ] <= expressionValue ) ;
78
57
}
79
58
80
59
if ( returned ) {
81
60
// console.log(returned, 'retu');
82
61
return returned
83
62
} else {
84
- throw 'No Key found!' ;
63
+ throw new Error ( 'No correct WHERE expression found!' ) ;
85
64
}
86
65
}
87
66
88
- /**
67
+ /**
89
68
* Creates a new array concatenating array with any additional arrays and/or values.
90
69
* @param concatWith array or value
91
70
* @returns this
92
71
*/
93
- concat ( concatWith : any ) : this {
94
- return this . selectorArray . concat ( concatWith ) ;
72
+
73
+ join ( concatWith : any ) : this {
74
+ return ! util . isEmpty ( concatWith ) ? this . selectType . concat ( concatWith ) : this ;
95
75
}
96
76
97
77
/**
98
- * option to filter by another predicate
99
- * @param andOption any
100
- * return this
78
+ * another predicate which eventually calls where
79
+ * @param expressionKey string or any key
80
+ * @param expressionOperator Operator type operator
81
+ * @param expressionValue string or any value
82
+ * @returns extracted array like object
101
83
*/
102
- and ( key : any , operator : Operators , value : any ) : this {
103
- return this . where ( key , operator , value ) ;
84
+ and ( expressionKey : any , expressionOperator : Operators , expressionValue : any ) {
85
+ return this . where ( expressionKey , expressionOperator , expressionValue ) ;
104
86
}
105
87
106
- /**
88
+ /**
107
89
* Creates an array of unique values, taking an iteratee to compute uniqueness with the provided key
108
90
* @param key string
109
91
* @returns
110
92
*/
111
93
uniqueByKey ( key : string ) : this {
112
94
let uniquArr : any = [ ] ;
113
- this . selectorArray . forEach ( ( value , index ) => {
95
+ this . selectType . forEach ( ( value , index ) => {
114
96
if ( uniquArr . indexOf ( value [ key ] ) === - 1 ) {
115
97
uniquArr . push ( value ) ;
116
98
117
99
}
118
100
} ) ;
119
101
return uniquArr ;
120
102
}
103
+
104
+ /**
105
+ * if no condition met or null then client can return it's own input/object
106
+ * @param input
107
+ * @returns client's provided input.
108
+ */
109
+ orElse ( input : any ) {
110
+ if ( ! util . isEmpty ( input ) ) {
111
+ return input ;
112
+ }
113
+ }
114
+
115
+ /**
116
+ * builder function
117
+ * @returns final object.
118
+ */
119
+ build ( ) : Selector {
120
+ return this . selector ;
121
+ }
122
+
121
123
}
0 commit comments