som-exp-sdk
Version:
Evaluate the User Expression
109 lines (108 loc) • 4.56 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NumericalExpression = void 0;
const js_big_decimal_1 = __importDefault(require("js-big-decimal"));
const constants_1 = require("./constants");
const numberToWords = require("number-to-words");
class NumericalExpression {
constructor() {
this.round = (value, places) => {
let output = this.const.INVALID_EXPRESSION;
try {
if (!!value) {
if (places >= 0) {
output = new js_big_decimal_1.default(value)
.round(places, js_big_decimal_1.default.RoundingModes.HALF_DOWN).getValue();
}
else {
console.error(`Incorrect rounding places value: ${places} cannot be rounded to negative values.`);
}
}
}
catch (e) {
console.log(`Error while rounding the value. Error is ${e}`);
}
return output;
};
this.roundUp = (value, places) => {
let output = this.const.INVALID_EXPRESSION;
try {
if (!!value) {
if (places >= 0) {
output = new js_big_decimal_1.default(value)
.round(places, js_big_decimal_1.default.RoundingModes.CEILING).getValue();
}
else {
console.error(`Incorrect rounding places value: ${places} cannot be rounded to negative values.`);
}
}
}
catch (e) {
console.log(`Error while rounding up the value. Error is ${e}`);
}
return output;
};
this.roundDown = (value, places) => {
let output = this.const.INVALID_EXPRESSION;
try {
if (!!value) {
if (places >= 0) {
output = new js_big_decimal_1.default(value)
.round(places, js_big_decimal_1.default.RoundingModes.DOWN).getValue();
}
else {
console.error(`Incorrect rounding places value: ${places} cannot be rounded to negative values.`);
}
}
}
catch (e) {
console.log(`Error while rounding down the value. Error is ${e}`);
}
return output;
};
this.power = (base, exponent) => {
let output = this.const.INVALID_EXPRESSION;
if (base && exponent) {
output = (Math.pow(base, exponent)).toString();
}
return output;
};
this.spellout = (value) => {
let output = this.const.INVALID_EXPRESSION;
if (!!value) {
let inputArr = String(value).split(".");
let isValidInputArr = inputArr
&& Array.isArray(inputArr)
&& (inputArr.length > 0);
if (isValidInputArr) {
let wholeNumber = parseInt(inputArr[0]);
const wholeNumberText = numberToWords.toWords(wholeNumber);
output = wholeNumberText;
// check if the input value has decimal
if (inputArr.length > 1) {
const decimalPointPart = inputArr[1];
let decimalText = "";
for (let num = 0; num < decimalPointPart.length; ++num) {
let decimalValue = parseInt(decimalPointPart[num]);
let decimalValueText = this.const.DIGITS_IN_WORDS[decimalValue];
decimalText = `${decimalText} ${decimalValueText}`;
}
output = `${output} ${this.const.POINT} ${decimalText}`;
}
}
else {
console.log("Value must be a string of digits.");
}
}
else {
console.log("Value cannot be undefined or empty.");
}
return output;
};
this.const = new constants_1.Constants();
}
}
exports.NumericalExpression = NumericalExpression;