3.8.110 • Published 10 months ago

@diotoborg/sed-sunt v3.8.110

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

JEXP

Command line application that allows parser and evaluator of json/yaml applying expressions.

Features

  • json and yaml formats
  • Constants, enums, number, string, datetime, variables, objects and array
  • Arithmetic , assignment , comparison , logical and bitwise operators
  • Number , string , datetime , array and nullable functions
  • Conversion functions
  • Arrow functions
  • Group functions (distinct, first, last, min, max, sum and avg)
  • Sets functions (union, intersection, difference and symmetric difference)
  • Environment variables

Global installation

it is necessary to install the package globally to be able to access the command line applications.

npm install @diotoborg/sed-sunt -g

Usage

Commands

CommandDescription
evalreturns the result of the expression on the source
validatevalidate the source from a schema

Eval:

@diotoborg/sed-sunt eval <expression> <source> [options]

Validate:

@diotoborg/sed-sunt validate <schema> <source> [options]

Expression

The expressions correspond to the package 3xpr expression that is applied to the data source

The root of the data is accessed from dot

@diotoborg/sed-sunt eval '.' ./data/orders.json

From the dot we write the expressions

@diotoborg/sed-sunt eval '.details.article' ./data/orders.json
@diotoborg/sed-sunt eval '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json

Source

Get data source from json file

@diotoborg/sed-sunt eval '.[0].details' ./data/orders.json

Get data source from yaml file

@diotoborg/sed-sunt eval '.min(p=> p.total)' ./data/orders.yaml

Get data source from json stringify

@diotoborg/sed-sunt eval 'concatenate(capitalize(.fruit.name)," ",.fruit.color)' '{"fruit":{"name":"apple","color":"green","price":1.20}}'

Get data source from pipeline command

curl -s https://raw.githubusercontent.com/data7expressions/@diotoborg/sed-sunt/main/data/orders.json | @diotoborg/sed-sunt eval '.number'

Options

OptionAbbreviationDescriptionOptions
--output -oForce outputjson, yaml
--beautiful-bBeautiful output

Examples

file orders.js

[
  {
    "number": "20001",
    "customer": { "firstName": "John", "lastName": "Murphy" },
    "orderTime": "2022-07-30T10:15:54",
    "details": [
      { "article": "Pear", "unitPrice": 1.78, "qty": 2 },
      { "article": "Banana", "unitPrice": 1.99, "qty": 1 },
      { "article": "White grape", "unitPrice": 2.03, "qty": 1 }
    ]
  },
  {
    "number": "20002",
    "customer": { "firstName": "Paul", "lastName": "Smith"  },
    "orderTime": "2022-07-30T12:12:43",
    "details": [
      { "article": "Apple", "unitPrice": 2.15, "qty": 1 },
      { "article": "Banana", "unitPrice": 1.99, "qty": 2 },
      { "article": "Pear", "unitPrice": 1.78, "qty": 1 }
    ]
  },
  {
    "number": "20003",
    "customer": { "firstName": "George", "lastName": "Williams" },
    "orderTime": "2022-07-30T14:43:11",
    "details": [
      { "article": "Apple", "unitPrice": 2.15, "qty": 1 },
      { "article": "Banana", "unitPrice": 1.99, "qty": 1 },
      { "article": "Pear", "unitPrice": 1.78, "qty": 1 },
      { "article": "White grape", "unitPrice": 2.03, "qty": 1 }
    ]
  }
]

Return the entire content of the file:

@diotoborg/sed-sunt eval '.' ./data/orders.json

Result:

