jexp 0.1.9 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +96 -74
- package/cli.d.ts +2 -0
- package/cli.js +121 -0
- package/cli.js.map +1 -0
- package/index.d.ts +4 -1
- package/index.js +18 -87
- package/index.js.map +1 -1
- package/manager/index.d.ts +2 -0
- package/manager/index.js +19 -0
- package/manager/index.js.map +1 -0
- package/manager/jexp.d.ts +9 -0
- package/manager/jexp.js +41 -0
- package/manager/jexp.js.map +1 -0
- package/manager/schema.d.ts +11 -0
- package/manager/schema.js +36 -0
- package/manager/schema.js.map +1 -0
- package/model/index.d.ts +2 -0
- package/model/index.js +19 -0
- package/model/index.js.map +1 -0
- package/model/jexp.d.ts +8 -0
- package/model/jexp.js +3 -0
- package/model/jexp.js.map +1 -0
- package/model/schema.d.ts +7 -0
- package/model/schema.js +3 -0
- package/model/schema.js.map +1 -0
- package/package.json +8 -4
package/README.md
CHANGED
|
@@ -24,8 +24,23 @@ npm install jexp -g
|
|
|
24
24
|
|
|
25
25
|
## Usage
|
|
26
26
|
|
|
27
|
+
### Commands
|
|
28
|
+
|
|
29
|
+
| Command | Description |
|
|
30
|
+
|-----------|-----------------------------------------------------|
|
|
31
|
+
| eval | returns the result of the expression on the source |
|
|
32
|
+
| validate | validate the source from a schema |
|
|
33
|
+
|
|
34
|
+
Eval:
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
jexp eval <expression> <source> [options]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Validate:
|
|
41
|
+
|
|
27
42
|
```sh
|
|
28
|
-
jexp <
|
|
43
|
+
jexp validate <schema> <source> [options]
|
|
29
44
|
```
|
|
30
45
|
|
|
31
46
|
### Expression
|
|
@@ -36,17 +51,17 @@ jexp <expression> <source> [options]
|
|
|
36
51
|
> The root of the data is accessed from **dot**
|
|
37
52
|
|
|
38
53
|
```sh
|
|
39
|
-
jexp '.' ./data/orders.json
|
|
54
|
+
jexp eval '.' ./data/orders.json
|
|
40
55
|
```
|
|
41
56
|
|
|
42
57
|
From the **dot** we write the expressions
|
|
43
58
|
|
|
44
59
|
```sh
|
|
45
|
-
jexp '.details.article' ./data/orders.json
|
|
60
|
+
jexp eval '.details.article' ./data/orders.json
|
|
46
61
|
```
|
|
47
62
|
|
|
48
63
|
```sh
|
|
49
|
-
jexp '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json
|
|
64
|
+
jexp eval '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json
|
|
50
65
|
```
|
|
51
66
|
|
|
52
67
|
### Source
|
|
@@ -54,25 +69,25 @@ jexp '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json
|
|
|
54
69
|
Get data source from json file
|
|
55
70
|
|
|
56
71
|
```sh
|
|
57
|
-
jexp '.[0].details' ./data/orders.json
|
|
72
|
+
jexp eval '.[0].details' ./data/orders.json
|
|
58
73
|
```
|
|
59
74
|
|
|
60
75
|
Get data source from yaml file
|
|
61
76
|
|
|
62
77
|
```sh
|
|
63
|
-
jexp '.min(p=> p.total)' ./data/orders.yaml
|
|
78
|
+
jexp eval '.min(p=> p.total)' ./data/orders.yaml
|
|
64
79
|
```
|
|
65
80
|
|
|
66
81
|
Get data source from json stringify
|
|
67
82
|
|
|
68
83
|
```sh
|
|
69
|
-
jexp 'concatenate(capitalize(.fruit.name)," ",.fruit.color)' '{"fruit":{"name":"apple","color":"green","price":1.20}}'
|
|
84
|
+
jexp eval 'concatenate(capitalize(.fruit.name)," ",.fruit.color)' '{"fruit":{"name":"apple","color":"green","price":1.20}}'
|
|
70
85
|
```
|
|
71
86
|
|
|
72
87
|
Get data source from pipeline command
|
|
73
88
|
|
|
74
89
|
```sh
|
|
75
|
-
curl -s https://raw.githubusercontent.com/FlavioLionelRita/jexp/main/data/orders.json | jexp '.number'
|
|
90
|
+
curl -s https://raw.githubusercontent.com/FlavioLionelRita/jexp/main/data/orders.json | jexp eval '.number'
|
|
76
91
|
```
|
|
77
92
|
|
|
78
93
|
### Options
|
|
@@ -125,7 +140,7 @@ file orders.js
|
|
|
125
140
|
**Return the entire content of the file**:
|
|
126
141
|
|
|
127
142
|
```sh
|
|
128
|
-
jexp '.' ./data/orders.json
|
|
143
|
+
jexp eval '.' ./data/orders.json
|
|
129
144
|
```
|
|
130
145
|
|
|
131
146
|
*Result*:
|
|
@@ -137,7 +152,7 @@ jexp '.' ./data/orders.json
|
|
|
137
152
|
**Returns the number property of the list**:
|
|
138
153
|
|
|
139
154
|
```sh
|
|
140
|
-
jexp '.number' ./data/orders.json
|
|
155
|
+
jexp eval '.number' ./data/orders.json
|
|
141
156
|
```
|
|
142
157
|
|
|
143
158
|
*Result*:
|
|
@@ -149,7 +164,7 @@ jexp '.number' ./data/orders.json
|
|
|
149
164
|
**Concatenates two properties and capitalizes the first one**:
|
|
150
165
|
|
|
151
166
|
```sh
|
|
152
|
-
jexp 'concatenate(capitalize(.fruit.name)," ",.fruit.color)' '{"fruit":{"name":"apple","color":"green","price":1.20}}'
|
|
167
|
+
jexp eval 'concatenate(capitalize(.fruit.name)," ",.fruit.color)' '{"fruit":{"name":"apple","color":"green","price":1.20}}'
|
|
153
168
|
```
|
|
154
169
|
|
|
155
170
|
*Result*:
|
|
@@ -161,7 +176,7 @@ jexp 'concatenate(capitalize(.fruit.name)," ",.fruit.color)' '{"fruit":{"name":"
|
|
|
161
176
|
**Returns the first element of an array from the index in yaml format**:
|
|
162
177
|
|
|
163
178
|
```sh
|
|
164
|
-
jexp '.[0]' ./data/orders.json -o yaml
|
|
179
|
+
jexp eval '.[0]' ./data/orders.json -o yaml
|
|
165
180
|
```
|
|
166
181
|
|
|
167
182
|
*Result*:
|
|
@@ -188,7 +203,7 @@ details:
|
|
|
188
203
|
**Returns the details property of the first element in beautiful format**:
|
|
189
204
|
|
|
190
205
|
```sh
|
|
191
|
-
jexp '.[0].details' ./data/orders.json -b
|
|
206
|
+
jexp eval '.[0].details' ./data/orders.json -b
|
|
192
207
|
```
|
|
193
208
|
|
|
194
209
|
*Result*:
|
|
@@ -216,7 +231,7 @@ jexp '.[0].details' ./data/orders.json -b
|
|
|
216
231
|
**Returns the details property of the first element, as the file is yaml, it returns it in yaml format**:
|
|
217
232
|
|
|
218
233
|
```sh
|
|
219
|
-
jexp '.[0].details' ./data/orders.yaml
|
|
234
|
+
jexp eval '.[0].details' ./data/orders.yaml
|
|
220
235
|
```
|
|
221
236
|
|
|
222
237
|
*Result*:
|
|
@@ -237,7 +252,7 @@ jexp '.[0].details' ./data/orders.yaml
|
|
|
237
252
|
**Returns the details property of the first element, although the file is yaml it forces the output in json format**:
|
|
238
253
|
|
|
239
254
|
```sh
|
|
240
|
-
jexp '.[0].details' ./data/orders.yaml -b -o json
|
|
255
|
+
jexp eval '.[0].details' ./data/orders.yaml -b -o json
|
|
241
256
|
```
|
|
242
257
|
|
|
243
258
|
*Result*:
|
|
@@ -265,7 +280,7 @@ jexp '.[0].details' ./data/orders.yaml -b -o json
|
|
|
265
280
|
**Returns the details property of the listing**:
|
|
266
281
|
|
|
267
282
|
```sh
|
|
268
|
-
jexp '.details' ./data/orders.json
|
|
283
|
+
jexp eval '.details' ./data/orders.json
|
|
269
284
|
```
|
|
270
285
|
|
|
271
286
|
*Result*:
|
|
@@ -277,7 +292,7 @@ jexp '.details' ./data/orders.json
|
|
|
277
292
|
**Returns the article property of the list of details of each element of the list**:
|
|
278
293
|
|
|
279
294
|
```sh
|
|
280
|
-
jexp '.details.article' ./data/orders.json
|
|
295
|
+
jexp eval '.details.article' ./data/orders.json
|
|
281
296
|
```
|
|
282
297
|
|
|
283
298
|
*Result*:
|
|
@@ -286,22 +301,22 @@ jexp '.details.article' ./data/orders.json
|
|
|
286
301
|
["Pear","Banana","White grape","Apple","Banana","Pear","Apple","Banana","Pear","White grape"]
|
|
287
302
|
```
|
|
288
303
|
|
|
289
|
-
**
|
|
304
|
+
**The order with the smallest total**:
|
|
290
305
|
|
|
291
306
|
```sh
|
|
292
|
-
jexp '.min(p=> p.total)' ./data/orders.json
|
|
307
|
+
jexp eval '.map(p=>{nro:p.number,total:p.details.sum(q=> q.unitPrice * q.qty)}).min(p=> p.total)' ./data/orders.json
|
|
293
308
|
```
|
|
294
309
|
|
|
295
310
|
*Result*:
|
|
296
311
|
|
|
297
312
|
```json
|
|
298
|
-
|
|
313
|
+
7.58
|
|
299
314
|
```
|
|
300
315
|
|
|
301
316
|
**Get the minimum of the article property from all the details**:
|
|
302
317
|
|
|
303
318
|
```sh
|
|
304
|
-
jexp '.details.min(p=> p.article )' ./data/orders.json
|
|
319
|
+
jexp eval '.details.min(p=> p.article )' ./data/orders.json
|
|
305
320
|
```
|
|
306
321
|
|
|
307
322
|
*Result*:
|
|
@@ -313,7 +328,7 @@ jexp '.details.min(p=> p.article )' ./data/orders.json
|
|
|
313
328
|
**Get the maximum "unitPrice * p.qty" from all the details**:
|
|
314
329
|
|
|
315
330
|
```sh
|
|
316
|
-
jexp '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
331
|
+
jexp eval '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
317
332
|
```
|
|
318
333
|
|
|
319
334
|
*Result*:
|
|
@@ -325,7 +340,7 @@ jexp '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
|
325
340
|
**Get the middle value "unitPrice * p.qty" from all the details**:
|
|
326
341
|
|
|
327
342
|
```sh
|
|
328
|
-
jexp 'round(.details.avg(p=> p.unitPrice * p.qty),2)' ./data/orders.json
|
|
343
|
+
jexp eval 'round(.details.avg(p=> p.unitPrice * p.qty),2)' ./data/orders.json
|
|
329
344
|
```
|
|
330
345
|
|
|
331
346
|
*Result*:
|
|
@@ -337,7 +352,7 @@ jexp 'round(.details.avg(p=> p.unitPrice * p.qty),2)' ./data/orders.json
|
|
|
337
352
|
**Gets the sum of the total property**:
|
|
338
353
|
|
|
339
354
|
```sh
|
|
340
|
-
jexp '.sum(p=> p.total )' ./data/orders.json
|
|
355
|
+
jexp eval '.sum(p=> p.total )' ./data/orders.json
|
|
341
356
|
```
|
|
342
357
|
|
|
343
358
|
*Result*:
|
|
@@ -349,7 +364,7 @@ jexp '.sum(p=> p.total )' ./data/orders.json
|
|
|
349
364
|
**Get the sum "unitPrice * p.qty" of the details of item 1 of the list**:
|
|
350
365
|
|
|
351
366
|
```sh
|
|
352
|
-
jexp '.[1].details.sum(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
367
|
+
jexp eval '.[1].details.sum(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
353
368
|
```
|
|
354
369
|
|
|
355
370
|
*Result*:
|
|
@@ -361,7 +376,7 @@ jexp '.[1].details.sum(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
|
361
376
|
**Get the number of details where "unitPrice * p.qty " is less than 3**:
|
|
362
377
|
|
|
363
378
|
```sh
|
|
364
|
-
jexp '.details.count(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json
|
|
379
|
+
jexp eval '.details.count(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json
|
|
365
380
|
```
|
|
366
381
|
|
|
367
382
|
*Result*:
|
|
@@ -373,13 +388,19 @@ jexp '.details.count(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json
|
|
|
373
388
|
**Get the first article property of all details where "unitPrice * p.qty" is less than 3**:
|
|
374
389
|
|
|
375
390
|
```sh
|
|
376
|
-
jexp '.details.first(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
391
|
+
jexp eval '.details.first(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
377
392
|
```
|
|
378
393
|
|
|
379
394
|
*Result*:
|
|
380
395
|
|
|
381
396
|
```json
|
|
382
|
-
"
|
|
397
|
+
"Banana"
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
**Get the last article property of all details where "unitPrice * p.qty" is less than 3**:
|
|
401
|
+
|
|
402
|
+
```sh
|
|
403
|
+
jexp eval '.details.last(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
383
404
|
```
|
|
384
405
|
|
|
385
406
|
*Result*:
|
|
@@ -391,7 +412,7 @@ jexp '.details.first(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
|
391
412
|
**Get the first detail where "unitPrice * p.qty" is less than 3 in beautiful format**:
|
|
392
413
|
|
|
393
414
|
```sh
|
|
394
|
-
jexp '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json -b
|
|
415
|
+
jexp eval '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json -b
|
|
395
416
|
```
|
|
396
417
|
|
|
397
418
|
*Result*:
|
|
@@ -404,22 +425,10 @@ jexp '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json -b
|
|
|
404
425
|
}
|
|
405
426
|
```
|
|
406
427
|
|
|
407
|
-
**Get the sum "unitPrice * p.qty" of the details of item 1 of the list using pipeline**:
|
|
408
|
-
|
|
409
|
-
```sh
|
|
410
|
-
curl -s https://raw.githubusercontent.com/FlavioLionelRita/jexp/main/data/orders.json | jexp '.details.sum(p=> p.unitPrice * p.qty )'
|
|
411
|
-
```
|
|
412
|
-
|
|
413
|
-
*Result*:
|
|
414
|
-
|
|
415
|
-
```json
|
|
416
|
-
23.44
|
|
417
|
-
```
|
|
418
|
-
|
|
419
428
|
**Get the smallest article**:
|
|
420
429
|
|
|
421
430
|
```sh
|
|
422
|
-
jexp '.details.min(p=> p.article )' ./data/orders.json
|
|
431
|
+
jexp eval '.details.min(p=> p.article )' ./data/orders.json
|
|
423
432
|
```
|
|
424
433
|
|
|
425
434
|
*Result*:
|
|
@@ -431,7 +440,7 @@ jexp '.details.min(p=> p.article )' ./data/orders.json
|
|
|
431
440
|
**Get the total of all the details**:
|
|
432
441
|
|
|
433
442
|
```sh
|
|
434
|
-
jexp '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
443
|
+
jexp eval '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
435
444
|
```
|
|
436
445
|
|
|
437
446
|
*Result*:
|
|
@@ -443,7 +452,7 @@ jexp '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
|
443
452
|
**Average value of the price of the items purchased in the order 20003**:
|
|
444
453
|
|
|
445
454
|
```sh
|
|
446
|
-
jexp 'round(.filter(p=> p.number == "20003").details.avg(p=> p.unitPrice),2)' ./data/orders.json
|
|
455
|
+
jexp eval 'round(.filter(p=> p.number == "20003").details.avg(p=> p.unitPrice),2)' ./data/orders.json
|
|
447
456
|
```
|
|
448
457
|
|
|
449
458
|
*Result*:
|
|
@@ -455,7 +464,7 @@ jexp 'round(.filter(p=> p.number == "20003").details.avg(p=> p.unitPrice),2)' ./
|
|
|
455
464
|
**Get the total of the details of order 1**:
|
|
456
465
|
|
|
457
466
|
```sh
|
|
458
|
-
jexp '.[1].details.sum(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
467
|
+
jexp eval '.[1].details.sum(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
459
468
|
```
|
|
460
469
|
|
|
461
470
|
*Result*:
|
|
@@ -467,7 +476,7 @@ jexp '.[1].details.sum(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
|
467
476
|
**Gets the number of details where the subtotal is less than 3**:
|
|
468
477
|
|
|
469
478
|
```sh
|
|
470
|
-
jexp '.details.count(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json
|
|
479
|
+
jexp eval '.details.count(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json
|
|
471
480
|
```
|
|
472
481
|
|
|
473
482
|
*Result*:
|
|
@@ -479,7 +488,7 @@ jexp '.details.count(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json
|
|
|
479
488
|
**Get the article of the first detail where the subtotal is less than 3**:
|
|
480
489
|
|
|
481
490
|
```sh
|
|
482
|
-
jexp '.details.first(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
491
|
+
jexp eval '.details.first(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
483
492
|
```
|
|
484
493
|
|
|
485
494
|
*Result*:
|
|
@@ -491,7 +500,7 @@ jexp '.details.first(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
|
491
500
|
**Get the article of the last detail where the subtotal is less than 3**:
|
|
492
501
|
|
|
493
502
|
```sh
|
|
494
|
-
jexp '.details.last(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
503
|
+
jexp eval '.details.last(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
495
504
|
```
|
|
496
505
|
|
|
497
506
|
*Result*:
|
|
@@ -503,7 +512,7 @@ jexp '.details.last(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
|
503
512
|
**Get the first detail where the subtotal is less than 3**:
|
|
504
513
|
|
|
505
514
|
```sh
|
|
506
|
-
jexp '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json
|
|
515
|
+
jexp eval '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json
|
|
507
516
|
```
|
|
508
517
|
|
|
509
518
|
*Result*:
|
|
@@ -515,7 +524,7 @@ jexp '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json
|
|
|
515
524
|
**Calculate the total for each order**:
|
|
516
525
|
|
|
517
526
|
```sh
|
|
518
|
-
jexp '.each(p=>p.total=p.details.sum(q=>q.qty*q.unitPrice)).map(p=>{nro:p.number,total:p.total})' ./data/orders.json
|
|
527
|
+
jexp eval '.each(p=>p.total=p.details.sum(q=>q.qty*q.unitPrice)).map(p=>{nro:p.number,total:p.total})' ./data/orders.json
|
|
519
528
|
```
|
|
520
529
|
|
|
521
530
|
*Result*:
|
|
@@ -527,7 +536,7 @@ jexp '.each(p=>p.total=p.details.sum(q=>q.qty*q.unitPrice)).map(p=>{nro:p.number
|
|
|
527
536
|
**Calculate the subtotal for each order**:
|
|
528
537
|
|
|
529
538
|
```sh
|
|
530
|
-
jexp '.details.foreach(p=>p.subtotal=p.qty*p.unitPrice).subtotal' ./data/orders.json
|
|
539
|
+
jexp eval '.details.foreach(p=>p.subtotal=p.qty*p.unitPrice).subtotal' ./data/orders.json
|
|
531
540
|
```
|
|
532
541
|
|
|
533
542
|
*Result*:
|
|
@@ -539,7 +548,7 @@ jexp '.details.foreach(p=>p.subtotal=p.qty*p.unitPrice).subtotal' ./data/orders.
|
|
|
539
548
|
**calculates the total of all the details**:
|
|
540
549
|
|
|
541
550
|
```sh
|
|
542
|
-
jexp '.details.foreach(p=>total=nvl(total,0)+p.qty*p.unitPrice);total' ./data/orders.json
|
|
551
|
+
jexp eval '.details.foreach(p=>total=nvl(total,0)+p.qty*p.unitPrice);total' ./data/orders.json
|
|
543
552
|
```
|
|
544
553
|
|
|
545
554
|
*Result*:
|
|
@@ -551,7 +560,7 @@ jexp '.details.foreach(p=>total=nvl(total,0)+p.qty*p.unitPrice);total' ./data/or
|
|
|
551
560
|
**Get the list of items without repeating**:
|
|
552
561
|
|
|
553
562
|
```sh
|
|
554
|
-
jexp '.details.distinct(p=>p.article)' ./data/orders.json -b
|
|
563
|
+
jexp eval '.details.distinct(p=>p.article)' ./data/orders.json -b
|
|
555
564
|
```
|
|
556
565
|
|
|
557
566
|
*Result*:
|
|
@@ -565,22 +574,10 @@ jexp '.details.distinct(p=>p.article)' ./data/orders.json -b
|
|
|
565
574
|
]
|
|
566
575
|
```
|
|
567
576
|
|
|
568
|
-
**Get the list of items and quantity without repeating**:
|
|
569
|
-
|
|
570
|
-
```sh
|
|
571
|
-
jexp '.details.distinct(p=>{article:p.article,qty:p.qty}) -b' ./data/orders.json
|
|
572
|
-
```
|
|
573
|
-
|
|
574
|
-
*Result*:
|
|
575
|
-
|
|
576
|
-
```json
|
|
577
|
-
null
|
|
578
|
-
```
|
|
579
|
-
|
|
580
577
|
**Get the total and amount of each item**:
|
|
581
578
|
|
|
582
579
|
```sh
|
|
583
|
-
jexp '.details.map(p=>{article:p.article,count:count(1),total:sum(p.qty * p.unitPrice)})' ./data/orders.json -b
|
|
580
|
+
jexp eval '.details.map(p=>{article:p.article,count:count(1),total:sum(p.qty * p.unitPrice)})' ./data/orders.json -b
|
|
584
581
|
```
|
|
585
582
|
|
|
586
583
|
*Result*:
|
|
@@ -613,7 +610,7 @@ jexp '.details.map(p=>{article:p.article,count:count(1),total:sum(p.qty * p.unit
|
|
|
613
610
|
**Get the total of the first order**:
|
|
614
611
|
|
|
615
612
|
```sh
|
|
616
|
-
jexp '{total:.[0].details.sum(p=>p.qty * p.unitPrice)}' ./data/orders.json
|
|
613
|
+
jexp eval '{total:.[0].details.sum(p=>p.qty * p.unitPrice)}' ./data/orders.json
|
|
617
614
|
```
|
|
618
615
|
|
|
619
616
|
*Result*:
|
|
@@ -625,7 +622,7 @@ jexp '{total:.[0].details.sum(p=>p.qty * p.unitPrice)}' ./data/orders.json
|
|
|
625
622
|
**Get the total of the last order**:
|
|
626
623
|
|
|
627
624
|
```sh
|
|
628
|
-
jexp '{total:round(.[.length()-1].details.sum(p=>p.qty * p.unitPrice),2)}' ./data/orders.json -b
|
|
625
|
+
jexp eval '{total:round(.[.length()-1].details.sum(p=>p.qty * p.unitPrice),2)}' ./data/orders.json -b
|
|
629
626
|
```
|
|
630
627
|
|
|
631
628
|
*Result*:
|
|
@@ -639,19 +636,32 @@ jexp '{total:round(.[.length()-1].details.sum(p=>p.qty * p.unitPrice),2)}' ./dat
|
|
|
639
636
|
**List the orders with their totals**:
|
|
640
637
|
|
|
641
638
|
```sh
|
|
642
|
-
jexp '.map(p=>{nro:p.number,total:p.details.sum(q=>q.qty * q.unitPrice)})' ./data/orders.json
|
|
639
|
+
jexp eval '.map(p=>{nro:p.number,total:round(p.details.sum(q=>q.qty * q.unitPrice),2)})' ./data/orders.json -b
|
|
643
640
|
```
|
|
644
641
|
|
|
645
642
|
*Result*:
|
|
646
643
|
|
|
647
644
|
```json
|
|
648
|
-
[
|
|
645
|
+
[
|
|
646
|
+
{
|
|
647
|
+
"nro": "20001",
|
|
648
|
+
"total": 7.6
|
|
649
|
+
},
|
|
650
|
+
{
|
|
651
|
+
"nro": "20002",
|
|
652
|
+
"total": 7.9
|
|
653
|
+
},
|
|
654
|
+
{
|
|
655
|
+
"nro": "20003",
|
|
656
|
+
"total": 7.95
|
|
657
|
+
}
|
|
658
|
+
]
|
|
649
659
|
```
|
|
650
660
|
|
|
651
661
|
**All articles that are in orders 20001 and 20003**:
|
|
652
662
|
|
|
653
663
|
```sh
|
|
654
|
-
jexp '.[0].details.article.union(.[1].details.article)' ./data/orders.json
|
|
664
|
+
jexp eval '.[0].details.article.union(.[1].details.article)' ./data/orders.json
|
|
655
665
|
```
|
|
656
666
|
|
|
657
667
|
*Result*:
|
|
@@ -663,7 +673,7 @@ jexp '.[0].details.article.union(.[1].details.article)' ./data/orders.json
|
|
|
663
673
|
**The articles in common between order 20001 and 20002**:
|
|
664
674
|
|
|
665
675
|
```sh
|
|
666
|
-
jexp '.[0].details.article.intersection(.[1].details.article)' ./data/orders.json
|
|
676
|
+
jexp eval '.[0].details.article.intersection(.[1].details.article)' ./data/orders.json
|
|
667
677
|
```
|
|
668
678
|
|
|
669
679
|
*Result*:
|
|
@@ -675,7 +685,7 @@ jexp '.[0].details.article.intersection(.[1].details.article)' ./data/orders.jso
|
|
|
675
685
|
**Articles that are in order 20001 and are not in order 20002**:
|
|
676
686
|
|
|
677
687
|
```sh
|
|
678
|
-
jexp '.[0].details.article.difference(.[1].details.article)' ./data/orders.json
|
|
688
|
+
jexp eval '.[0].details.article.difference(.[1].details.article)' ./data/orders.json
|
|
679
689
|
```
|
|
680
690
|
|
|
681
691
|
*Result*:
|
|
@@ -687,7 +697,7 @@ jexp '.[0].details.article.difference(.[1].details.article)' ./data/orders.json
|
|
|
687
697
|
**Articles of orders 20001 and 20003 that are not shared**:
|
|
688
698
|
|
|
689
699
|
```sh
|
|
690
|
-
jexp '.[0].details.article.symmetricDifference(.[1].details.article)' ./data/orders.json
|
|
700
|
+
jexp eval '.[0].details.article.symmetricDifference(.[1].details.article)' ./data/orders.json
|
|
691
701
|
```
|
|
692
702
|
|
|
693
703
|
*Result*:
|
|
@@ -696,6 +706,18 @@ jexp '.[0].details.article.symmetricDifference(.[1].details.article)' ./data/ord
|
|
|
696
706
|
["White grape","Apple"]
|
|
697
707
|
```
|
|
698
708
|
|
|
709
|
+
**Get the sum "unitPrice * p.qty" of the details of item 1 of the list using pipeline**:
|
|
710
|
+
|
|
711
|
+
```sh
|
|
712
|
+
curl -s https://raw.githubusercontent.com/FlavioLionelRita/jexp/main/data/orders.json | jexp eval '.details.sum(p=> p.unitPrice * p.qty )'
|
|
713
|
+
```
|
|
714
|
+
|
|
715
|
+
*Result*:
|
|
716
|
+
|
|
717
|
+
```json
|
|
718
|
+
23.44
|
|
719
|
+
```
|
|
720
|
+
|
|
699
721
|
## js-expression
|
|
700
722
|
|
|
701
723
|
> [Js-expression](https://www.npmjs.com/package/js-expressions) is an extensible expression evaluator and parser.
|
package/cli.d.ts
ADDED
package/cli.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
const _1 = require("./");
|
|
17
|
+
const js_expressions_1 = require("js-expressions");
|
|
18
|
+
const path_1 = __importDefault(require("path"));
|
|
19
|
+
const { program, Option } = require('commander');
|
|
20
|
+
const yaml = require('js-yaml');
|
|
21
|
+
const to = (result, options, extension) => {
|
|
22
|
+
const forceJson = options ? options.output === 'json' : undefined;
|
|
23
|
+
const forceYaml = options ? options.output === 'yaml' : undefined;
|
|
24
|
+
const beautiful = options && options.beautiful !== undefined ? options.beautiful : false;
|
|
25
|
+
if (forceYaml || (!forceJson && extension && ['.yaml', 'yml'].includes(extension))) {
|
|
26
|
+
return yaml.dump(result);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
const formatted = beautiful ? JSON.stringify(result, null, 2) : JSON.stringify(result);
|
|
30
|
+
return formatted;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const getSource = (source) => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
if (typeof source !== 'string') {
|
|
35
|
+
if (typeof source === 'object') {
|
|
36
|
+
return { data: source };
|
|
37
|
+
}
|
|
38
|
+
throw new Error('invalid source');
|
|
39
|
+
}
|
|
40
|
+
const info = { data: {} };
|
|
41
|
+
if ((source.startsWith('[') || source.startsWith('{'))) {
|
|
42
|
+
info.data = js_expressions_1.Helper.tryParse(source);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
info.extension = path_1.default.extname(source);
|
|
46
|
+
if (!['.json', '.yaml', 'yml'].includes(info.extension)) {
|
|
47
|
+
throw Error(`extension ${info.extension} not supported `);
|
|
48
|
+
}
|
|
49
|
+
const content = yield js_expressions_1.Helper.readFile(source);
|
|
50
|
+
if (!content) {
|
|
51
|
+
throw Error(`can not read file ${source}`);
|
|
52
|
+
}
|
|
53
|
+
if (info.extension === 'json') {
|
|
54
|
+
info.data = js_expressions_1.Helper.tryParse(content);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
info.data = yaml.load(content);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (info.data === null || info.data === undefined) {
|
|
61
|
+
throw Error(`can not parse content of ${source}`);
|
|
62
|
+
}
|
|
63
|
+
return info;
|
|
64
|
+
});
|
|
65
|
+
const getInput = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
66
|
+
// https://wellingguzman.com/notes/node-pipe-input
|
|
67
|
+
return new Promise(function (resolve, reject) {
|
|
68
|
+
const stdin = process.stdin;
|
|
69
|
+
let data = '';
|
|
70
|
+
stdin.setEncoding('utf8');
|
|
71
|
+
stdin.on('data', function (chunk) {
|
|
72
|
+
data += chunk;
|
|
73
|
+
});
|
|
74
|
+
stdin.on('end', function () {
|
|
75
|
+
resolve(data);
|
|
76
|
+
});
|
|
77
|
+
stdin.on('error', reject);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
function main() {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
program
|
|
83
|
+
.name('eval')
|
|
84
|
+
.command('eval <expression> [source]')
|
|
85
|
+
.addOption(new Option('-o, --output <format>', 'Force output').choices(['json', 'yaml']))
|
|
86
|
+
.option('-b, --beautiful', 'Beautiful output', false)
|
|
87
|
+
.option('-q, --query-file <path>', 'query file')
|
|
88
|
+
.action((expression, source, options) => __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
try {
|
|
90
|
+
const _source = (source !== undefined) ? source : yield getInput();
|
|
91
|
+
const sourceInfo = yield getSource(_source);
|
|
92
|
+
const result = yield _1.jexp.eval(expression, sourceInfo.data);
|
|
93
|
+
const output = to(result, options, sourceInfo.extension);
|
|
94
|
+
console.log(output);
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
console.error(error.message);
|
|
98
|
+
}
|
|
99
|
+
}));
|
|
100
|
+
program
|
|
101
|
+
.name('validate')
|
|
102
|
+
.command('validate <schema> [source]')
|
|
103
|
+
.addOption(new Option('-o, --output <format>', 'Force output').choices(['json', 'yaml']))
|
|
104
|
+
.option('-b, --beautiful', 'Beautiful output', false)
|
|
105
|
+
.action((schema, source, options) => __awaiter(this, void 0, void 0, function* () {
|
|
106
|
+
try {
|
|
107
|
+
const _source = (source !== undefined) ? source : yield getInput();
|
|
108
|
+
const sourceInfo = yield getSource(_source);
|
|
109
|
+
const result = yield _1.jexp.validate(schema, sourceInfo.data);
|
|
110
|
+
const output = to(result, options, sourceInfo.extension);
|
|
111
|
+
console.log(output);
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
console.error(error.message);
|
|
115
|
+
}
|
|
116
|
+
}));
|
|
117
|
+
yield program.parseAsync(process.argv);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
main();
|
|
121
|
+
//# sourceMappingURL=cli.js.map
|
package/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../src/lib/cli.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,yBAAqC;AACrC,mDAAuC;AACvC,gDAAuB;AACvB,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,CAAA;AAChD,MAAM,IAAI,GAAG,OAAO,CAAC,SAAS,CAAC,CAAA;AAE/B,MAAM,EAAE,GAAG,CAAC,MAAU,EAAE,OAAY,EAAE,SAAiB,EAAQ,EAAE;IAChE,MAAM,SAAS,GAAG,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,SAAS,CAAA;IACjE,MAAM,SAAS,GAAG,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,SAAS,CAAA;IACjE,MAAM,SAAS,GAAG,OAAO,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAA;IACxF,IAAI,SAAS,IAAI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UAAU,GAAG,MAAM,SAAS,CAAC,OAAO,CAAC,CAAA;gBAC3C,MAAM,MAAM,GAAG,MAAM,OAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC,IAAI,CAAC,CAAA;gBAC3D,MAAM,MAAM,GAAG,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,UAAU,CAAC,SAAS,CAAC,CAAA;gBACxD,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;aACnB;YAAC,OAAO,KAAS,EAAE;gBACnB,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;aAC5B;QACF,CAAC,CAAA,CAAC,CAAA;QACH,MAAM,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IACvC,CAAC;CAAA;AACD,IAAI,EAAE,CAAA"}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,92 +1,23 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
14
16
|
};
|
|
15
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
let data = {};
|
|
22
|
-
let extension = '';
|
|
23
|
-
if ((source.startsWith('[') || source.startsWith('{'))) {
|
|
24
|
-
data = js_expressions_1.Helper.tryParse(source);
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
extension = path_1.default.extname(source);
|
|
28
|
-
if (!['.json', '.yaml', 'yml'].includes(extension)) {
|
|
29
|
-
throw Error(`extension ${extension} not supported `);
|
|
30
|
-
}
|
|
31
|
-
const content = yield js_expressions_1.Helper.readFile(source);
|
|
32
|
-
if (!content) {
|
|
33
|
-
throw Error(`can not read file ${source}`);
|
|
34
|
-
}
|
|
35
|
-
if (extension === 'json') {
|
|
36
|
-
data = js_expressions_1.Helper.tryParse(content);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
data = yaml.load(content);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (data === null || data === undefined) {
|
|
43
|
-
throw Error(`can not parse content of ${source}`);
|
|
44
|
-
}
|
|
45
|
-
const result = js_expressions_1.expressions.eval(expression, { '.': data });
|
|
46
|
-
const forceJson = options.output === 'json';
|
|
47
|
-
const forceYaml = options.output === 'yaml';
|
|
48
|
-
if (forceYaml || (!forceJson && extension && ['.yaml', 'yml'].includes(extension))) {
|
|
49
|
-
console.log(yaml.dump(result));
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
const formatted = options.beautiful ? JSON.stringify(result, null, 2) : JSON.stringify(result);
|
|
53
|
-
console.log(formatted);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
const getInput = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
57
|
-
// https://wellingguzman.com/notes/node-pipe-input
|
|
58
|
-
return new Promise(function (resolve, reject) {
|
|
59
|
-
const stdin = process.stdin;
|
|
60
|
-
let data = '';
|
|
61
|
-
stdin.setEncoding('utf8');
|
|
62
|
-
stdin.on('data', function (chunk) {
|
|
63
|
-
data += chunk;
|
|
64
|
-
});
|
|
65
|
-
stdin.on('end', function () {
|
|
66
|
-
resolve(data);
|
|
67
|
-
});
|
|
68
|
-
stdin.on('error', reject);
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
function main() {
|
|
72
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
-
program
|
|
74
|
-
.argument('<expression>')
|
|
75
|
-
.argument('[source]')
|
|
76
|
-
.addOption(new Option('-o, --output <format>', 'Force output').choices(['json', 'yaml']))
|
|
77
|
-
.option('-b, --beautiful', 'Beautiful output', false)
|
|
78
|
-
.option('-q, --query-file <path>', 'query file')
|
|
79
|
-
.action((expression, source, options) => __awaiter(this, void 0, void 0, function* () {
|
|
80
|
-
try {
|
|
81
|
-
const data = (source !== undefined) ? source : yield getInput();
|
|
82
|
-
yield run(expression, data, options);
|
|
83
|
-
}
|
|
84
|
-
catch (error) {
|
|
85
|
-
console.error(error.message);
|
|
86
|
-
}
|
|
87
|
-
}));
|
|
88
|
-
yield program.parseAsync(process.argv);
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
main();
|
|
18
|
+
exports.jexp = void 0;
|
|
19
|
+
const jexp_1 = require("./manager/jexp");
|
|
20
|
+
exports.jexp = jexp_1.Jexp.instance;
|
|
21
|
+
__exportStar(require("./model"), exports);
|
|
22
|
+
__exportStar(require("./manager"), exports);
|
|
92
23
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AACA,yCAAqC;AACxB,QAAA,IAAI,GAAG,WAAI,CAAC,QAAQ,CAAA;AACjC,0CAAuB;AACvB,4CAAyB"}
|
package/manager/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./schema"), exports);
|
|
18
|
+
__exportStar(require("./jexp"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/manager/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB;AACxB,yCAAsB"}
|
package/manager/jexp.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Jexp = void 0;
|
|
13
|
+
const js_expressions_1 = require("js-expressions");
|
|
14
|
+
const jemv_1 = require("jemv");
|
|
15
|
+
const schema_1 = require("./schema");
|
|
16
|
+
class Jexp {
|
|
17
|
+
constructor() {
|
|
18
|
+
this.exp = js_expressions_1.expressions;
|
|
19
|
+
this.jemv = jemv_1.jemv;
|
|
20
|
+
this.jemv.addConstraintBuilder(new schema_1.ExpressionConstraintBuilder());
|
|
21
|
+
this.jemv.addConstraintValidator(new schema_1.ExpressionConstraintValidator(this.exp));
|
|
22
|
+
}
|
|
23
|
+
static get instance() {
|
|
24
|
+
if (!this._instance) {
|
|
25
|
+
this._instance = new Jexp();
|
|
26
|
+
}
|
|
27
|
+
return this._instance;
|
|
28
|
+
}
|
|
29
|
+
eval(expression, data) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
return this.exp.eval(expression, { '.': data });
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
validate(schema, data) {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
return this.jemv.validate(schema, data);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.Jexp = Jexp;
|
|
41
|
+
//# sourceMappingURL=jexp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jexp.js","sourceRoot":"","sources":["../../../src/lib/manager/jexp.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mDAAgE;AAChE,+BAAiC;AACjC,qCAAqF;AAErF,MAAa,IAAI;IAGhB;QACC,IAAI,CAAC,GAAG,GAAG,4BAAG,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,WAAI,CAAA;QAChB,IAAI,CAAC,IAAI,CAAC,oBAAoB,CAAC,IAAI,oCAA2B,EAAE,CAAC,CAAA;QACjE,IAAI,CAAC,IAAI,CAAC,sBAAsB,CAAC,IAAI,sCAA6B,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAA;IAC9E,CAAC;IAGM,MAAM,KAAK,QAAQ;QACzB,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACpB,IAAI,CAAC,SAAS,GAAG,IAAI,IAAI,EAAE,CAAA;SAC3B;QACD,OAAO,IAAI,CAAC,SAAS,CAAA;IACtB,CAAC;IAEY,IAAI,CAAE,UAAiB,EAAE,IAAQ;;YAC7C,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,CAAA;QAChD,CAAC;KAAA;IAEY,QAAQ,CAAE,MAAa,EAAE,IAAQ;;YAC7C,OAAO,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;QACxC,CAAC;KAAA;CACD;AAzBD,oBAyBC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ConstraintValidator, Constraint, Schema, ConstraintBuilder } from 'jemv';
|
|
2
|
+
import { Expressions } from 'js-expressions';
|
|
3
|
+
export declare class ExpressionConstraintBuilder implements ConstraintBuilder {
|
|
4
|
+
build(property: Schema): Constraint[];
|
|
5
|
+
}
|
|
6
|
+
export declare class ExpressionConstraintValidator implements ConstraintValidator {
|
|
7
|
+
private expressions;
|
|
8
|
+
constructor(expressions: Expressions);
|
|
9
|
+
apply(constraint: Constraint): boolean;
|
|
10
|
+
validate(constraint: Constraint, data: any): boolean;
|
|
11
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExpressionConstraintValidator = exports.ExpressionConstraintBuilder = void 0;
|
|
4
|
+
class ExpressionConstraintBuilder {
|
|
5
|
+
build(property) {
|
|
6
|
+
const constraints = [];
|
|
7
|
+
const _property = property;
|
|
8
|
+
if (_property && _property.expression) {
|
|
9
|
+
constraints.push({
|
|
10
|
+
message: `Does not meet the expression ${_property.expression}`,
|
|
11
|
+
expression: _property.expression
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
return constraints;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.ExpressionConstraintBuilder = ExpressionConstraintBuilder;
|
|
18
|
+
class ExpressionConstraintValidator {
|
|
19
|
+
constructor(expressions) {
|
|
20
|
+
this.expressions = expressions;
|
|
21
|
+
}
|
|
22
|
+
apply(constraint) {
|
|
23
|
+
return 'expression' in constraint;
|
|
24
|
+
}
|
|
25
|
+
validate(constraint, data) {
|
|
26
|
+
const expressionConstraint = constraint;
|
|
27
|
+
if (expressionConstraint !== undefined) {
|
|
28
|
+
return this.expressions.eval(expressionConstraint.expression, data);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
throw new Error('Undefined ExpressionConstraint');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.ExpressionConstraintValidator = ExpressionConstraintValidator;
|
|
36
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/lib/manager/schema.ts"],"names":[],"mappings":";;;AAIA,MAAa,2BAA2B;IAChC,KAAK,CAAE,QAAgB;QAC7B,MAAM,WAAW,GAA2B,EAAE,CAAA;QAC9C,MAAM,SAAS,GAAG,QAA4B,CAAA;QAC9C,IAAI,SAAS,IAAI,SAAS,CAAC,UAAU,EAAE;YACtC,WAAW,CAAC,IAAI,CAAC;gBAChB,OAAO,EAAE,gCAAgC,SAAS,CAAC,UAAU,EAAE;gBAC/D,UAAU,EAAE,SAAS,CAAC,UAAU;aAChC,CAAC,CAAA;SACF;QACD,OAAO,WAAW,CAAA;IACnB,CAAC;CACD;AAZD,kEAYC;AAED,MAAa,6BAA6B;IAEzC,YAAa,WAAwB;QACpC,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;IAC/B,CAAC;IAEM,KAAK,CAAE,UAAsB;QACnC,OAAO,YAAY,IAAK,UAAmC,CAAA;IAC5D,CAAC;IAEM,QAAQ,CAAE,UAAsB,EAAE,IAAS;QACjD,MAAM,oBAAoB,GAAG,UAAkC,CAAA;QAC/D,IAAI,oBAAoB,KAAK,SAAS,EAAE;YACvC,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,oBAAoB,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;SACnE;aAAM;YACN,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAA;SACjD;IACF,CAAC;CACD;AAlBD,sEAkBC"}
|
package/model/index.d.ts
ADDED
package/model/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./schema"), exports);
|
|
18
|
+
__exportStar(require("./jexp"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/model/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB;AACxB,yCAAsB"}
|
package/model/jexp.d.ts
ADDED
package/model/jexp.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jexp.js","sourceRoot":"","sources":["../../../src/lib/model/jexp.ts"],"names":[],"mappings":""}
|
package/model/schema.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/lib/model/schema.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jexp",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"description": "Parser and evaluator of expressions by line of commands. The expressions correspond to the package js-expressions",
|
|
5
5
|
"author": "Flavio Lionel Rita <flaviolrita@hotmail.com>",
|
|
6
6
|
"license": "Apache-2.0 License",
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"commander": "^9.4.0",
|
|
21
|
-
"
|
|
21
|
+
"jemv": "0.0.14",
|
|
22
|
+
"js-expressions": "1.1.9",
|
|
22
23
|
"js-yaml": "^4.1.0"
|
|
23
24
|
},
|
|
24
25
|
"keywords": [
|
|
@@ -28,7 +29,10 @@
|
|
|
28
29
|
"exp",
|
|
29
30
|
"parser",
|
|
30
31
|
"eval",
|
|
31
|
-
"evaluator"
|
|
32
|
+
"evaluator",
|
|
33
|
+
"json",
|
|
34
|
+
"schema",
|
|
35
|
+
"json-schema"
|
|
32
36
|
],
|
|
33
37
|
"tags": [
|
|
34
38
|
"cli",
|
|
@@ -40,7 +44,7 @@
|
|
|
40
44
|
"evaluator"
|
|
41
45
|
],
|
|
42
46
|
"bin": {
|
|
43
|
-
"jexp": "
|
|
47
|
+
"jexp": "cli.js"
|
|
44
48
|
},
|
|
45
49
|
"types": "index.d.ts"
|
|
46
50
|
}
|