"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var pluralize = require("pluralize");
var config_1 = require("../../configs/config");
/**
* Export utils class.
*
* @export
* @class Utils
*/
var Utils = (function () {
function Utils() {
}
/**
* Convert data type name to 'Object' naming
* @param {string} word - eg: users
* @returns {string} eg: User
*/
Utils.prototype.toTitleCase = function (word) {
var initialPosition = 0;
var secondWord = 1;
return pluralize.singular(word.charAt(initialPosition).toUpperCase() + word.substring(secondWord));
};
/**
* Convert word to it's table name
* @param {string} word = eg: User
* @return {string} eg: users
*/
Utils.prototype.toTableName = function (word) {
return this.pluralize(word.toLowerCase());
};
/**
* Convert word to comply with the column name schema.
*
* @param {string} word
* @returns {string} eg: user_id
*/
Utils.prototype.toColumnName = function (word) {
return word.replace(/(?:Id|_id)\b/g, '');
};
/**
* Pluralizes a word.
* @param {string} word - eg: user
* @return {string} eg: users
*/
Utils.prototype.pluralize = function (word) {
return pluralize.plural(word);
};
/**
* Convert plural word into singular
* @param {string} word - word to be singularized
* @returns {string} - singularized word
*/
Utils.prototype.singular = function (word) {
return pluralize.singular(word);
};
/**
* Pluralise a word that contains [] at the end
* @param {string} word - string object
* @return {string} - Example: input: User[]; output: Users
*/
Utils.prototype.pluraliseWordArray = function (word) {
var initialPosition = 0;
return word.includes('[]') ? pluralize.plural(word.substring(initialPosition, word.indexOf('[]'))) : word;
};
/**
* Convert js plain object into JSON string
* @param {{}} object - object to be converted
* @return {string} stringy version of js object
*/
Utils.prototype.convertToJSON = function (object) {
return JSON.stringify(object, undefined, '\t');
};
/**
* IndexOf implementation with ignore case
* @param {string[]} haystack - array of strings
* @param {string} needle - query string
* @return {number} key of the query inside the array
*/
Utils.prototype.indexOfIgnoreCase = function (haystack, needle) {
return haystack.findIndex(function (item) { return needle.toLowerCase() === item.toLowerCase(); });
};
/**
* Returns an array of a certain length with same object
* @param {{}} object - js plain object
* @param {int} length - length of returned array
* @return {Array.<{}>} filled array
*/
Utils.prototype.fillArray = function (object, length) {
var start = 0;
return new Array(length).fill(object, start, length);
};
/**
* Formats the line for spec.
*
* @param {string} initialIndentation - holds the initial indentation
* @param {number} tabs - holds how many tabs should prepend to the line
* @param {string} message - holds the actual content of the line
* @return {string} - returns the formatted line
*/
Utils.prototype.formatLine = function (initialIndentation, tabs, message) {
var line = "" + initialIndentation;
for (var i = 0; i < tabs; i++) {
line += "" + config_1.default.DEFAULT_INDENTATION;
}
line += "" + message + config_1.default.END_OF_LINE;
return line;
};
/**
* Find the common edit distance between two strings.
*
* @param {string} needle string which will be matched
* @param {string} haystack string to be matched against
* @returns {number} common edit distance between strings
*/
Utils.prototype.editDistance = function (needle, haystack) {
needle = needle.toLowerCase();
haystack = haystack.toLowerCase();
var costs = new Array();
for (var i = 0; i <= needle.length; i++) {
var lastValue = i;
for (var j = 0; j <= haystack.length; j++) {
if (i === 0) {
costs[j] = j;
}
else {
if (j > 0) {
var newValue = costs[j - 1];
if (needle.charAt(i - 1) !== haystack.charAt(j - 1)) {
newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
}
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0) {
costs[haystack.length] = lastValue;
}
}
return costs[haystack.length];
};
/**
* Finds the similarity between two strings
*
* @param {string} needle
* @param {string} haystack
* @returns {number} percentage of similarity as float
*/
Utils.prototype.similarity = function (needle, haystack) {
var longer = needle;
var shorter = haystack;
Iif (needle.length < haystack.length) {
longer = haystack;
shorter = needle;
}
var longerLength = longer.length;
Iif (longerLength === 0) {
return 1;
}
return (longerLength - this.editDistance(longer, shorter)) / parseFloat(longerLength.toFixed(2));
};
return Utils;
}());
exports.Utils = Utils;
exports.default = new Utils();
|