[{"number":"20001","customer":{"firstName":"John","lastName":"Murphy"},"orderTime":"2022-07-30T10:15:54","details":[{"article":"Pear","unitPrice":1.78,"qty":2},{"article":"Banana","unitPrice":1.99,"qty":1},{"article":"White grape","unitPrice":2.03,"qty":1}]},{"number":"20002","customer":{"firstName":"Paul","lastName":"Smith"},"orderTime":"2022-07-30T12:12:43","details":[{"article":"Apple","unitPrice":2.15,"qty":1},{"article":"Banana","unitPrice":1.99,"qty":2},{"article":"Pear","unitPrice":1.78,"qty":1}]},{"number":"20003","customer":{"firstName":"George","lastName":"Williams"},"orderTime":"2022-07-30T14:43:11","details":[{"article":"Apple","unitPrice":2.15,"qty":1},{"article":"Banana","unitPrice":1.99,"qty":1},{"article":"Pear","unitPrice":1.78,"qty":1},{"article":"White grape","unitPrice":2.03,"qty":1}]}]

Returns the number property of the list:

@diotoborg/sed-sunt eval '.number' ./data/orders.json

Result:

["20001","20002","20003"]

Concatenates two properties and capitalizes the first one:

@diotoborg/sed-sunt eval 'concatenate(capitalize(.fruit.name)," ",.fruit.color)' '{"fruit":{"name":"apple","color":"green","price":1.20}}'

Result:

"Apple green"

Returns the first element of an array from the index in yaml format:

@diotoborg/sed-sunt eval '.[0]' ./data/orders.json -o yaml

Result:

number: '20001'
customer:
  firstName: John
  lastName: Murphy
orderTime: '2022-07-30T10:15:54'
details:
  - article: Pear
    unitPrice: 1.78
    qty: 2
  - article: Banana
    unitPrice: 1.99
    qty: 1
  - article: White grape
    unitPrice: 2.03
    qty: 1

Returns the details property of the first element in beautiful format:

@diotoborg/sed-sunt eval '.[0].details' ./data/orders.json -b

Result:

[
  {
    "article": "Pear",
    "unitPrice": 1.78,
    "qty": 2
  },
  {
    "article": "Banana",
    "unitPrice": 1.99,
    "qty": 1
  },
  {
    "article": "White grape",
    "unitPrice": 2.03,
    "qty": 1
  }
]

Returns the details property of the first element, as the file is yaml, it returns it in yaml format:

@diotoborg/sed-sunt eval '.[0].details' ./data/orders.yaml

Result:

- article: Pear
  unitPrice: 1.78
  qty: 2
- article: Banana
  unitPrice: 1.99
  qty: 1
- article: White grape
  unitPrice: 2.03
  qty: 1

Returns the details property of the first element, although the file is yaml it forces the output in json format:

@diotoborg/sed-sunt eval '.[0].details' ./data/orders.yaml -b -o json

Result:

[
  {
    "article": "Pear",
    "unitPrice": 1.78,
    "qty": 2
  },
  {
    "article": "Banana",
    "unitPrice": 1.99,
    "qty": 1
  },
  {
    "article": "White grape",
    "unitPrice": 2.03,
    "qty": 1
  }
]

Returns the details property of the listing:

@diotoborg/sed-sunt eval '.details' ./data/orders.json

Result:

[{"article":"Pear","unitPrice":1.78,"qty":2},{"article":"Banana","unitPrice":1.99,"qty":1},{"article":"White grape","unitPrice":2.03,"qty":1},{"article":"Apple","unitPrice":2.15,"qty":1},{"article":"Banana","unitPrice":1.99,"qty":2},{"article":"Pear","unitPrice":1.78,"qty":1},{"article":"Apple","unitPrice":2.15,"qty":1},{"article":"Banana","unitPrice":1.99,"qty":1},{"article":"Pear","unitPrice":1.78,"qty":1},{"article":"White grape","unitPrice":2.03,"qty":1}]

Returns the article property of the list of details of each element of the list:

@diotoborg/sed-sunt eval '.details.article' ./data/orders.json

Result:

["Pear","Banana","White grape","Apple","Banana","Pear","Apple","Banana","Pear","White grape"]

The order with the smallest total:

