Skip to content

Commit 638d8cc

Browse files
author
Nadeem Ur-Rehman
committed
feat: fix SelectQL to be used properly as a chainable methods
BREAKING CHANGE: it might break if older version might used
1 parent fb6ff83 commit 638d8cc

File tree

2 files changed

+72
-65
lines changed

2 files changed

+72
-65
lines changed

src/SelectQL.ts

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import { SelectStatement, ISelector, Selector, ISelectBuilder, Operators } from './Selector'
2+
import { SelectStatement, ISelector, Selector, ISelectBuilder, Operators, whereClauseArray } from './Selector'
33
import * as util from './utils'
44

55
export class SelectQL implements ISelectBuilder {
@@ -15,107 +15,109 @@ export class SelectQL implements ISelectBuilder {
1515
this.selector = new Selector();
1616
if (util.checkArray(selectorInput)) {
1717
this.isArray = true;
18-
this.selectType = new ArraySelector(selectorInput);
19-
}
18+
// this.selectType = new ArraySelector(selectorInput);
19+
}
2020
} else {
2121
throw new Error('Provide array or object as an input to select');
2222
}
2323
}
2424

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-
}
5325

5426
/**
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
5832
*/
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;
6135

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) {
6342
// 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;
6645
}), 1);
67-
} else if (operator == Operators.NOT_EQUAL) {
46+
} else if (expressionOperator == Operators.NOT_EQUAL) {
6847
// 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);
7857
}
7958

8059
if (returned) {
8160
// console.log(returned, 'retu');
8261
return returned
8362
} else {
84-
throw 'No Key found!';
63+
throw new Error('No correct WHERE expression found!');
8564
}
8665
}
8766

88-
/**
67+
/**
8968
* Creates a new array concatenating array with any additional arrays and/or values.
9069
* @param concatWith array or value
9170
* @returns this
9271
*/
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;
9575
}
9676

9777
/**
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
10183
*/
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);
10486
}
10587

106-
/**
88+
/**
10789
* Creates an array of unique values, taking an iteratee to compute uniqueness with the provided key
10890
* @param key string
10991
* @returns
11092
*/
11193
uniqueByKey(key: string): this {
11294
let uniquArr: any = [];
113-
this.selectorArray.forEach((value, index) => {
95+
this.selectType.forEach((value, index) => {
11496
if (uniquArr.indexOf(value[key]) === -1) {
11597
uniquArr.push(value);
11698

11799
}
118100
});
119101
return uniquArr;
120102
}
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+
121123
}

src/Selector.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11

22
export interface SelectStatement {
33
where: any,
4-
concat: any,
4+
join: any,
55
and: any,
66
uniqueByKey: any
77
}
88

99
export interface ISelector {
10-
where(key: any, operator: Operators, value: any, notOption?: boolean): this
11-
concat(concatWith: any): this
10+
where(key: any, operator: Operators, value: any): this
11+
join(concatWith: any): this
1212
and(key: any, operator: Operators, value: any): this
1313
uniqueByKey(key: string): this
1414
}
15+
export interface whereClauseArray {
16+
key: any;
17+
operator: Operators;
18+
value: any;
19+
}
1520

1621
export interface ISelectBuilder extends ISelector {
1722
build(): Selector
@@ -22,8 +27,8 @@ export class Selector {
2227
}
2328

2429
export enum Operators {
25-
EQUAL = '==',
26-
NOT_EQUAL = '!==',
30+
EQUAL = '=',
31+
NOT_EQUAL = '!=',
2732
GREATER_THAN = '>',
2833
GREATER_EQUAL = '>=',
2934
LESS_THEN = '<',

0 commit comments

Comments
 (0)