UNPKG

som-exp-sdk

Version:

Evaluate the User Expression

175 lines (174 loc) 6.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SentenceExpression = void 0; const constants_1 = require("./constants"); class SentenceExpression { constructor() { this.charCount = (token) => { let output = ""; if (!!token) { output = token.length.toString(); } return output; }; this.wordCount = (token) => { let output = "0"; if (!!token) { let textArr = token.split(" "); output = textArr.length.toString(); } return output; }; this.word = (token, position) => { let output = ""; let str = new Array(); if (!!token && position > 0) { let words = token.split(" "); if (words && Array.isArray(words) && words.length > 0) { for (let tkn = 0; tkn < words.length; ++tkn) { if (words != null) { str.push(words[tkn]); } } if (position <= str.length) { output = str[position - 1]; } } } return output; }; this.upper = (token) => { return (!!token) ? token.toUpperCase() : ""; }; this.lower = (fullName) => { return (!!fullName) ? fullName.toLowerCase() : ""; }; this.extractFirstName = (token) => { let output = ""; let str = new Array(); if (!!token) { let tknArr = token.split(" "); if (tknArr && Array.isArray(tknArr) && tknArr.length > 0) { for (let tkn = 0; tkn < tknArr.length; ++tkn) { if (!!tknArr) { str.push(tknArr[tkn]); } } if (str.length > 0) { output = str[0]; } } } return output; }; this.extractMiddleName = (token) => { let output = ""; if (!!token) { token = token.trim(); let startPos = 0; while (startPos < token.length && token.charAt(startPos) != " ") { startPos++; } let endPos = token.length - 1; while (endPos > 0 && token.charAt(endPos) != " ") { endPos--; } output = token.substring(startPos, endPos + 1).trim(); } return output; }; this.extractLastName = (token) => { let output = ""; if (!!token) { let words = token.split(" "); if (words && Array.isArray(words) && words.length > 0) { if (words.length == 2) { output = words[1]; } if (words.length > 2) { for (let i = 2; i < words.length; ++i) { output = (output) ? `${output} ${words[i]}` : words[i]; } } } } return output; }; this.left = (text, charCount) => { let output = ""; if (!!text) { if (charCount && charCount > 0) { const endIndex = (charCount < text.length) ? charCount : text.length; output = text.slice(0, endIndex); } else { output = text; } } else { console.log(`Text must be a string.`); } return output; }; this.right = (text, charCount) => { let output = ""; if (!!text) { if (charCount && charCount > 0) { const startIndex = (charCount < text.length) ? (text.length - charCount) : 0; output = text.slice(startIndex); } else { output = text; } } else { console.log(`Text must be a string.`); } return output; }; this.concatenate = (...texts) => { let output = ""; const totalWords = texts.length; if (totalWords > 0) { const nonEmptyWords = texts.filter(arg => arg.trim() !== ""); output = nonEmptyWords.join(""); } return output; }; this.getSpecificToken = (token, position) => { let output = ""; if (!!token) { const tokenArr = token.split(","); if (tokenArr && Array.isArray(tokenArr) && tokenArr.length > 0 && position && position > 0 && position <= tokenArr.length) { output = tokenArr[position - 1]; } } return output; }; this.retrieveSubstring = (token, startPosition, endPosition) => { let output = ""; if (!!token && this.validatePositionValues(startPosition, endPosition) && (startPosition <= token.length)) { endPosition = endPosition > token.length ? token.length : endPosition; output = token.substring(startPosition - 1, endPosition); } return output; }; this.validatePositionValues = (startPosition, endPosition) => { const valid = typeof startPosition === 'number' && typeof endPosition === 'number' && startPosition > 0 && endPosition > 0 && startPosition <= endPosition; return valid; }; this.const = new constants_1.Constants(); } } exports.SentenceExpression = SentenceExpression;