@diotoborg/sed-sunt eval '.map(p=>{nro:p.number,total:p.details.sum(q=> q.unitPrice * q.qty)}).min(p=> p.total)' ./data/orders.json

Result:

7.58

Get the minimum of the article property from all the details:

@diotoborg/sed-sunt eval '.details.min(p=> p.article )' ./data/orders.json

Result:

"Apple"

Get the maximum "unitPrice * p.qty" from all the details:

@diotoborg/sed-sunt eval '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.json

Result:

3.98

Get the middle value "unitPrice * p.qty" from all the details:

@diotoborg/sed-sunt eval 'round(.details.avg(p=> p.unitPrice * p.qty),2)' ./data/orders.json

Result:

2.35

Gets the sum of the total property:

@diotoborg/sed-sunt eval '.sum(p=> p.total )' ./data/orders.json

Result:

0

Get the sum "unitPrice * p.qty" of the details of item 1 of the list:

@diotoborg/sed-sunt eval '.[1].details.sum(p=> p.unitPrice * p.qty )' ./data/orders.json

Result:

7.91

Get the number of details where "unitPrice * p.qty " is less than 3:

@diotoborg/sed-sunt eval '.details.count(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json

Result:

8

Get the first article property of all details where "unitPrice * p.qty" is less than 3:

@diotoborg/sed-sunt eval '.details.first(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json

Result:

"Banana"

Get the last article property of all details where "unitPrice * p.qty" is less than 3:

@diotoborg/sed-sunt eval '.details.last(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json

Result:

"White grape"

Get the first detail where "unitPrice * p.qty" is less than 3 in beautiful format:

@diotoborg/sed-sunt eval '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json -b

Result:

{
  "article": "Banana",
  "unitPrice": 1.99,
  "qty": 1
}

Get the smallest article:

@diotoborg/sed-sunt eval '.details.min(p=> p.article )' ./data/orders.json

Result:

"Apple"

Get the total of all the details:

@diotoborg/sed-sunt eval '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.json

Result:

3.98

Average value of the price of the items purchased in the order 20003:

@diotoborg/sed-sunt eval 'round(.filter(p=> p.number == "20003").details.avg(p=> p.unitPrice),2)' ./data/orders.json

Result:

2

Get the total of the details of order 1:

@diotoborg/sed-sunt eval '.[1].details.sum(p=> p.unitPrice * p.qty )' ./data/orders.json

Result:

7.91

Gets the number of details where the subtotal is less than 3:

@diotoborg/sed-sunt eval '.details.count(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json

Result:

8

Get the article of the first detail where the subtotal is less than 3:

@diotoborg/sed-sunt eval '.details.first(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json

Result:

"Banana"

Get the article of the last detail where the subtotal is less than 3:

@diotoborg/sed-sunt eval '.details.last(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.json

Result:

"White grape"

Get the first detail where the subtotal is less than 3:

@diotoborg/sed-sunt eval '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json

Result:

{"article":"Banana","unitPrice":1.99,"qty":1}

Calculate the total for each order:

@diotoborg/sed-sunt eval '.each(p=>p.total=p.details.sum(q=>q.qty*q.unitPrice)).map(p=>{nro:p.number,total:p.total})' ./data/orders.json

Result:

[{"nro":"20001","total":7.58},{"nro":"20002","total":7.91},{"nro":"20003","total":7.949999999999999}]

Calculate the subtotal for each order:

@diotoborg/sed-sunt eval '.details.foreach(p=>p.subtotal=p.qty*p.unitPrice).subtotal' ./data/orders.json

Result:

[3.56,1.99,2.03,2.15,3.98,1.78,2.15,1.99,1.78,2.03]

calculates the total of all the details:

@diotoborg/sed-sunt eval '.details.foreach(p=>total=nvl(total,0)+p.qty*p.unitPrice);total' ./data/orders.json

Result:

23.44

Get the list of items without repeating:

