all files / commons/utils/ utils.js

95.38% Statements 62/65
85.71% Branches 12/14
100% Functions 15/15
95.31% Lines 61/64
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                          126× 126× 126×             727×                           735×             39×                                       1118×                                 134× 134× 264×   134× 134×                 11× 11× 11× 11× 109× 109× 852× 84×     768× 670× 670× 600×   670× 670×       109× 98×     11×                 11× 11× 11×       11× 11×     11×      
"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();