-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathepvCommonOperators.js
More file actions
519 lines (506 loc) · 17.9 KB
/
Copy pathepvCommonOperators.js
File metadata and controls
519 lines (506 loc) · 17.9 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
const env = process.env.NODE_ENV || "dev"
const log = require("./log")
const get = require("lodash/get")
const merge = require("lodash/merge")
const set = require("lodash/set")
const sortBy = require("lodash/sortBy")
const orderBy = require("lodash/orderBy")
const some = require("lodash/some")
const every = require("lodash/every")
const uniq = require("lodash/uniq")
const uniqBy = require("lodash/uniqBy")
const includes = require("lodash/includes")
const filter = require("lodash/filter")
const compact = require("lodash/compact")
const reduce = require("lodash/reduce")
const groupBy = require("lodash/groupBy")
const pickBy = require("lodash/pickBy")
const find = require("lodash/find")
const map = require("lodash/map")
const concat = require("lodash/concat")
const split = require("lodash/split")
const join = require("lodash/join")
const values = require("lodash/values")
const mapValues = require("lodash/mapValues")
const castArray = require("lodash/castArray")
const mapKeys = require("lodash/mapKeys")
const reverse = require("lodash/reverse")
const difference = require("lodash/difference")
const intersection = require("lodash/intersection")
const last = require("lodash/last")
const first = (arr) => (arr ? arr[0] : null)
const startsWith = require("lodash/startsWith")
const flatten = require("lodash/flatten")
const flattenDeep = require("lodash/flattenDeep")
const take = require("lodash/take")
const drop = require("lodash/drop")
const takeRight = require("lodash/takeRight")
const sum = require("lodash/sum")
const mean = require("lodash/mean")
const min = require("lodash/min")
const max = require("lodash/max")
const round = require("lodash/round")
const floor = require("lodash/floor")
const ceil = require("lodash/ceil")
const zipObject = require("lodash/zipObject")
const fromPairs = require("lodash/fromPairs")
const updateWith = require("lodash/updateWith")
const toNumber = require("lodash/toNumber")
const padStart = require("lodash/padStart")
const matchSorter = require("match-sorter").default
const localeIndexOf = require("locale-index-of")(Intl)
const formatISO = require("date-fns/formatISO")
const cartesian = require("cartesian")
const obsMemoize = require("./obsMemoize")
const formatInteger = require("./operators/formatInteger")
const formatDecimal = require("./operators/formatDecimal")
const formatCurrency = require("./operators/formatCurrency")
const formatDate = require("./operators/formatDate")
const formatDateTime = require("./operators/formatDateTime")
const isoDateTimeToPrecision = require("./operators/isoDateTimeToPrecision")
const ANY = "$any$"
const isIn = (value, arr) => {
if (value === ANY) return true
if (Array.isArray(arr)) {
return includes(arr, value)
}
if (typeof arr === "string") {
return includes(split(arr, ","), value)
}
return false
}
const logComputed =
env === "dev"
? (fn, name) => (arg1, arg2) => {
const timeName = `computing ${name}: ${arg1}, ${arg2}`
console.time(timeName)
const res = fn(arg1, arg2)
console.timeEnd(timeName)
return res
}
: (fn) => fn
const compareFr = new Intl.Collator("fr", { sensitivity: "base" }).compare
module.exports = (store) => {
const operators = {
logValues: (v, message) => {
console.log(message, v)
return v
},
getPvOf: (id) => store.getFromE_pv(id), // à usage interne/spécifique seulement
forEachTriplet: (cb) => store.forEachTriplet(cb), // à usage interne/spécifique seulement
patchToRemoveAllPropsOf: (id) =>
id
? {
[id]: store.createPatchToRemoveAllPropsOf(id),
}
: {},
entityRemovePatch: (entityId) =>
store.createPatchToRemoveAllPropsOf(entityId),
entitiesRemovePatch: (ids) =>
fromPairs(map(ids, (id) => [id, operators.entityRemovePatch(id)])),
entitiesMatching: function (arg1, arg2) {
const filter = arguments.length === 2 ? arg2 : arg1
const filterKeys = Object.keys(filter)
if (filterKeys.length === 1) {
// optimise le cas avec une seule prop par égalité
const prop = filterKeys[0]
const value = filter[prop]
if (typeof value !== "object") return store.getFromPve(prop, value)
}
return store.getEntitiesMatching(filter)
},
findEntityMatching: (filter) =>
first(operators.entitiesMatching(filter)) || null, // à remplacer par un appel à entitiesByValueOfProps
entitiesWithValue: (prop, value) => store.getFromPve(prop, value),
entityWithValue: (prop, value) => last(store.getFromPve(prop, value)),
entitiesWithProp: (value, prop) => store.getFromPve(prop, value),
entityWithProp: (value, prop) => last(store.getFromPve(prop, value)),
valueOfProp: (id, prop) => store.getFromEpv(id, prop),
entitiesByValueOf: (prop) => store.getFromP_ve(prop),
//TODO: créer un index "entitesByValueOfProps" qui permet d'indexer avec plusieurs props au lieu d'une seule comme dans "entitesByValueOf"
entitiesAndValueOfProp: (prop) => store.getFromP_ev(prop),
getFromGroupBy: (groupBy, value) => get(groupBy, value, []),
constant: function (v1, v2) {
return arguments.length === 2 ? v2 : v1
}, // dans le cas où il y a une source, constant est appelé avec 2 args mais c'est le 2ème qui compte
typeof: (v) => typeof v,
callOperator: (args, operator) => {
return operators[operator](args[0], args[1])
},
get: (v, prop) => get(v, prop),
set: (o, { path, value }) => set(o, path, value),
first,
last,
flatten,
flattenDeep,
sum: (arg) => {
return sum(compact(arg))
},
mean: (values) => mean(filter(values, (v) => v != null)),
min,
max,
compact,
plus: (v, exp) => v + operators.query(exp),
minus: (v, exp) => (exp ? v - operators.query(exp) : v[0] - v[1]),
divide: (v, exp) => (exp ? v / operators.query(exp) : v[0] / v[1]),
multiply: (v, exp) => {
if (!exp && Array.isArray(v)) {
return v.reduce((a, b) => a * b)
}
return v * operators.query(exp)
},
modulo: (v, m) => v % m,
take: (v, n) => (typeof v === "string" ? v.slice(0, n) : take(v, n)),
takeEnd: (v, n) =>
typeof v === "string" ? v.slice(n * -1) : takeRight(v, n),
drop,
count: (arr) => arr.length,
contains: (arr, exp) => includes(arr, operators.query(exp)),
containedIn: (v, arr) => includes(arr, v),
localeInsensitiveIndexOf: (string, substring) => {
if (
!string ||
!substring ||
typeof string !== "string" ||
typeof substring !== "string"
)
return -1
return localeIndexOf(string, substring, "fr", { sensitivity: "base" })
},
localeInsensitiveContains: (string, substring) =>
operators.localeInsensitiveIndexOf(string, substring) >= 0,
ObjectKeys: (o) => Object.keys(o),
ObjectValues: (o) => values(o),
reverse: (arr) => reverse(arr.slice()),
toBoolean: (v) => !!v,
not: (v) => !v,
identity: (v) => v,
some,
every,
unique: uniq,
uniqueBy: (arr, exp) =>
uniqBy(arr, (v) => operators.query(concat({ constant: v }, exp))),
round,
roundMultiple: (v, precision) => round(v / precision) * precision,
floor,
floorMultiple: (v, precision) => floor(v / precision) * precision,
roundDown: floor,
ceil,
ceilMultiple: (v, precision) => ceil(v / precision) * precision,
roundUp: ceil,
padStart: (s, arg) => {
if (typeof arg === "number") return padStart(s, arg)
return padStart(s, arg.length, arg.chars)
},
zipObject: (args) => zipObject(args[0], args[1]),
fromPairs,
concat: (v1, v2) => (Array.isArray(v1) ? v1.concat(v2) : v1 + v2),
concatExp: (v1, exp) =>
Array.isArray(v1)
? v1.concat(operators.query(exp))
: v1 + operators.query(exp),
split: (string, sep) => split(string, sep),
join: (strings, sep) => join(strings, sep),
isDefined: (v) => v != null,
isEmpty: (v) => (Array.isArray(v) ? v.length === 0 : v == null),
default: (value, defaultValue) => (value == null ? defaultValue : value),
and: (value, exp) => value && operators.query(exp),
andLazy: (value, exps) =>
every(exps, (exp) => operators.query([{ constant: value }].concat(exp))),
or: (v, exps) => {
return some(exps, (exp) => operators.query([{ constant: v }].concat(exp)))
},
defaultExp: (value, exp) => (value != null ? value : operators.query(exp)),
groupBy: (arr, exp) => {
return groupBy(arr, (v) => {
return operators.query([{ constant: v }].concat(exp))
})
},
filterBy: (arr, exp) => {
return filter(arr, (v) => {
return operators.query([{ constant: v }].concat(exp))
})
},
filterObjectBy: (obj, exp) => {
return pickBy(obj, (v) => {
return operators.query([{ constant: v }].concat(exp))
})
},
rejectBy: (arr, exp) => {
return filter(arr, (v) => {
return !operators.query([{ constant: v }].concat(exp))
})
},
findBy: (arr, exp) => {
return find(arr, (v) => {
return operators.query([{ constant: v }].concat(exp))
})
},
sortBy: (ids, exp) =>
sortBy(ids, (id) => operators.query([{ constant: id }].concat(exp))),
localeSortBy: (ids, exp) => {
if (!ids || !ids.sort) return null
const mapBy = (id) => operators.query([{ constant: id }].concat(exp))
return ids.sort((a, b) => compareFr(mapBy(a), mapBy(b)))
},
orderBy: (ids, arg) => {
const { mappers, orders } = arg
return orderBy(
ids,
mappers.map(
(exp) => (id) => operators.query([{ constant: id }].concat(exp))
),
orders
)
},
someBy: (ids, exp) =>
some(ids, (id) => operators.query([{ constant: id }].concat(exp))),
mapBy: (ids, exp) =>
map(ids, (id) => operators.query([{ constant: id }].concat(exp))),
// crée un objet avec les valeurs du array en key and value
arrayToObject: (arr) => reduce(arr, (acc, id) => set(acc, id, id), {}),
mapObjectBy: (obj, exp) =>
mapValues(obj, (id) => operators.query([{ constant: id }].concat(exp))),
mapKeysBy: (obj, exp) =>
mapKeys(obj, (id) => operators.query([{ constant: id }].concat(exp))),
// assigne sur arg1 ou sur un objet vide, le ou les objets sources (soit un objet seul ou un array de sources)
assign: (arg1, arg2) => {
const target = arg2 ? arg1 : {}
const sources = operators.query(arg2 ? arg2 : arg1)
return Object.assign.apply(null, concat(target, sources))
},
mergeExp: (o, exp) => {
const source = operators.query([{ constant: o }].concat(exp))
return merge(o, source)
},
merge: (sources) => merge({}, ...sources),
each: (v, exps) => {
let mapExp
if (!exps) {
exps = v
mapExp = (exp) => operators.query(exp)
} else {
mapExp = (exp) => operators.query([{ constant: v }].concat(exp))
}
return (Array.isArray(exps) ? map : mapValues)(exps, mapExp)
},
// retourne le premier résultat non null
// c'est comme un each mais qui retourne le premier résultat non null ...
firstDefined: (v, exps) => {
let mapExp
if (!exps) {
exps = v
mapExp = (exp) => operators.query(exp)
} else {
mapExp = (exp) => operators.query([{ constant: v }].concat(exp))
}
for (var i = 0; i < exps.length; i++) {
let res = mapExp(exps[i])
if (res != null) return res
}
},
gte: (arg1, arg2) => {
const [v1, v2] = Array.isArray(arg1) ? arg1 : [arg1, arg2]
return v1 >= v2
},
gt: (arg1, arg2) => {
const [v1, v2] = Array.isArray(arg1) ? arg1 : [arg1, arg2]
return v1 > v2
},
lte: (arg1, arg2) => {
const [v1, v2] = Array.isArray(arg1) ? arg1 : [arg1, arg2]
return v1 <= v2
},
lt: (arg1, arg2) => {
const [v1, v2] = Array.isArray(arg1) ? arg1 : [arg1, arg2]
return v1 < v2
},
equal: (arg1, arg2) => {
const [v1, v2] = Array.isArray(arg1) ? arg1 : [arg1, arg2]
return v1 === v2
},
isIn: (arg1, arg2) => {
const [v1, v2] = Array.isArray(arg1) ? arg1 : [arg1, arg2]
return isIn(v1, v2)
},
equalExp: (value, exp) => value === operators.query(exp),
isTruthy: (v) => !!v,
isFalsy: (v) => !v,
startsWith: (arg1, arg2) => {
return startsWith(arg1, arg2)
},
prepend: (v, exp) => [operators.query(exp), v],
append: (v, exp) => [v, operators.query(exp)],
union: ([s1, s2]) => s1.concat(s2),
difference: ([s1, s2]) => {
return difference(s1, s2)
},
intersection: ([s1, s2]) => intersection(s1, s2),
branch: (value, args) => {
if (value === undefined) {
value = null
}
const cond = args.cond
? operators.query([{ constant: value }].concat(args.cond))
: value
const branch = cond ? args["truthy"] : args["falsy"]
if (!branch) return value
return operators.query([{ constant: value }].concat(branch))
},
match: (value, args) => {
if (value === undefined) {
value = null
}
let branch = args[value]
if (!branch) {
branch = args["default"]
}
if (!branch) return value
return operators.query([{ constant: value }].concat(branch))
},
matchBy: (value, { cond, cases }) => {
if (value === undefined) {
value = null
}
const condValue = operators.query(concat({ constant: value }, cond))
let branch = cases[condValue]
if (!branch) {
branch = cases["default"]
}
if (!branch) return value
return operators.query([{ constant: value }].concat(branch))
},
formatInteger,
formatNumber: (n, options) =>
get(n, "toLocaleString") ? n.toLocaleString("fr", options) : "",
formatDate,
formatDecimal: (n, decimals) => formatDecimal(decimals)(n),
formatTime: (n, options) =>
n ? new Date(n).toLocaleTimeString("fr", options) : "",
formatDateTime,
formatBoolean: (n) => (n ? "OUI" : "NON"),
formatCurrency,
toNumber,
castArray,
toDateTime: ([date, time]) => {
if (!date || !date.concat || !time) return
return date.concat("T", time)
},
// on obtient un time dans la time zone locale et on perd le décalage horaire
isoDateTimeToTime: (isoDateTime) =>
isoDateTime
? formatISO(new Date(isoDateTime), { representation: "time" }).slice(
0,
8
)
: null,
isoDateTimeToDate: isoDateTimeToPrecision("date"),
isoDateTimeToWeek: isoDateTimeToPrecision("week"),
isoDateTimeToMonth: isoDateTimeToPrecision("month"),
isoDateTimeToYear: isoDateTimeToPrecision("year"),
stringify: JSON.stringify,
jsonParse: (v) => {
try {
return JSON.parse(v)
} catch (err) {
log.warn("jsonParse operator error", { err })
return null
}
},
multiGroupBy: obsMemoize(
// on inclue la définition de la source dans les arguments pour pouvoir
// memoizer le résultat à partir de la définition de source et non de sa valeur
(arg) => () => {
const values = {}
// console.time("multiGroupByData")
const data = operators.query(arg.source)
// console.timeEnd("multiGroupByData")
// console.time("multiGroupBy combinaisons")
data.forEach((item) => {
const dimValues = arg.dims.map((dimExp) =>
operators.query([{ constant: item }].concat(dimExp))
)
// toutes les combinaisons de dimensions, totaux compris
const combinations = cartesian(dimValues.map((v) => [v, ANY]))
combinations.forEach((path) => {
// ajoute l'élément dans la liste croisée
updateWith(
values,
path,
(arr) => (arr ? arr.concat(item) : [item]),
Object
)
})
})
// console.timeEnd("multiGroupBy combinaisons")
return values
/*
return {
// longueur 1200
1200: {
// lg 1200, essence idEss1
idEss1: {
// lg 1200, essence idEss1, qualité idQual1
idQual1: [item1, item2],
idQual2: [item5, item6],
// lg 1200, essence idEss1, toute qualité
$any$: [item1, item2, item5, item6],
},
idEss2: {
idQual1: [item3, item4],
$any$: [item3, item4]
},
// lg 1200, toute essence
$any$: {
// lg 1200, toute essence, qual1
idQual1: [item1, item2, item3, item4],
idQual2: [item5, item6],
// lg 1200, toute essence, toute qualité
$any$: [item1, item2, item3, item4, item5, item6]
}
},
$any$: {
...
},
}
*/
},
"multiGroupBy"
),
getGroupsFromMultiGroupBy: (data, path) => {
const groups = path && path.length > 0 ? get(data, path) : data
if (!groups) return []
return Object.keys(groups).filter((k) => k !== ANY)
},
getValuesFromMultiGroupBy: (data, path) => {
return get(data, path, [])
},
matchSorter: (source, arg) => {
// keys est facultatif
const { searchValue, mapBy, keys } = arg
// map source items
const items = map(source, (item) => ({
sourceItem: item,
sortingValue: operators.query([{ constant: item }].concat(mapBy)),
}))
// sort items, and return sorted source items back
return map(
matchSorter(items, searchValue, {
keys: keys
? map(keys, (k) => (i) => i.sortingValue[k])
: [(i) => i.sortingValue],
}),
(i) => i.sourceItem
)
},
toCsvCell: (v) => {
if (v == null) return "" //null or undefined
return JSON.stringify(v)
},
toCsvRow: (arr, options) =>
arr.map(operators.toCsvCell).join((options && options.separator) || ","),
toCsv: (arr, options) =>
arr.map((r) => operators.toCsvRow(r, options)).join("\r\n"),
}
return operators
}