🧮 applyCalc
applyCalc is a powerful utility for safely manipulating values, including numbers, strings, arrays, and objects. It supports advanced operations such as mathematical expressions, date adjustments, string transformations, and recursive object merging, all while providing safeguards against unsafe code execution.
Import
1import { applyCalc } from "hbh-nodes";
All functions from this module are available under the applyCalc namespace.
Functions
1. applyCalc.applyCalc(oldVal, newVal, context = {})
Applies transformations to a value (oldVal) based on the input (newVal). Supports:
- Numbers: arithmetic, percentage changes, math expressions, increments/decrements.
- Dates: adding/subtracting days, months, years, hours, minutes, or seconds.
- Strings: modifiers like trim, upper/lowercase, capitalize, title case, reverse, append, prepend, slice, padding, repeat, and regex replacement.
- Objects & Arrays: recursive updates, deletion, merging, pushing/popping/shifting/unshifting array items, deduplication, and conditional fallback.
Example:
1const oldVal = 10;2const newVal = "++5"; // increment by 53const result = applyCalc.applyCalc(oldVal, newVal);4console.log(result); // 15
1const date = new Date("2025-01-01");2const newDate = applyCalc.applyCalc(date, "++10d");3console.log(newDate); // 2025-01-11
1const str = "hello";2const modified = applyCalc.applyCalc(str, " | upper | reverse");3console.log(modified); // "OLLEH"
1const obj = { a: 1, b: 2 };2const updated = applyCalc.applyCalc(obj, { b: "__DELETE__", c: 3 });3console.log(updated); // { a: 1, c: 3 }
2. applyCalc.safeMathEval(expr, val, context = {})
Safely evaluates mathematical expressions using only allowed operations. Prevents access to global objects, constructors, or dangerous code.
Example:
1const result = applyCalc.safeMathEval("val * 2 + 5", 10);2console.log(result); // 25
3. applyCalc.interpolateString(str, context)
Replaces placeholders in a string with values from a context object. Placeholders use {{key}} syntax.
Example:
1const text = "Hello {{name}}!";2const result = applyCalc.interpolateString(text, { name: "Alice" });3console.log(result); // "Hello Alice!"
4. applyCalc.applyStringModifiers(val, modifiers, context)
Applies a series of string transformations/modifiers to a value. Supports:
trim,upper,lower,capitalize,title,reverseappend:<text>,prepend:<text>slice:start:end,padStart:length:char,padEnd:length:char,repeat:countreplace:from:to,replaceRegex:/pattern/flags:replacement- Interpolation using
{{key}} - Special:
__now__→ current ISO timestamp
Example:
1const str = " hello world ";2const result = applyCalc.applyStringModifiers(str, ["trim", "title"]);3console.log(result); // "Hello World"
5. applyCalc.dateAdd(date, amount, unit)
Adds or subtracts units of time from a JavaScript Date object. Supported units: d (days), m (months), y (years), h (hours), min (minutes), s (seconds).
Example:
1const newDate = applyCalc.dateAdd(new Date("2025-01-01"), 5, "d");2console.log(newDate); // 2025-01-06
Usage Example
1import { applyCalc } from "hbh-nodes";3// Number increment4const num = applyCalc.applyCalc(100, "++10%");5console.log(num); // 1107// String transformation8const text = applyCalc.applyCalc("hello", "| upper | reverse");9console.log(text); // "OLLEH"11// Object update with deletion12const obj = { a: 1, b: 2 };13const updated = applyCalc.applyCalc(obj, { b: "__DELETE__", c: 3 });14console.log(updated); // { a: 1, c: 3 }16// Array merge and deduplicate17const arr = [{ id: 1 }, { id: 2 }];18const merged = applyCalc.applyCalc(arr, { __MERGE__: true, items: [{ id: 2 }, { id: 3 }], __UNIQUE__: true });19console.log(merged); // [{ id: 1 }, { id: 2 }, { id: 3 }]