-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlodash.example.object.subset.ts
301 lines (264 loc) · 9.03 KB
/
lodash.example.object.subset.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import * as _ from "lodash";
interface CodeValuePair {
code: string;
value: string;
};
export class Dummy {
private static combinationsAllowed4ServiceCode = {
snowRemoval: [
// Signs-related problems
[
{ code: "problem", value: "signs" },
{ code: "signsProblem", value: "noMatchWithMobileApp" }
],
[
{ code: "problem", value: "signs" },
{ code: "signsProblem", value: "delayTooShort" }
],
[
{ code: "problem", value: "signs" },
{ code: "signsProblem", value: "signsStillinPlace" }
],
// Snow-removal-related problems
[
{ code: "problem", value: "snowClearing" },
{ code: "snowClearingProblem", value: "roadNotCleared" }
],
[
{ code: "problem", value: "snowClearing" },
{ code: "snowClearingProblem", value: "roadPartlyCleared" }
],
[
{ code: "problem", value: "snowClearing" },
{ code: "snowClearingProblem", value: "sidewalkNotCleared" }
],
[
{ code: "problem", value: "snowClearing" },
{ code: "snowClearingProblem", value: "obstructedEntrance" }
]
],
slipperyRoadSidewalk: [
[
{ code: "problem", value: "road" },
{ code: "location", value: "busStop" },
{ code: "magnitude", value: "whereIndicatedOnly" }
],
[
{ code: "problem", value: "road" },
{ code: "location", value: "busStop" },
{ code: "magnitude", value: "wholeSection" }
],
[
{ code: "problem", value: "road" },
{ code: "location", value: "school" },
{ code: "magnitude", value: "whereIndicatedOnly" }
],
[
{ code: "problem", value: "road" },
{ code: "location", value: "school" },
{ code: "magnitude", value: "wholeSection" }
],
[
{ code: "problem", value: "road" },
{ code: "location", value: "hospital" },
{ code: "magnitude", value: "whereIndicatedOnly" }
],
[
{ code: "problem", value: "road" },
{ code: "location", value: "hospital" },
{ code: "magnitude", value: "wholeSection" }
],
[
{ code: "problem", value: "sidewalk" },
{ code: "location", value: "busStop" },
{ code: "magnitude", value: "whereIndicatedOnly" }
],
[
{ code: "problem", value: "sidewalk" },
{ code: "location", value: "busStop" },
{ code: "magnitude", value: "wholeSection" }
],
[
{ code: "problem", value: "sidewalk" },
{ code: "location", value: "school" },
{ code: "magnitude", value: "whereIndicatedOnly" }
],
[
{ code: "problem", value: "sidewalk" },
{ code: "location", value: "school" },
{ code: "magnitude", value: "wholeSection" }
],
[
{ code: "problem", value: "sidewalk" },
{ code: "location", value: "hospital" },
{ code: "magnitude", value: "whereIndicatedOnly" }
],
[
{ code: "problem", value: "sidewalk" },
{ code: "location", value: "hospital" },
{ code: "magnitude", value: "wholeSection" }
]
]
};
public isAttributesValid4ServiceCodeLodash(
serviceCode: string,
attributes: Array<CodeValuePair>
): boolean {
const serviceCodeCamelCase = _.camelCase(serviceCode);
const combinationsAllowed: Array<Array<CodeValuePair>> =
Dummy.combinationsAllowed4ServiceCode[serviceCodeCamelCase];
// Ensure that serviceCode is a valid one and attributes is provided...
if (!combinationsAllowed || !attributes) {
return false;
}
// this.aMethod();
// Must provides distinct codes. Otherwise, bizarre case would returns true
const distinctAttributes = _.uniqWith(attributes, (a, b) => a.code === b.code);
if (distinctAttributes.length !== attributes.length) {
return false;
}
return !!combinationsAllowed && combinationsAllowed.some(
(aValidCombination) => _.differenceWith(aValidCombination, attributes, _.isEqual).length === 0
);
}
public isAttributesValid4ServiceCodeArrayOperator(
serviceCode: string,
attributes: Array<CodeValuePair>
): boolean {
const serviceCodeCamelCase = _.camelCase(serviceCode);
const combinationsAllowed: Array<Array<CodeValuePair>> =
Dummy.combinationsAllowed4ServiceCode[serviceCodeCamelCase];
// Ensure that serviceCode is a valid one and attributes is provided...
if (!combinationsAllowed || !attributes) {
return false;
}
// Must provides distinct codes. Otherwise, bizarre case would returns true
const distinctCodes = new Set(attributes
.map((value) => `${value.code}¶${value.value}`))
;
if(distinctCodes && distinctCodes.size !== attributes.length) {
return false;
}
return combinationsAllowed.some(
(aValidCombination) => aValidCombination.length === attributes.length &&
aValidCombination.some(
(acceptedCodeValuePair) => attributes.some(
(providedCodeValuePair) =>
providedCodeValuePair.code === acceptedCodeValuePair.code &&
providedCodeValuePair.value === acceptedCodeValuePair.value
)
)
);
}
private aMethod(): string {
return 'sdjnsjkn';
}
}
let dummy = new Dummy();
//const isAttributesValid = dummy.isAttributesValid4ServiceCodeArrayOperator;
const isAttributesValid = dummy.isAttributesValid4ServiceCodeLodash;
//
// FAILURE tests
//
// No params provided
const fail1 = isAttributesValid(undefined, undefined);
// Bad casing for serviceCode
const fail2 = isAttributesValid("snowRemoval", undefined);
// no attributes provided
const fail3 = isAttributesValid("SNOW-REMOVAL", undefined);
// Bad serviceCode
const fail6 = isAttributesValid("SLIPPERY-ROAD-SIDEWALK2", [
{ code: "problem", value: "sidewalk" },
{ code: "location", value: "hospital" },
{ code: "magnitude", value: "wholeSection"}
]);
// code malformed (problem2 instead of problem)
const fail8 = isAttributesValid("SLIPPERY-ROAD-SIDEWALK", [
{ code: "problem2", value: "sidewalk" },
{ code: "location", value: "hospital" },
{ code: "magnitude", value: "wholeSection" }
]);
// value malformed (sidewalk2 instead of sidewalk)
const fail9 = isAttributesValid("SLIPPERY-ROAD-SIDEWALK", [
{ code: "problem", value: "sidewalk2" },
{ code: "location", value: "hospital" },
{ code: "magnitude", value: "wholeSection" }
]);
const fail11 = isAttributesValid("SLIPPERY-ROAD-SIDEWALK", <any>[]);
const fail12 = isAttributesValid("", <any>[]);
const fail13 = isAttributesValid(undefined, <any>[]);
const fail14 = isAttributesValid(null, <any>[]);
const fail15 = isAttributesValid("SLIPPERY-ROAD-SIDEWALK", [
{ code: "problem", value: "sidewalk" },
{ code: "location", value: "hospital" },
]);
// Should not supply same code more than once
const fail16 = isAttributesValid("SLIPPERY-ROAD-SIDEWALK", [
{ code: "problem", value: "sidewalk" },
{ code: "problem", value: "sidewalk" },
{ code: "location", value: "hospital" },
{ code: "magnitude", value: "wholeSection" },
{ code: "problem", value: "sidewalk" },
{ code: "location", value: "school" },
{ code: "magnitude", value: "whereIndicatedOnly" }
]);
// Extra property "whatEverElse" which is not required here
const fail17 = isAttributesValid("SLIPPERY-ROAD-SIDEWALK", [
{ code: "problem", value: "sidewalk" },
{ code: "location", value: "hospital" },
<any>{ code: "magnitude", value: "wholeSection", whatEverElse: "???" }
]);
// Criss-cross attributes of SLIPPERY-ROAD-SIDEWALK
const fail18 = isAttributesValid("SNOW-REMOVAL", [
{ code: "problem", value: "sidewalk" },
{ code: "location", value: "hospital" },
{ code: "magnitude", value: "wholeSection" }
]);
// Duplicate of same code
const fail19 = isAttributesValid("SLIPPERY-ROAD-SIDEWALK", [
{ code: "problem", value: "sidewalk" },
{ code: "problem", value: "sign" }, // duplicate
{ code: "location", value: "hospital" },
{ code: "magnitude", value: "wholeSection" }
]);
//
// SUCCESS
//
const success01 = isAttributesValid("SNOW-REMOVAL", [
{ code: "problem", value: "signs" },
{ code: "signsProblem", value: "noMatchWithMobileApp" },
// extra code/value not related to SNOW-REMOVAL and expected to be ignored
{ code: "magnitude", value: "wholeSection" }
]);
const success02 = isAttributesValid("SNOW-REMOVAL", [
{ code: "problem", value: "signs" },
{ code: "signsProblem", value: "noMatchWithMobileApp" },
]);
const success03 = isAttributesValid("SLIPPERY-ROAD-SIDEWALK", [
{ code: "problem", value: "sidewalk" },
{ code: "location", value: "hospital" },
{ code: "magnitude", value: "wholeSection" }
]);
const successXX = isAttributesValid("SNOW-REMOVAL", [
{ "code": "problem", "value": "snowClearing" },
{ "code": "snowClearingProblem", "value": "roadNotCleared" },
]);
success01;
success02;
success03;
successXX;
fail19;
fail18;
fail17;
fail16;
fail15;
fail14;
fail13;
fail12;
fail11;
fail9;
fail8;
fail6;
fail3;
fail2;
fail1;