@milichev/funexp
A simple template function expression generator.
Installation
NPM:
npm install @milichev/funexp
Yarn:
yarn add @milichev/funexp
Usage
With this tiny tagged expression processor, you can create a highly performant functions in runtime.
const toDate = jest.fn((value: Date | string | number) => new Date(value));
const discount = 0.75;
const calcTotal = jest.fn(
(price: number, discount: number) => price * discount
);
const myExpr = makeFun({ ctxVar: "methods", entryPrefix: "$m_" });
const exp = myExpr`
model.createdOn = ${toDate}(model.createdOn);
model.total = ${calcTotal}(model.price, ${discount});
return model;`;
const patcher = new Function("methods", "model", exp.src).bind(null, exp.ctx);
const createdOn = "2023-05-26T14:04:08.023Z";
const model = { createdOn, price: 100 };
const result = patcher(model);
expect(toDate).toHaveBeenCalledWith(createdOn);
expect(calcTotal).toHaveBeenCalledWith(100, 0.75);
expect(result).toBe(model);
expect(result.createdOn).toEqual(new Date(createdOn));
expect(result.total).toEqual(75);