Skip to content

Commit 05d1bdc

Browse files
committed
fix(and and where): and and where expression body fixed
1 parent dce2e59 commit 05d1bdc

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

src/SelectQL.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,40 @@ export class SelectQL implements ISelector {
3030
* @returns extracted array like object
3131
*/
3232
where(expressionKey: any, expressionOperator: Operators, expressionValue: any): this {
33+
let filtered = this._filteredBody(expressionKey, expressionOperator, expressionValue);
34+
if (filtered) {
35+
this.data = filtered;
36+
// console.log(returned, 'retu');
37+
return this;
38+
} else {
39+
throw new Error('WHERE expression not correctly passed!');
40+
}
41+
}
42+
43+
/**
44+
* another predicate which eventually calls where
45+
* @param expressionKey string or any key
46+
* @param expressionOperator Operator type operator
47+
* @param expressionValue string or any value
48+
* @returns extracted array like object
49+
*/
50+
and(expressionKey: any, expressionOperator: Operators, expressionValue: any): this {
51+
let filtered = this._filteredBody(expressionKey, expressionOperator, expressionValue);
52+
if (filtered) {
53+
this.data = filtered;
54+
// console.log(returned, 'retu');
55+
return this;
56+
} else {
57+
throw new Error('AND expression not correctly passed!');
58+
}
59+
}
60+
61+
_filteredBody(expressionKey: any, expressionOperator: Operators, expressionValue: any): this {
3362
let returned = this.data;
3463

35-
// checks if any of the where clause is key/operator or value is empty
64+
// checks if any of the where/and clause is key/operator or value is empty
3665
if (util.isEmpty(expressionKey) || util.isEmpty(expressionOperator) || util.isEmpty(expressionValue)) {
37-
throw new Error('WHERE expression not provided correctly!');
66+
throw new Error('Expression mandatory syntax missing!');
3867
}
3968

4069
if (expressionOperator == Operators.EQUAL) {
@@ -55,13 +84,7 @@ export class SelectQL implements ISelector {
5584
returned = this.data.filter(o => o[expressionKey] <= expressionValue);
5685
}
5786

58-
if (returned) {
59-
this.data = returned;
60-
// console.log(returned, 'retu');
61-
return this;
62-
} else {
63-
throw new Error('No correct WHERE expression found!');
64-
}
87+
return returned;
6588
}
6689

6790
/**
@@ -75,18 +98,7 @@ export class SelectQL implements ISelector {
7598
return this;
7699
}
77100

78-
/**
79-
* another predicate which eventually calls where
80-
* @param expressionKey string or any key
81-
* @param expressionOperator Operator type operator
82-
* @param expressionValue string or any value
83-
* @returns extracted array like object
84-
*/
85-
and(expressionKey: any, expressionOperator: Operators, expressionValue: any): this {
86-
let returnedAnd = this.where(expressionKey, expressionOperator, expressionValue);
87-
this.data = returnedAnd;
88-
return this;
89-
}
101+
90102

91103
/**
92104
* Creates an array of unique values, taking an iteratee to compute uniqueness with the provided key

0 commit comments

Comments
 (0)