jexp 0.1.8 → 0.1.11
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 +138 -74
- package/cli.d.ts +2 -0
- package/cli.js +123 -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
|
+
|
|
27
36
|
```sh
|
|
28
|
-
jexp <expression> <source> [options]
|
|
37
|
+
jexp eval <expression> <source> [options]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Validate:
|
|
41
|
+
|
|
42
|
+
```sh
|
|
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,19 +340,19 @@ 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 '.details.avg(p=> p.unitPrice * p.qty
|
|
343
|
+
jexp eval 'round(.details.avg(p=> p.unitPrice * p.qty),2)' ./data/orders.json
|
|
329
344
|
```
|
|
330
345
|
|
|
331
346
|
*Result*:
|
|
332
347
|
|
|
333
348
|
```json
|
|
334
|
-
2.
|
|
349
|
+
2.35
|
|
335
350
|
```
|
|
336
351
|
|
|
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,7 +388,7 @@ 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*:
|
|
@@ -385,7 +400,7 @@ jexp '.details.first(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
|
385
400
|
**Get the last article property of all details where "unitPrice * p.qty" is less than 3**:
|
|
386
401
|
|
|
387
402
|
```sh
|
|
388
|
-
jexp '.details.last(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
403
|
+
jexp eval '.details.last(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
389
404
|
```
|
|
390
405
|
|
|
391
406
|
*Result*:
|
|
@@ -397,7 +412,7 @@ jexp '.details.last(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
|
397
412
|
**Get the first detail where "unitPrice * p.qty" is less than 3 in beautiful format**:
|
|
398
413
|
|
|
399
414
|
```sh
|
|
400
|
-
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
|
|
401
416
|
```
|
|
402
417
|
|
|
403
418
|
*Result*:
|
|
@@ -410,22 +425,10 @@ jexp '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json -b
|
|
|
410
425
|
}
|
|
411
426
|
```
|
|
412
427
|
|
|
413
|
-
**Get the sum "unitPrice * p.qty" of the details of item 1 of the list using pipeline**:
|
|
414
|
-
|
|
415
|
-
```sh
|
|
416
|
-
curl -s https://raw.githubusercontent.com/FlavioLionelRita/jexp/main/data/orders.json | jexp '.details.sum(p=> p.unitPrice * p.qty )'
|
|
417
|
-
```
|
|
418
|
-
|
|
419
|
-
*Result*:
|
|
420
|
-
|
|
421
|
-
```json
|
|
422
|
-
15.49
|
|
423
|
-
```
|
|
424
|
-
|
|
425
428
|
**Get the smallest article**:
|
|
426
429
|
|
|
427
430
|
```sh
|
|
428
|
-
jexp '.details.min(p=> p.article )' ./data/orders.json
|
|
431
|
+
jexp eval '.details.min(p=> p.article )' ./data/orders.json
|
|
429
432
|
```
|
|
430
433
|
|
|
431
434
|
*Result*:
|
|
@@ -437,7 +440,7 @@ jexp '.details.min(p=> p.article )' ./data/orders.json
|
|
|
437
440
|
**Get the total of all the details**:
|
|
438
441
|
|
|
439
442
|
```sh
|
|
440
|
-
jexp '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
443
|
+
jexp eval '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
441
444
|
```
|
|
442
445
|
|
|
443
446
|
*Result*:
|
|
@@ -446,22 +449,22 @@ jexp '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
|
446
449
|
3.98
|
|
447
450
|
```
|
|
448
451
|
|
|
449
|
-
**
|
|
452
|
+
**Average value of the price of the items purchased in the order 20003**:
|
|
450
453
|
|
|
451
454
|
```sh
|
|
452
|
-
jexp '.details.avg(p=> p.unitPrice
|
|
455
|
+
jexp eval 'round(.filter(p=> p.number == "20003").details.avg(p=> p.unitPrice),2)' ./data/orders.json
|
|
453
456
|
```
|
|
454
457
|
|
|
455
458
|
*Result*:
|
|
456
459
|
|
|
457
460
|
```json
|
|
458
|
-
2
|
|
461
|
+
2
|
|
459
462
|
```
|
|
460
463
|
|
|
461
464
|
**Get the total of the details of order 1**:
|
|
462
465
|
|
|
463
466
|
```sh
|
|
464
|
-
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
|
|
465
468
|
```
|
|
466
469
|
|
|
467
470
|
*Result*:
|
|
@@ -473,7 +476,7 @@ jexp '.[1].details.sum(p=> p.unitPrice * p.qty )' ./data/orders.json
|
|
|
473
476
|
**Gets the number of details where the subtotal is less than 3**:
|
|
474
477
|
|
|
475
478
|
```sh
|
|
476
|
-
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
|
|
477
480
|
```
|
|
478
481
|
|
|
479
482
|
*Result*:
|
|
@@ -485,7 +488,7 @@ jexp '.details.count(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json
|
|
|
485
488
|
**Get the article of the first detail where the subtotal is less than 3**:
|
|
486
489
|
|
|
487
490
|
```sh
|
|
488
|
-
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
|
|
489
492
|
```
|
|
490
493
|
|
|
491
494
|
*Result*:
|
|
@@ -497,7 +500,7 @@ jexp '.details.first(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
|
497
500
|
**Get the article of the last detail where the subtotal is less than 3**:
|
|
498
501
|
|
|
499
502
|
```sh
|
|
500
|
-
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
|
|
501
504
|
```
|
|
502
505
|
|
|
503
506
|
*Result*:
|
|
@@ -509,7 +512,7 @@ jexp '.details.last(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json
|
|
|
509
512
|
**Get the first detail where the subtotal is less than 3**:
|
|
510
513
|
|
|
511
514
|
```sh
|
|
512
|
-
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
|
|
513
516
|
```
|
|
514
517
|
|
|
515
518
|
*Result*:
|
|
@@ -521,7 +524,7 @@ jexp '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json
|
|
|
521
524
|
**Calculate the total for each order**:
|
|
522
525
|
|
|
523
526
|
```sh
|
|
524
|
-
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
|
|
525
528
|
```
|
|
526
529
|
|
|
527
530
|
*Result*:
|
|
@@ -533,7 +536,7 @@ jexp '.each(p=>p.total=p.details.sum(q=>q.qty*q.unitPrice)).map(p=>{nro:p.number
|
|
|
533
536
|
**Calculate the subtotal for each order**:
|
|
534
537
|
|
|
535
538
|
```sh
|
|
536
|
-
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
|
|
537
540
|
```
|
|
538
541
|
|
|
539
542
|
*Result*:
|
|
@@ -545,7 +548,7 @@ jexp '.details.foreach(p=>p.subtotal=p.qty*p.unitPrice).subtotal' ./data/orders.
|
|
|
545
548
|
**calculates the total of all the details**:
|
|
546
549
|
|
|
547
550
|
```sh
|
|
548
|
-
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
|
|
549
552
|
```
|
|
550
553
|
|
|
551
554
|
*Result*:
|
|
@@ -557,7 +560,7 @@ jexp '.details.foreach(p=>total=nvl(total,0)+p.qty*p.unitPrice);total' ./data/or
|
|
|
557
560
|
**Get the list of items without repeating**:
|
|
558
561
|
|
|
559
562
|
```sh
|
|
560
|
-
jexp '.details.distinct(p=>p.article)' ./data/orders.json -b
|
|
563
|
+
jexp eval '.details.distinct(p=>p.article)' ./data/orders.json -b
|
|
561
564
|
```
|
|
562
565
|
|
|
563
566
|
*Result*:
|
|
@@ -571,22 +574,10 @@ jexp '.details.distinct(p=>p.article)' ./data/orders.json -b
|
|
|
571
574
|
]
|
|
572
575
|
```
|
|
573
576
|
|
|
574
|
-
**Get the list of items and quantity without repeating**:
|
|
575
|
-
|
|
576
|
-
```sh
|
|
577
|
-
jexp '.details.distinct(p=>{article:p.article,qty:p.qty}) -b' ./data/orders.json
|
|
578
|
-
```
|
|
579
|
-
|
|
580
|
-
*Result*:
|
|
581
|
-
|
|
582
|
-
```json
|
|
583
|
-
null
|
|
584
|
-
```
|
|
585
|
-
|
|
586
577
|
**Get the total and amount of each item**:
|
|
587
578
|
|
|
588
579
|
```sh
|
|
589
|
-
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
|
|
590
581
|
```
|
|
591
582
|
|
|
592
583
|
*Result*:
|
|
@@ -619,7 +610,7 @@ jexp '.details.map(p=>{article:p.article,count:count(1),total:sum(p.qty * p.unit
|
|
|
619
610
|
**Get the total of the first order**:
|
|
620
611
|
|
|
621
612
|
```sh
|
|
622
|
-
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
|
|
623
614
|
```
|
|
624
615
|
|
|
625
616
|
*Result*:
|
|
@@ -631,27 +622,100 @@ jexp '{total:.[0].details.sum(p=>p.qty * p.unitPrice)}' ./data/orders.json
|
|
|
631
622
|
**Get the total of the last order**:
|
|
632
623
|
|
|
633
624
|
```sh
|
|
634
|
-
jexp '{total
|
|
625
|
+
jexp eval '{total:round(.[.length()-1].details.sum(p=>p.qty * p.unitPrice),2)}' ./data/orders.json -b
|
|
635
626
|
```
|
|
636
627
|
|
|
637
628
|
*Result*:
|
|
638
629
|
|
|
639
630
|
```json
|
|
640
631
|
{
|
|
641
|
-
"total": 7.
|
|
632
|
+
"total": 7.95
|
|
642
633
|
}
|
|
643
634
|
```
|
|
644
635
|
|
|
645
636
|
**List the orders with their totals**:
|
|
646
637
|
|
|
647
638
|
```sh
|
|
648
|
-
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
|
|
649
640
|
```
|
|
650
641
|
|
|
651
642
|
*Result*:
|
|
652
643
|
|
|
653
644
|
```json
|
|
654
|
-
[
|
|
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
|
+
]
|
|
659
|
+
```
|
|
660
|
+
|
|
661
|
+
**All articles that are in orders 20001 and 20003**:
|
|
662
|
+
|
|
663
|
+
```sh
|
|
664
|
+
jexp eval '.[0].details.article.union(.[1].details.article)' ./data/orders.json
|
|
665
|
+
```
|
|
666
|
+
|
|
667
|
+
*Result*:
|
|
668
|
+
|
|
669
|
+
```json
|
|
670
|
+
["Pear","Banana","White grape","Apple"]
|
|
671
|
+
```
|
|
672
|
+
|
|
673
|
+
**The articles in common between order 20001 and 20002**:
|
|
674
|
+
|
|
675
|
+
```sh
|
|
676
|
+
jexp eval '.[0].details.article.intersection(.[1].details.article)' ./data/orders.json
|
|
677
|
+
```
|
|
678
|
+
|
|
679
|
+
*Result*:
|
|
680
|
+
|
|
681
|
+
```json
|
|
682
|
+
["Banana","Pear"]
|
|
683
|
+
```
|
|
684
|
+
|
|
685
|
+
**Articles that are in order 20001 and are not in order 20002**:
|
|
686
|
+
|
|
687
|
+
```sh
|
|
688
|
+
jexp eval '.[0].details.article.difference(.[1].details.article)' ./data/orders.json
|
|
689
|
+
```
|
|
690
|
+
|
|
691
|
+
*Result*:
|
|
692
|
+
|
|
693
|
+
```json
|
|
694
|
+
["White grape"]
|
|
695
|
+
```
|
|
696
|
+
|
|
697
|
+
**Articles of orders 20001 and 20003 that are not shared**:
|
|
698
|
+
|
|
699
|
+
```sh
|
|
700
|
+
jexp eval '.[0].details.article.symmetricDifference(.[1].details.article)' ./data/orders.json
|
|
701
|
+
```
|
|
702
|
+
|
|
703
|
+
*Result*:
|
|
704
|
+
|
|
705
|
+
```json
|
|
706
|
+
["White grape","Apple"]
|
|
707
|
+
```
|
|
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
|
|
655
719
|
```
|
|
656
720
|
|
|
657
721
|
## js-expression
|
package/cli.d.ts
ADDED
package/cli.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
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
|
+
.argument('<expression>')
|
|
85
|
+
.argument('[source]')
|
|
86
|
+
.addOption(new Option('-o, --output <format>', 'Force output').choices(['json', 'yaml']))
|
|
87
|
+
.option('-b, --beautiful', 'Beautiful output', false)
|
|
88
|
+
.option('-q, --query-file <path>', 'query file')
|
|
89
|
+
.action((expression, source, options) => __awaiter(this, void 0, void 0, function* () {
|
|
90
|
+
try {
|
|
91
|
+
const _source = (source !== undefined) ? source : yield getInput();
|
|
92
|
+
const sourceInfo = yield getSource(_source);
|
|
93
|
+
const result = yield _1.jexp.eval(expression, sourceInfo.data);
|
|
94
|
+
const output = to(result, options, sourceInfo.extension);
|
|
95
|
+
console.log(output);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
console.error(error.message);
|
|
99
|
+
}
|
|
100
|
+
}));
|
|
101
|
+
program
|
|
102
|
+
.name('validate')
|
|
103
|
+
.argument('<schema>')
|
|
104
|
+
.argument('[source]')
|
|
105
|
+
.addOption(new Option('-o, --output <format>', 'Force output').choices(['json', 'yaml']))
|
|
106
|
+
.option('-b, --beautiful', 'Beautiful output', false)
|
|
107
|
+
.action((schema, source, options) => __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
try {
|
|
109
|
+
const _source = (source !== undefined) ? source : yield getInput();
|
|
110
|
+
const sourceInfo = yield getSource(_source);
|
|
111
|
+
const result = yield _1.jexp.validate(schema, sourceInfo.data);
|
|
112
|
+
const output = to(result, options, sourceInfo.extension);
|
|
113
|
+
console.log(output);
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
console.error(error.message);
|
|
117
|
+
}
|
|
118
|
+
}));
|
|
119
|
+
yield program.parseAsync(process.argv);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
main();
|
|
123
|
+
//# 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,CAAC,CAAC,SAAS,IAAI,SAAS,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,EAAE;QACnF,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;KACxB;SAAM;QACN,MAA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AAC,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.11",
|
|
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
|
}
|