Skip to content

Commit

Permalink
feat(functions)!: remove builtin math functions
Browse files Browse the repository at this point in the history
Functions should be added as external for more flexible customization.

BREAKING CHANGE
  • Loading branch information
wmakeev committed Apr 16, 2024
1 parent 1d10bda commit 72e55eb
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 121 deletions.
20 changes: 0 additions & 20 deletions dist/browser/filtrex.js
Original file line number Diff line number Diff line change
Expand Up @@ -3256,16 +3256,6 @@ var filtrex = (function (exports) {
* * `if x then y else z` If boolean x, value y, else z
* * `( x )` Explicity operator precedence
* * `( x, y, z )` Array of elements x, y and z
* * `abs(x)` Absolute value
* * `ceil(x)` Round floating point up
* * `floor(x)` Round floating point down
* * `log(x)` Natural logarithm
* * `log2(x)` Binary logarithm
* * `log10(x)` Decadic logarithm
* * `max(a, b, c...)` Max value (variable length of args)
* * `min(a, b, c...)` Min value (variable length of args)
* * `round(x)` Round floating point
* * `sqrt(x)` Square root
* * `exists(x)` True if `x` is neither `undefined` nor `null`
* * `empty(x)` True if `x` doesn't exist, it is an empty string or empty array
* * `myFooBarFunction(x)` Custom function defined in `options.extraFunctions`
Expand All @@ -3291,16 +3281,6 @@ var filtrex = (function (exports) {


var functions = {
abs: Math.abs,
ceil: Math.ceil,
floor: Math.floor,
log: Math.log,
log2: Math.log2,
log10: Math.log10,
max: Math.max,
min: Math.min,
round: Math.round,
sqrt: Math.sqrt,
exists: function exists(v) {
return v !== undefined && v !== null;
},
Expand Down
2 changes: 1 addition & 1 deletion dist/browser/filtrex.min.js

Large diffs are not rendered by default.

20 changes: 0 additions & 20 deletions dist/cjs/filtrex.js
Original file line number Diff line number Diff line change
Expand Up @@ -3041,16 +3041,6 @@ function useDotAccessOperatorAndOptionalChaining(name, get, obj, type) {
* * `if x then y else z` If boolean x, value y, else z
* * `( x )` Explicity operator precedence
* * `( x, y, z )` Array of elements x, y and z
* * `abs(x)` Absolute value
* * `ceil(x)` Round floating point up
* * `floor(x)` Round floating point down
* * `log(x)` Natural logarithm
* * `log2(x)` Binary logarithm
* * `log10(x)` Decadic logarithm
* * `max(a, b, c...)` Max value (variable length of args)
* * `min(a, b, c...)` Min value (variable length of args)
* * `round(x)` Round floating point
* * `sqrt(x)` Square root
* * `exists(x)` True if `x` is neither `undefined` nor `null`
* * `empty(x)` True if `x` doesn't exist, it is an empty string or empty array
* * `myFooBarFunction(x)` Custom function defined in `options.extraFunctions`
Expand All @@ -3077,16 +3067,6 @@ function compileExpression(expression, options) {


var functions = {
abs: Math.abs,
ceil: Math.ceil,
floor: Math.floor,
log: Math.log,
log2: Math.log2,
log10: Math.log10,
max: Math.max,
min: Math.min,
round: Math.round,
sqrt: Math.sqrt,
exists: function exists(v) {
return v !== undefined && v !== null;
},
Expand Down
20 changes: 0 additions & 20 deletions dist/esm/filtrex.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3035,16 +3035,6 @@ function useDotAccessOperatorAndOptionalChaining(name, get, obj, type) {
* * `if x then y else z` If boolean x, value y, else z
* * `( x )` Explicity operator precedence
* * `( x, y, z )` Array of elements x, y and z
* * `abs(x)` Absolute value
* * `ceil(x)` Round floating point up
* * `floor(x)` Round floating point down
* * `log(x)` Natural logarithm
* * `log2(x)` Binary logarithm
* * `log10(x)` Decadic logarithm
* * `max(a, b, c...)` Max value (variable length of args)
* * `min(a, b, c...)` Min value (variable length of args)
* * `round(x)` Round floating point
* * `sqrt(x)` Square root
* * `exists(x)` True if `x` is neither `undefined` nor `null`
* * `empty(x)` True if `x` doesn't exist, it is an empty string or empty array
* * `myFooBarFunction(x)` Custom function defined in `options.extraFunctions`
Expand All @@ -3071,16 +3061,6 @@ function compileExpression(expression, options) {


var functions = {
abs: Math.abs,
ceil: Math.ceil,
floor: Math.floor,
log: Math.log,
log2: Math.log2,
log10: Math.log10,
max: Math.max,
min: Math.min,
round: Math.round,
sqrt: Math.sqrt,
exists: function exists(v) {
return v !== undefined && v !== null;
},
Expand Down
21 changes: 0 additions & 21 deletions dist/esnext/filtrex.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,6 @@ export function useDotAccessOperator(name, get, obj, type) {
* * `if x then y else z` If boolean x, value y, else z
* * `( x )` Explicity operator precedence
* * `( x, y, z )` Array of elements x, y and z
* * `abs(x)` Absolute value
* * `ceil(x)` Round floating point up
* * `floor(x)` Round floating point down
* * `log(x)` Natural logarithm
* * `log2(x)` Binary logarithm
* * `log10(x)` Decadic logarithm
* * `max(a, b, c...)` Max value (variable length of args)
* * `min(a, b, c...)` Min value (variable length of args)
* * `round(x)` Round floating point
* * `sqrt(x)` Square root
* * `exists(x)` True if `x` is neither `undefined` nor `null`
* * `empty(x)` True if `x` doesn't exist, it is an empty string or empty array
* * `myFooBarFunction(x)` Custom function defined in `options.extraFunctions`
Expand All @@ -301,18 +291,7 @@ export function compileExpression(expression, options) {


// Functions available to the expression

let functions = {
abs: Math.abs,
ceil: Math.ceil,
floor: Math.floor,
log: Math.log,
log2: Math.log2,
log10: Math.log10,
max: Math.max,
min: Math.min,
round: Math.round,
sqrt: Math.sqrt,
exists: (v) => v !== undefined && v !== null,
empty: (v) => v === undefined || v === null || v === '' || Array.isArray(v) && v.length === 0
}
Expand Down
21 changes: 0 additions & 21 deletions src/filtrex.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,6 @@ export function useDotAccessOperator(name, get, obj, type) {
* * `if x then y else z` If boolean x, value y, else z
* * `( x )` Explicity operator precedence
* * `( x, y, z )` Array of elements x, y and z
* * `abs(x)` Absolute value
* * `ceil(x)` Round floating point up
* * `floor(x)` Round floating point down
* * `log(x)` Natural logarithm
* * `log2(x)` Binary logarithm
* * `log10(x)` Decadic logarithm
* * `max(a, b, c...)` Max value (variable length of args)
* * `min(a, b, c...)` Min value (variable length of args)
* * `round(x)` Round floating point
* * `sqrt(x)` Square root
* * `exists(x)` True if `x` is neither `undefined` nor `null`
* * `empty(x)` True if `x` doesn't exist, it is an empty string or empty array
* * `myFooBarFunction(x)` Custom function defined in `options.extraFunctions`
Expand All @@ -301,18 +291,7 @@ export function compileExpression(expression, options) {


// Functions available to the expression

let functions = {
abs: Math.abs,
ceil: Math.ceil,
floor: Math.floor,
log: Math.log,
log2: Math.log2,
log10: Math.log10,
max: Math.max,
min: Math.min,
round: Math.round,
sqrt: Math.sqrt,
exists: (v) => v !== undefined && v !== null,
empty: (v) => v === undefined || v === null || v === '' || Array.isArray(v) && v.length === 0
}
Expand Down
23 changes: 6 additions & 17 deletions test/arithmetics.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ const { compileExpression } = require("../dist/cjs/filtrex");
const { describe, it } = require("mocha");
const { expect } = require("chai");

const eval = (str, obj) => compileExpression(str)(obj);


const eval = (str, obj) => compileExpression(str, {
extraFunctions: {
'min': Math.min,
'max': Math.max
}
})(obj);


describe('Arithmetics', () => {
Expand All @@ -21,20 +24,6 @@ describe('Arithmetics', () => {
expect( eval('2 * 3 ^ 2') ).equals(18)
});


it('does math functions', () => {
expect( eval('abs(-5)') ).equals(5);
expect( eval('abs(5)') ).equals(5);
expect( eval('ceil(4.1)') ).equals(5);
expect( eval('ceil(4.6)') ).equals(5);
expect( eval('floor(4.1)') ).equals(4);
expect( eval('floor(4.6)') ).equals(4);
expect( eval('round(4.1)') ).equals(4);
expect( eval('round(4.6)') ).equals(5);
expect( eval('sqrt(9)') ).equals(3);
});


it('supports functions with multiple args', () => {
expect( eval('min()') ).equals(Infinity);
expect( eval('min(2)') ).equals(2);
Expand Down
9 changes: 8 additions & 1 deletion test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,14 @@ describe('Various other things', () => {
});

it('kitchensink', () => {
var kitchenSink = compileExpression('if 4 > lowNumber * 2 and (max(a, b) < 20 or foo:bar) then 1.1 else 9.4');
var kitchenSink = compileExpression(
'if 4 > lowNumber * 2 and (max(a, b) < 20 or foo:bar) then 1.1 else 9.4',
{
extraFunctions: {
max: Math.max
}
}
);
expect( kitchenSink({lowNumber: 1.5, a: 10, b: 12, 'foo:bar': false}) ).equals(1.1);
expect( kitchenSink({lowNumber: 3.5, a: 10, b: 12, 'foo:bar': false}) ).equals(9.4);
});
Expand Down

0 comments on commit 72e55eb

Please sign in to comment.