Skip to content

Commit 9d0349d

Browse files
author
Nadeem Ur-Rehman
committed
fix: fix builder pattern
Fix builder selectorQL BREAKING CHANGE: Previous import syntax might not work
1 parent 54f1387 commit 9d0349d

File tree

2 files changed

+41
-33
lines changed

2 files changed

+41
-33
lines changed

src/SelectQL.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11

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

5+
/**
6+
* Select implementation
7+
*/
58
export class SelectQL implements ISelector {
69
// The Concrete Builder
7-
selector: Selector;
8-
selectedInput: any;
9-
isArray: boolean = false;
1010
data: any = [];
11+
private _isArray: boolean = true;
1112
constructor(selectorInput: any) {
1213

1314
if (util.checkArray(selectorInput) || util.checkObject(selectorInput)) {
14-
this.selector = new Selector();
1515
this.data = selectorInput;
1616
if (util.checkArray(selectorInput)) {
17-
this.isArray = true;
17+
this._isArray = true;
1818
}
1919
} else {
2020
throw new Error('Provide array or object as an input to select');
@@ -30,7 +30,7 @@ export class SelectQL implements ISelector {
3030
* @returns extracted array like object
3131
*/
3232
where(expressionKey: any, expressionOperator: Operators, expressionValue: any) : this {
33-
let returned = this.selectedInput;
33+
let returned = this.data;
3434

3535
// checks if any of the where clause is key/operator or value is empty
3636
if (util.isEmpty(expressionKey) || util.isEmpty(expressionOperator) || util.isEmpty(expressionValue)) {
@@ -39,26 +39,26 @@ export class SelectQL implements ISelector {
3939

4040
if (expressionOperator == Operators.EQUAL) {
4141
// Returned only one item where condition met
42-
returned = this.selectedInput.splice(this.selectedInput.findIndex((o) => {
42+
returned = this.data.splice(this.data.findIndex((o) => {
4343
return o[expressionKey] == expressionValue;
4444
}), 1);
4545
} else if (expressionOperator == Operators.NOT_EQUAL) {
4646
// Remove all element except the key mentioned
47-
returned = this.selectedInput.filter(o => o[expressionKey] !== expressionValue);
47+
returned = this.data.filter(o => o[expressionKey] !== expressionValue);
4848
} else if (expressionOperator == Operators.GREATER_THAN) {
49-
returned = this.selectedInput.filter(o => o[expressionKey] > expressionValue);
49+
returned = this.data.filter(o => o[expressionKey] > expressionValue);
5050
} else if (expressionOperator == Operators.GREATER_EQUAL) {
51-
returned = this.selectedInput.filter(o => o[expressionKey] >= expressionValue);
51+
returned = this.data.filter(o => o[expressionKey] >= expressionValue);
5252
} else if (expressionOperator == Operators.LESS_THEN) {
53-
returned = this.selectedInput.filter(o => o[expressionKey] < expressionValue);
53+
returned = this.data.filter(o => o[expressionKey] < expressionValue);
5454
} else if (expressionOperator == Operators.LESS_THEN_EQUAL) {
55-
returned = this.selectedInput.filter(o => o[expressionKey] <= expressionValue);
55+
returned = this.data.filter(o => o[expressionKey] <= expressionValue);
5656
}
5757

5858
if (returned) {
5959
this.data = returned;
6060
// console.log(returned, 'retu');
61-
return this.data;
61+
return this;
6262
} else {
6363
throw new Error('No correct WHERE expression found!');
6464
}
@@ -71,7 +71,8 @@ export class SelectQL implements ISelector {
7171
*/
7272

7373
join(concatWith: any): this {
74-
return this.data = !util.isEmpty(concatWith) ? this.selectedInput.concat(concatWith) : this;
74+
this.data = !util.isEmpty(concatWith) ? this.data.concat(concatWith) : this;
75+
return this;
7576
}
7677

7778
/**
@@ -82,7 +83,8 @@ export class SelectQL implements ISelector {
8283
* @returns extracted array like object
8384
*/
8485
and(expressionKey: any, expressionOperator: Operators, expressionValue: any): this {
85-
return this.data = this.where(expressionKey, expressionOperator, expressionValue);
86+
this.data = this.where(expressionKey, expressionOperator, expressionValue);
87+
return this;
8688
}
8789

8890
/**
@@ -92,30 +94,31 @@ export class SelectQL implements ISelector {
9294
*/
9395
uniqueByKey(key: string): this {
9496
let uniquArr: any = [];
95-
this.selectedInput.forEach((value, index) => {
97+
this.data.forEach((value, index) => {
9698
if (uniquArr.indexOf(value[key]) === -1) {
9799
uniquArr.push(value);
98100

99101
}
100102
});
101-
return this.data = uniquArr;
103+
return this;
102104
}
103105

104106
/**
105107
* if no condition met or null then client can return it's own input/object
106108
* @param input
107109
* @returns client's provided input.
108110
*/
109-
orElse(input: any) {
110-
return this.data = input;
111+
orElse(input: any): this {
112+
this.data = input;
113+
return this;
111114
}
112115

113116
/**
114117
* builder function
115118
* @returns final object.
116119
*/
117-
build(): Selector {
118-
return this.selector;
120+
build(): Select {
121+
return new Select(this);
119122
}
120123

121124
}

src/Selector.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
1+
import { SelectQL } from "./SelectQL";
22
export interface ISelector {
33
where(key: any, operator: Operators, value: any): this
44
join(concatWith: any): this
55
and(key: any, operator: Operators, value: any): this
66
uniqueByKey(key: string): this
7-
build(): Selector
7+
build(): Select
88
}
99

10-
export default class Selector {
11-
data: any = [] || {} || null;
12-
13-
construction(): any {
14-
return this.data;
15-
}
16-
}
17-
1810
export enum Operators {
1911
EQUAL = '==',
2012
NOT_EQUAL = '!=',
2113
GREATER_THAN = '>',
2214
GREATER_EQUAL = '>=',
2315
LESS_THEN = '<',
2416
LESS_THEN_EQUAL = '<=',
25-
}
17+
}
18+
19+
/**
20+
* The definition of the Builder Pattern is a separation of the
21+
* construction of a complex object from its representation.
22+
*
23+
*/
24+
export class Select {
25+
data: any[] = [];
26+
27+
constructor(selectQl: SelectQL) {
28+
this.data = selectQl.data;
29+
}
30+
}

0 commit comments

Comments
 (0)