Catalog npm
PACKAGE EXPLORERView in registry

3xpr

npm

expressions

evalevaluatorexpexpressionexpressionsparser
Versions collected98
Direct dependencies2From release 5.0.0
Last observed20 Oct, 19:55 UTC

README

Current package documentation

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}}

Extend

You can extend the library by adding enums, constants, formats, operators, and functions. To do this, use the following functions:

  • AddConstant: Adds a constant to the library.
  • AddEnum: Adds an enumeration to the library.
  • AddFormat: Adds a format to the library.
  • AddOperator: Adds an operator to the library.
  • AddFunction: Adds a function to the library.

Example

import { expressions as exp } from '3xpr'
const CryptoJS = require('crypto-js')

exp.addFunction(
	'encrypt(value:string):string',
	(value: string, key:string):string => CryptoJS.AES.encrypt(value, key).toString(),
	{ description: 'Encrypt a string' }
)
exp.addFunction(
	'decrypt(value:string):string', 
	(value: string, key:string):string => CryptoJS.AES.decrypt(value, key).toString(CryptoJS.enc.Utf8),
	{ description: 'Decrypt a string' }
)

Documentation

Full documentation is available in the Wiki.

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Versions 98

Collected releases
VersionPublishedDirect dependenciesOpen version
1.15.1215 Apr 20242
1.15.1114 Apr 20242
1.15.1020 Mar 20242
1.15.916 Feb 20242
1.15.716 Feb 20242
1.15.626 Jan 20242
1.15.521 Jan 20242
1.15.421 Jan 20242
1.15.321 Jan 20242
1.15.218 Jan 20242
1.15.118 Jan 20242
1.15.015 Dec 20232
1.14.11 Dec 20232
1.14.01 Dec 20232
1.13.21 Dec 20232
1.13.11 Dec 20232
1.13.01 Dec 20232
1.12.2028 Nov 20232
1.12.1826 Nov 20232
1.12.1726 Nov 20232

Package advisories

0
No matching advisories collected

This describes our collected data; it is not a security assessment.

Repository

PENDING
flaviolionelrita/3xpr

Shared repository data · GitHub

License detected in repository
Not collected

Repository

PENDING
data7expressions/3xpr

Shared repository data · GitHub

License detected in repository
Not collected

Repository

PENDING
expr-solver/3xpr

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 19:55 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package:617845d3c31f9c05004ccbd33ef109de0f2cf5b64585b0ff5931d95e34d26b19