This repository has been archived by the owner on Sep 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memoperation.ts
119 lines (103 loc) · 2.94 KB
/
memoperation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
export abstract class MemOperation {
abstract pipe(array: Array<any>): Promise<Array<any>>;
}
/**Distincts */
/**
* Checks all properties
*/
class FullMatchDistinctOp extends MemOperation {
pipe(array: any[]): Promise<any[]> {
return Promise.resolve(array.filter((item, index, arr) => {
return arr.findIndex((a) => {
if (a == null) return false;
for (let i in a) {
if (item[i] !== a[i]) return false;
}
return true;
}) === index;
}));
}
}
/**
* Checks only equality of references
*/
class ReferenceDistinctOp extends MemOperation {
pipe(array: any[]): Promise<any[]> {
return Promise.resolve(array.filter((item, index, arr) => {
return arr.findIndex((a) => a === item) === index;
}));
}
}
/**
* Checks only selected properties
*/
class OneProperyDistinctOp extends MemOperation {
constructor(private properties: Array<any>) {
super();
}
pipe(array: any[]): Promise<any[]> {
return Promise.resolve(array.filter((item, index, arr) => {
return arr.findIndex((a) => {
if (a == null) return false;
return this.properties.every((prop) => {
return item[prop] === a[prop];
})
}) === index;
}))
}
}
export class Distincs {
/**
* Checks all properties
*/
static fullMatch = new FullMatchDistinctOp();
/**
* Checks only equality of references
*/
static referenceMatch = new ReferenceDistinctOp();
/**
* Checks only selected properties
*/
static propertyMatch = function (...properties: Array<any>) {
let props = properties.length === 0 && Array.isArray(properties[0]) ? properties[0] : properties;
return new OneProperyDistinctOp(props);
}
}
/**
* Distinct model
* @param algorithm
*/
export function distinct(algorithm: MemOperation | Array<any> | string = Distincs.referenceMatch) {
if(typeof algorithm === "string") return Distincs.propertyMatch(algorithm);
if(Array.isArray(algorithm)) return Distincs.propertyMatch.apply(null,algorithm);
return algorithm;
}
/**
* Checks all properties
*/
export function fullDistinct() {
return Distincs.fullMatch;
}
class NotNullOp extends MemOperation {
pipe(array: any[]): Promise<any[]> {
return Promise.resolve(array.filter(x => x != null));
}
}
export function notNull() {
return new NotNullOp();
}
class MapOp extends MemOperation {
constructor(private mapFn: (item, index, array) => any) {
super();
}
pipe(array: any[]): Promise<any[]> {
return Promise.resolve(array.map(this.mapFn));
}
}
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @param mapFn
*/
export function map(mapFn: (item, index, array) => any) {
return new MapOp(mapFn);
}