1.0.0 • Published 5 months ago
ignate-expression-eval v1.0.0
Ignate Expression Evaluation Library with Date, Random, and String Functions
Overview
This JavaScript library extends the math.js
library by adding custom functions for date manipulation, random number generation, and string operations. It uses moment-timezone
for handling time zones and formatting dates effectively.
Features
- Date Functions: Add, subtract, format, and get start/end of week, month, or year.
- Random Functions: Generate random dates, integers, and floating-point numbers.
- String Functions: Perform various string operations such as trimming, concatenation, replacement, and case conversion.
- Expression Evaluation: Supports mathematical and logical expressions.
Dependencies
mathjs
moment-timezone
Installation
To use this library, install the required dependencies:
npm install mathjs moment-timezone
Usage
Import the library and evaluate expressions:
const { evaluateExpression } = require("./ignate-expression-eval");
console.log(evaluateExpression("2 + 3 * 5")); // 17
console.log(evaluateExpression("dateAdd('2025-02-11', 5, 'days', 'UTC')")); // "2025-02-16"
console.log(evaluateExpression("toUpperCase('hello')")); // "HELLO"
console.log(evaluateExpression("randomInt(1, 100)")); // Random integer between 1 and 100
console.log(evaluateExpression("2 + 3 * 5")); // 17
console.log(
evaluateExpression("dateFormat(dateAdd(now(), 5, 'days'),'yyyy/MM/DD')")
); // "2025-02-16"
console.log(evaluateExpression("now()")); // Current date-time in ISO format
console.log(evaluateExpression("dateFormat('2025-02-11', 'yyyy/MM/dd')")); // "2025/02/11"
console.log(
evaluateExpression("dateFormat(dateSubtract(now(), 5, 'days'),'yyyy/MM/dd')")
); // "2025/02/06"
console.log(evaluateExpression("getStartOfWeek(now())"));
console.log(evaluateExpression("getEndOfWeek(now())"));
console.log(evaluateExpression("getStartOfMonth(now())"));
console.log(evaluateExpression("getEndOfMonth(now())"));
console.log(evaluateExpression("getStartOfYear(now())"));
console.log(evaluateExpression("getEndOfYear(now())"));
console.log(evaluateExpression("randomInt(1,33)"));
console.log(evaluateExpression("generateUUID()"));
Available Functions
Date Functions
now(timeZone)
- Returns the current date-time in the specified time zone.today(timeZone)
- Returns the current date inYYYY-MM-DD
format.dateAdd(dateStr, amount, unit, timeZone)
- Adds a specified amount of time to a date.dateSubtract(dateStr, amount, unit, timeZone)
- Subtracts a specified amount of time from a date.dateFormat(dateStr, format, timeZone)
- Formats a date based on the given format string.getStartOfWeek(dateStr, timeZone)
,getEndOfWeek(dateStr, timeZone)
- Get start and end of the week.getStartOfMonth(dateStr, timeZone)
,getEndOfMonth(dateStr, timeZone)
- Get start and end of the month.getStartOfYear(dateStr, timeZone)
,getEndOfYear(dateStr, timeZone)
- Get start and end of the year.
Random Functions
randomDate(startDate, endDate)
- Returns a random date between two dates.randomInt(min, max)
- Returns a random integer in the given range.randomFloat(min, max)
- Returns a random floating-point number in the given range.
String Functions
toUpperCase(str)
,toLowerCase(str)
- Converts string to upper/lower case.trim(str)
- Trims whitespace from a string.substring(str, start, end)
- Extracts a substring.replace(str, search, replacement)
- Replaces occurrences of a substring.includes(str, search)
- Checks if a string contains another string.split(str, delimiter)
- Splits a string into an array.concat(...args)
- Concatenates multiple strings.length(str)
- Returns the length of a string.startsWith(str, search)
,endsWith(str, search)
- Checks if a string starts/ends with a given substring.
License
This library is open-source and available under the MIT License.
1.0.0
5 months ago