-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
107 lines (77 loc) · 2.38 KB
/
logic.js
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
const main = {
OLSKCollectionAPI (params) {
if (typeof params !== 'object' || params === null) {
throw new Error('OLSKErrorInputNotValid');
}
if (typeof params.OLSKCollectionSortFunction !== 'function') {
throw new Error('OLSKErrorInputNotValid');
}
if (typeof params._OLSKCollectionKeyFunction !== 'function') {
throw new Error('OLSKErrorInputNotValid');
}
if (typeof params.OLSKCollectionItems !== 'undefined') {
if (!Array.isArray(params.OLSKCollectionItems)) {
throw new Error('OLSKErrorInputNotValid');
}
} else {
params.OLSKCollectionItems = [];
}
if (typeof params.OLSKCollectionDispatchChange !== 'undefined') {
if (typeof params.OLSKCollectionDispatchChange !== 'function') {
throw new Error('OLSKErrorInputNotValid');
}
}
const mod = {
ValueItemsAll (inputData) {
params.OLSKCollectionItems.splice(...[0, params.OLSKCollectionItems.length].concat(inputData));
params.OLSKCollectionDispatchChange && params.OLSKCollectionDispatchChange();
},
};
const api = {
// DATA
OLSKCollectionDataItemsAll() {
return params.OLSKCollectionItems.slice();
},
// CONTROL
OLSKCollectionInsert (inputData) {
mod.ValueItemsAll([inputData].concat(params.OLSKCollectionItems));
return inputData;
},
OLSKCollectionUpdate (inputData) {
mod.ValueItemsAll(params.OLSKCollectionItems.map(function (e) {
return params._OLSKCollectionKeyFunction(e) === params._OLSKCollectionKeyFunction(inputData) ? inputData : e;
}));
return inputData;
},
OLSKCollectionRemove (inputData) {
mod.ValueItemsAll(params.OLSKCollectionItems.filter(function (e) {
return params._OLSKCollectionKeyFunction(e) !== params._OLSKCollectionKeyFunction(inputData);
}));
return inputData;
},
OLSKCollectionSort () {
mod.ValueItemsAll(params.OLSKCollectionItems.sort(params.OLSKCollectionSortFunction));
},
_OLSKCollectionDebugReassign (inputData) {
params.OLSKCollectionItems = inputData;
},
};
return api;
},
OLSKCollectionConstrainIndex (param1, param2) {
if (!Array.isArray(param1)) {
throw new Error('OLSKErrorInputNotValid');
}
if (typeof param2 !== 'number') {
throw new Error('OLSKErrorInputNotValid');
}
if (param2 < 0) {
return param1.length - 1;
}
if (param2 >= param1.length) {
return 0;
}
return param2;
},
};
export default main;