@diotoborg/sed-sunt eval '.details.distinct(p=>p.article)' ./data/orders.json -b

Result:

[
  "Pear",
  "Banana",
  "White grape",
  "Apple"
]

Get the total and amount of each item:

@diotoborg/sed-sunt eval '.details.map(p=>{article:p.article,count:count(1),total:sum(p.qty * p.unitPrice)})' ./data/orders.json -b

Result:

[
  {
    "article": "Pear",
    "count": 3,
    "total": 7.12
  },
  {
    "article": "Banana",
    "count": 3,
    "total": 7.96
  },
  {
    "article": "White grape",
    "count": 2,
    "total": 4.06
  },
  {
    "article": "Apple",
    "count": 2,
    "total": 4.3
  }
]

Get the total of the first order:

@diotoborg/sed-sunt eval '{total:.[0].details.sum(p=>p.qty * p.unitPrice)}' ./data/orders.json

Result:

{"total":7.58}

Get the total of the last order:

@diotoborg/sed-sunt eval '{total:round(.[.length()-1].details.sum(p=>p.qty * p.unitPrice),2)}' ./data/orders.json -b

Result:

{
  "total": 7.95
}

List the orders with their totals:

@diotoborg/sed-sunt eval '.map(p=>{nro:p.number,total:round(p.details.sum(q=>q.qty * q.unitPrice),2)})' ./data/orders.json -b

Result:

[
  {
    "nro": "20001",
    "total": 7.6
  },
  {
    "nro": "20002",
    "total": 7.9
  },
  {
    "nro": "20003",
    "total": 7.95
  }
]

All articles that are in orders 20001 and 20003:

@diotoborg/sed-sunt eval '.[0].details.article.union(.[1].details.article)' ./data/orders.json

Result:

["Pear","Banana","White grape","Apple"]

The articles in common between order 20001 and 20002:

@diotoborg/sed-sunt eval '.[0].details.article.intersection(.[1].details.article)' ./data/orders.json

Result:

["Banana","Pear"]

Articles that are in order 20001 and are not in order 20002:

@diotoborg/sed-sunt eval '.[0].details.article.difference(.[1].details.article)' ./data/orders.json

Result:

["White grape"]

Articles of orders 20001 and 20003 that are not shared:

@diotoborg/sed-sunt eval '.[0].details.article.symmetricDifference(.[1].details.article)' ./data/orders.json

Result:

["White grape","Apple"]

Get the sum "unitPrice * p.qty" of the details of item 1 of the list using pipeline:

curl -s https://raw.githubusercontent.com/data7expressions/@diotoborg/sed-sunt/main/data/orders.json | @diotoborg/sed-sunt eval '.details.sum(p=> p.unitPrice * p.qty )'

Result:

23.44

js-expression

Js-expression is an extensible expression evaluator and parser.

Besides the operators, functions, variables, objects and arrays that are supported.

Documentation

