kantime-rule-engine
Version:
A library for custom validation in the form based on the user defined json schema.
69 lines (62 loc) • 1.84 kB
JavaScript
let _ = require("lodash");
let functions = {
/*
Invert the bool value
Input: bool
output: bool
*/
INVERT: {
apply: function (input) {
try {
input = input == 'true' ? true : input;
input = input == 'false' ? false : input;
if(!_.isBoolean(input)){
throw "Incorrect Boolean Value - '" + input + "'";
}
return !input;
} catch (error) {
throw "INVERTFunctionException: " + error;
}
}
},
/*
calculate Age
Input: date
output: int
*/
AGE: {
apply: function (dateString) {
try {
// date is valid or not
// if(!_.isDate(dateString)){
// throw "Incorrect Date String - '" + dateString + "'";
// }
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
} catch (error) {
throw "AGEFunctionException: " + error;
}
}
},
/*
Find length of string
Input: string
output: int
*/
LENGTH: {
apply: function (input) {
try {
return input.length;
} catch (error) {
throw "LENGTHFunctionException: " + error;
}
}
}
}
module.exports = functions;