It is possible to configure functions as strings to parse them to functions during runtime.
The following example functionAsString
expects input and deals with it, I only know that it MUST return a boolean ( I'm expecting that )
const x = {
fields: {
age: 0
}
};
const y = {
fields: {
age: 1
}
};
const functionAsString = "(left, right) => left.fields.age < right.fields.age";
const compareFunction = new Function(functionAsString);
const isXLessThanY = compareFunction(x, y);
if (isXLessThanY === undefined) {
console.error("it should not be undefined...");
} else {
console.log({
isXLessThanY
});
}
isXLessThanY
is undefined
. Do you know how to setup a valid function based on a string?