Float32ArrayregexqueryexpressperformanthttpsatomdragreduxxhrargumentwalkTypeScriptpackagekoreanshellString.prototype.trimtslibrestfulhassharedconsumebeanstalkfilehtmldeepclonecloudwatchcode points_.extendwhatwgstylingcreatejson-schema-validatorcallbackmetadatavaliddebuggerfile systemJSON-SchemaArray.prototype.flattenArrayBufferbufferfixed-widthtransportcachejavascriptes-abstractcommandminimalestreeproputilawesomesauceredux-toolkitramdatddyamlECMAScript 2021trimRightprotobufsuperstructentriesweburlvalidatetoReversed256mkdirsgroupcomputed-typesreact animationdeterministicemojireducerHyBipolyfillpushexecutedotenvprivate datapyyamlconstReflect.getPrototypeOfbinaryrapidECMAScript 5avanegativeglobalterminalwafoptimizerstylestransformwalkingObject.definePropertyhookformendpointstoragegatewayvalidatorclasseszxmanagerreduceloggingtakees7typescriptflattendirrmdirthree-0fpsfetchworkspace:*searchglobal objectchecktypeofrsselbhasOwngetintrinsicappStyleSheetrouteObject.assignbuffersES2018class-validatorquerystringtoolsimmutablejoiqueuelocalStreamqueueMicrotasksymlinksdeletecompilerPushcurriedcloudfrontsource maptypedprefixjson-schema-validationtranspilekeysPromisesymbolimportexportcjksinatrakinesisgetOwnPropertyDescriptorreact-testing-librarytoolkitoptionreuselocationinternalclipropertyparentsjsonforkcollection.es6Mappreprocessorsnstaparraymatchfull-widthECMAScript 2015loadbalancingwriteqsboundtestarktyperegularinWeakMappopmotioncolorvarObject.fromEntriesnumbererrortestersymbolsidglacierttyes2018SymbolemitfrompositivematchesInt16ArrayharmonyobjfponceairbnbflagchromeoutputmoduleimportlibphonenumberArray.prototype.filterglobprototypetermjapaneseSymbol.toStringTagECMAScriptregular-expressionruntimeinstallroute53YAMLredirecttextwhichdescriptorbabel-corespawnposecirculartypeerrorstringifieres5readWebSocketschildeast-asian-widthES8parserES6routerform-validation
3.8.110

10 months ago

3.8.109

10 months ago

3.8.108

10 months ago

3.8.107

10 months ago

3.8.106

10 months ago

3.8.105

10 months ago

3.8.104

10 months ago

3.8.103

10 months ago

3.8.102

10 months ago

2.8.102

10 months ago

2.8.101

10 months ago

2.8.100

10 months ago

2.7.100

10 months ago

2.7.99

10 months ago

2.6.99

11 months ago

2.6.98

11 months ago

2.6.97

11 months ago

2.6.96

11 months ago

2.6.95

11 months ago

2.6.94

11 months ago

2.5.94

11 months ago

2.5.93

11 months ago

2.5.92

11 months ago

2.5.91

11 months ago

2.5.90

11 months ago

2.5.89

11 months ago

2.5.88

11 months ago

2.5.87

11 months ago

2.5.86

11 months ago

2.5.85

11 months ago

2.5.84

11 months ago

2.5.83

11 months ago

2.4.83

11 months ago

2.3.83

11 months ago

2.3.82

11 months ago

2.3.81

11 months ago

2.3.80

11 months ago

2.3.79

11 months ago

2.3.78

11 months ago

2.3.77

11 months ago

2.3.76

11 months ago

2.3.75

11 months ago

2.3.74

11 months ago

2.3.73

11 months ago

2.3.72

12 months ago

2.3.71

12 months ago

2.3.70

12 months ago

2.3.69

12 months ago

2.3.68

12 months ago

2.3.67

12 months ago

2.3.66

12 months ago

2.3.65

12 months ago

2.3.64

12 months ago

2.3.63

12 months ago

2.2.63

12 months ago

2.2.62

12 months ago

2.2.61

12 months ago

2.2.60

12 months ago

2.2.59

12 months ago

2.2.58

12 months ago

2.2.57

12 months ago

2.2.56

12 months ago

2.2.55

12 months ago

2.2.54

12 months ago

1.2.54

1 year ago

1.2.53

1 year ago

1.2.52

1 year ago

1.1.52

1 year ago

1.1.51

1 year ago

1.1.50

1 year ago

1.1.49

1 year ago

1.1.48

1 year ago

1.1.47

1 year ago

1.1.46

1 year ago

1.1.45

1 year ago

1.1.44

1 year ago

1.1.43

1 year ago

1.1.42

1 year ago

1.1.41

1 year ago

1.1.40

1 year ago

1.1.39

1 year ago

1.1.38

1 year ago

1.1.37

1 year ago

1.1.36

1 year ago

1.1.35

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.33

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.30

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago