1.15.20 • Published 2 days ago

3xpr v1.15.20

Weekly downloads
-
License
Apache-2.0 Licens...
Repository
github
Last release
2 days ago

3xpr

3xpr is an extensible expression evaluator and parser.

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

It is possible to extend it with your own functions, operators, etc

Features

Quick start

import { expressions as exp } from '3xpr'

const context = {
name: 'Spain',
region: 'Europe',
phoneCode: '34',
timezones: [
	{ name: 'Madrid', offset: 1, pos: { lat: 40.4165, log: -3.70256 } },
	{ name: 'Ceuta', offset: 1, pos: { lat: 35.8883, log: -5.3162 } },
	{ name: 'Canary', offset: 0, pos: { lat: 28.1248, log: -15.43 } }
	]
}

const result = exp.eval('5*(7+9)==(5*7+5*9)')
console.log(result)
// Output: true

// use context
exp.eval('toNumber(phoneCode) <= 30', context)
// false

// template
exp.eval('`${name} belongs to ${region}`', context)
// 'Spain belongs to Europe'

exp.eval('timezones.filter(p => substring(p.name,0,1)=="C")', context)
// ['{"name":"Ceuta","offset":1,"pos":{"lat":35.8883,"log":-5.3162}}'
// ,'{"name":"Canary","offset":0,"pos":{"lat":28.1248,"log":-15.43}}']

exp.eval('timezones.filter(p => p.offset == 1).sort(p => p.pos.lat).name', context)
// ['Ceuta','Madrid']

exp.eval('stringify(timezones.first(p => p.name == "Madrid").pos)', context)
// '{"lat":40.4165,"log":-3.70256}'

exp.eval('timezones.filter(p => p.pos.lat > 30 && p.pos.log > -4).pos.lat', context)
// [40.4165]

exp.eval('sort(timezones.name)', context)
// ['Canary','Ceuta','Madrid']

exp.eval('timezones[0].name', context)
// 'Madrid'

exp.eval('round(timezones.first(p=> p.name =="Madrid").pos.lat - timezones.first(p=> p.name =="Ceuta").pos.lat,2)', context)
// 4.55

exp.eval('timezones.each(p => p.pos={lat:round(p.pos.lat,2),log:round(p.pos.log,2)}).map(p=> stringify(p))', context)
// ['{"name":"Madrid","offset":1,"pos":{"lat":40.4,"log":-3.7}}'
// ,'{"name":"Ceuta","offset":1,"pos":{"lat":35.9,"log":-5.3}}'
// ,'{"name":"Canary","offset":0,"pos":{"lat":28.1,"log":-15.45}}']

exp.eval(`
list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
total = 0;
for (i = 0; i < list.length(); i += 1) {
	total += list[i];
}
`, context)
console.log(context.total)
// 45

exp.eval(`
while (p=timezones.pop()) {
	console(p);
}
`, context)
// outputs:
//         {"name":"Canary","offset":0,"pos":{"lat":28.1,"log":-15.45}}
//         {"name":"Ceuta","offset":1,"pos":{"lat":35.9,"log":-5.3}}
//         {"name":"Madrid","offset":1,"pos":{"lat":40.4,"log":-3.7}}

Related projects

Documentation

Full documentation is available in the Wiki.

1.15.18

2 days ago

1.15.19

2 days ago

1.15.16

2 days ago

1.15.17

2 days ago

1.15.20

2 days ago

1.15.15

16 days ago

1.15.14

16 days ago

1.15.13

18 days ago

1.15.12

23 days ago

1.15.11

24 days ago

1.15.10

2 months ago

1.15.7

3 months ago

1.15.9

3 months ago

1.15.6

3 months ago

1.15.4

4 months ago

1.15.3

4 months ago

1.15.5

4 months ago

1.15.2

4 months ago

1.15.1

4 months ago

1.15.0

5 months ago

1.14.1

5 months ago

1.14.0

5 months ago

1.12.20

5 months ago

1.12.18

5 months ago

1.12.17

5 months ago

1.13.2

5 months ago

1.13.1

5 months ago

1.13.0

5 months ago

1.12.7

11 months ago

1.12.9

11 months ago

1.12.8

11 months ago

1.12.10

11 months ago

1.12.3

12 months ago

1.10.5

1 year ago

1.12.2

12 months ago

1.10.4

1 year ago

1.12.1

12 months ago

1.10.3

1 year ago

1.12.0

12 months ago

1.10.2

1 year ago

1.12.6

12 months ago

1.12.4

12 months ago

1.9.12

1 year ago

1.9.11

1 year ago

1.9.10

1 year ago

1.9.9

1 year ago

1.9.8

1 year ago

1.9.7

1 year ago

1.9.6

1 year ago

1.9.5

1 year ago

1.9.4

1 year ago

1.9.3

1 year ago

1.9.2

1 year ago

1.11.0

1 year ago

1.11.4

1 year ago

1.11.3

1 year ago

1.11.2

1 year ago

1.11.1

1 year ago

1.11.5

1 year ago

1.9.1

1 year ago

1.9.0

1 year ago

1.10.1

1 year ago

1.10.0

1 year ago

1.8.0

1 year ago

1.7.2

1 year ago

1.7.1

1 year ago

1.7.0

1 year ago

1.7.9

1 year ago

1.7.10

1 year ago

1.7.8

1 year ago

1.7.7

1 year ago

1.6.11

1 year ago

1.7.6

1 year ago

1.6.10

1 year ago

1.6.12

1 year ago

1.6.2

1 year ago

1.6.1

1 year ago

1.6.0

1 year ago

1.6.9

1 year ago

1.6.8

1 year ago

1.6.7

1 year ago

1.6.6

1 year ago

1.5.11

1 year ago

1.5.10

1 year ago

1.5.9

1 year ago

1.5.4

1 year ago