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