-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
300 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { IIterator } from "../intefaces/iterator.interface"; | ||
|
||
export class AnyClause<T> implements IIterator<T> { | ||
|
||
_iterator: (item: T) => boolean; | ||
|
||
execute(source: T[] | any[]): T[] | T | any { | ||
if (!source) return source.length; | ||
else { | ||
return (source as T[]).every((x) => { | ||
return this._iterator(x); | ||
}); | ||
} | ||
} | ||
|
||
constructor(func: (item: T) => boolean) { | ||
this._iterator = func; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { IIterator } from "../intefaces/iterator.interface"; | ||
import { SelectClause } from "./select"; | ||
|
||
export class AvarageClause<T> implements IIterator<T> { | ||
|
||
_iterator: (item: T) => any; | ||
|
||
execute(source: any[] | T[]): T[] | T | any { | ||
if (!source) return 0; | ||
|
||
let _result = new SelectClause(this._iterator).execute(source) as number[]; | ||
|
||
if (!_result) return 0; | ||
|
||
if (!Number.isNaN(_result[0])) throw new Error("Avarage operator need type of number"); | ||
|
||
return (_result.reduce((a, b) => a + b, 0)) / _result.length; | ||
} | ||
|
||
constructor(func?: (item: T) => any) { | ||
this._iterator = func; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { IIterator } from "../intefaces/iterator.interface"; | ||
import { SelectClause } from "./select"; | ||
|
||
export class MaxClause<T> implements IIterator<T> { | ||
|
||
_iterator: (item: T) => any; | ||
|
||
execute(source: any[] | T[]): T[] | T | any { | ||
if (!source) return 0; | ||
|
||
let _result = new SelectClause(this._iterator).execute(source) as number[]; | ||
|
||
if (!_result) return 0; | ||
|
||
if (!Number.isNaN(_result[0])) throw new Error("Sum operator require type of number"); | ||
|
||
return Math.max(..._result); | ||
} | ||
|
||
constructor(func?: (item: T) => any) { | ||
this._iterator = func; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { IIterator } from "../intefaces/iterator.interface"; | ||
import { SelectClause } from "./select"; | ||
|
||
export class MinClause<T> implements IIterator<T> { | ||
|
||
_iterator: (item: T) => any; | ||
|
||
execute(source: any[] | T[]): T[] | T | any { | ||
if (!source) return 0; | ||
|
||
let _result = new SelectClause(this._iterator).execute(source) as number[]; | ||
|
||
if (!_result) return 0; | ||
|
||
if (!Number.isNaN(_result[0])) throw new Error("Sum operator require type of number"); | ||
|
||
return Math.min(..._result); | ||
} | ||
|
||
constructor(func?: (item: T) => any) { | ||
this._iterator = func; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { IIterator } from "../intefaces/iterator.interface"; | ||
import { SelectClause } from "./select"; | ||
|
||
export class OrderByClause<T> implements IIterator<T> { | ||
|
||
_iterator: (item: T) => any; | ||
|
||
execute(source: T[]): T[] | any[] { | ||
|
||
if (!source) return null; | ||
|
||
// Object contains all data in array follow by [index] : {object} | ||
let _tmp = {}; | ||
|
||
source.forEach((x: T, i: number) => { | ||
_tmp[i] = x; | ||
}); | ||
|
||
// Make lookup list base on input iterator function | ||
let _result = (source as T[]).map((x: T, index: number) => { | ||
return { | ||
value: (this._iterator(x)), | ||
index: index | ||
} | ||
}); | ||
|
||
if (!_result) return null; | ||
|
||
// Sort on loopkup list | ||
_result.sort((a: { value, index }, b: { value, index }) => { | ||
if (a.value < b.value) return -1; | ||
if (a.value > b.value) return 1; | ||
return 0; | ||
}); | ||
|
||
// Re-map to original list | ||
return _result.map((x: { value, index }) => { | ||
return _tmp[x.index]; | ||
}); | ||
} | ||
|
||
constructor(func: (item: T) => any) { | ||
this._iterator = func; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { IIterator } from "../intefaces/iterator.interface"; | ||
import { OrderByClause } from "./orderBy"; | ||
|
||
export class OrderByDescendingClause<T> implements IIterator<T> { | ||
|
||
_iterator: (item: T) => any; | ||
|
||
execute(source: T[]): T[] | any[] { | ||
|
||
if (!source) return null; | ||
|
||
let _result = new OrderByClause(this._iterator).execute(source); | ||
|
||
return _result.reverse(); | ||
} | ||
|
||
constructor(func: (item: T) => any) { | ||
this._iterator = func; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { IIterator } from "../intefaces/iterator.interface"; | ||
import { WhereClause } from "./where"; | ||
|
||
export class SingleClause<T> implements IIterator<T> { | ||
|
||
_iterator: (item: T) => boolean; | ||
|
||
execute(source: any[] | T[]): T { | ||
if (!this._iterator) { | ||
return this.getSingle(source); | ||
} | ||
else { | ||
let _result = new WhereClause(this._iterator).execute(source); | ||
return this.getSingle(_result); | ||
} | ||
} | ||
|
||
private getSingle(src: any[] | T[]): T | any { | ||
if (src.length > 1) throw new Error("The collection does not contain exactly one element"); | ||
else if (src.length === 1) | ||
return src[0]; | ||
} | ||
|
||
constructor(func?: (item: T) => boolean) { | ||
this._iterator = func; | ||
} | ||
} |
Oops, something went wrong.