index.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mid = exports.rept = exports.clean = exports.n = exports.compareIgnoreCase = exports.log = exports.count = exports.zfill = exports.flip = exports.lstrip = exports.rstrip = exports.split = exports.endsWith = exports.startsWith = exports.lower = exports.upper = exports.capitalizeAll = exports.capitalize = exports.len = void 0;
/**
 * Returns the length of a String object.
 * Gets the length of the array.
 * This is a number one higher than the highest index in the array.
 * @param {string | unknown[]} data
 * @returns {number}
 */
var len = function (data) {
    return data.length;
};
exports.len = len;
/**
 * Capitalize only the _first word_ in the String.
 * @param {string} data String
 * @returns {string} Capitalized String
 */
var capitalize = function (data) {
    return exports.upper(data.charAt(0)) + data.slice(1);
};
exports.capitalize = capitalize;
/**
 * Capitalize _each word_ in the specified String.
 * @param {string} data String
 * @returns {string} Capitalized String
 */
var capitalizeAll = function (data) {
    return data
        .split(" ")
        .map(function (word) { return "" + exports.upper(word[0]) + word.slice(1); })
        .join(" ");
};
exports.capitalizeAll = capitalizeAll;
/**
 * Converts all the alphabetic characters in a String to uppercase.
 * @param {string} data String
 * @returns {string} Uppercase String
 */
var upper = function (data) {
    return data.toUpperCase();
};
exports.upper = upper;
/**
 * Converts all the alphabetic characters in a String to lowercase.
 * @param {string} data String
 * @returns {string} Lowercase String
 */
var lower = function (data) {
    return data.toLowerCase();
};
exports.lower = lower;
/**
 * Checks if the String starts with the given pattern.
 * @param {string} data String
 * @param {string} start Start pattern.
 * @returns {boolean} Returns true if found.
 */
var startsWith = function (data, start) {
    return exports.split(data, [0, exports.len(start)]) === start;
};
exports.startsWith = startsWith;
/**
 * Checks if the String ends with the given pattern.
 * @param {string} data String
 * @param {string} start Pattern
 * @returns {boolean} Returns true if found.
 */
var endsWith = function (data, end) {
    return exports.split(data, [0, exports.flip(exports.len(end))]) === end;
};
exports.endsWith = endsWith;
var split = function (data, range) {
    if (exports.len(range) === 2) {
        if (range[1] < 0) {
            return data.slice(data.length - exports.flip(range[1]));
        }
        else {
            return data.slice(range[0], range[1] !== 0 ? range[1] : Infinity);
        }
    }
    else {
        return data[range];
    }
};
exports.split = split;
/**
 * Removes the specified characters at the end of the String.
 * @param {string} data String
 * @param {string} remove Characters to remove.
 * @returns {string}
 */
var rstrip = function (data, remove) {
    return exports.endsWith(data, remove) ? data.slice(0, exports.flip(exports.len(remove))) : data;
};
exports.rstrip = rstrip;
/**
 * Removes the specified characters at the beginning of the String.
 * @param {string} data String
 * @param {string} remove Characters to remove.
 * @returns {string}
 */
var lstrip = function (data, remove) {
    return exports.startsWith(data, remove) ? data.substring(exports.len(remove)) : data;
};
exports.lstrip = lstrip;
/**
 * Converts negative numbers to positive and positive numbers to negative.
 * @param {number} data
 * @returns {number}
 */
var flip = function (data) {
    return data < 0 ? Math.abs(data) : -Math.abs(data);
};
exports.flip = flip;
/**
 * The zfill() method adds zeros (0) at the beginning of the String.
 * @param {string} data String
 * @param {string | number} many How many zeros to add.
 * @returns {string | number}
 */
var zfill = function (data, many) {
    var fill = "";
    for (var i = 0; i < many; i++) {
        fill += 0;
    }
    return fill + data;
};
exports.zfill = zfill;
/**
 * Count the number of element persent inside the array
 * This works only for permitive values like, Number, String, Boolean
 * @param {any} arr Array
 * @param {any} toCount Any element
 * @returns {number} Count the number of elements persent.
 */
var count = function (arr, toCount) {
    var count = arr.filter(function (s) { return s === toCount; });
    return exports.len(count);
};
exports.count = count;
/**
 * Prints to `stdout` with newline.
 * @param {any} data
 */
var log = function (data) {
    console.log(data);
};
exports.log = log;
/**
 * Compares two Strings with case ignore.
 * @param {string} str1 String
 * @param {string} str2 String
 * @returns {boolean}
 */
var compareIgnoreCase = function (str1, str2) {
    return exports.lower(str1) === exports.lower(str2);
};
exports.compareIgnoreCase = compareIgnoreCase;
/**
 * A handy and easy-to-read way to write long numbers.
 * String `1_000_000` change into regular number.
 * @param {string} number
 * @returns {number}
 */
var n = function (number) {
    return Number(number.replace(/_/gm, ""));
};
exports.n = n;
/**
 * Remove all whitespaces, new lines and set correct punctuation
 * @param {string} data
 * @param {boolean} multiline default false
 * @returns {string}
 */
var clean = function (data, multiline) {
    // In English, it is always an error. There should be no space between a sentence and its ending punctuation, whether that's a period, a question mark, or an exclamation mark. There should also be no space before a colon, semicolon, or comma.
    if (multiline === void 0) { multiline = false; }
    data = !multiline ? data.replace(/\s+/g, " ") : data;
    return data
        .replace(/[ ]+/g, " ")
        .replace(/\s+\./g, ".")
        .replace(/\s+\,/g, ",")
        .replace(/\s+\?/g, "?")
        .replace(/\s+\:/g, ":")
        .replace(/\s+\;/g, ";")
        .replace(/\s+\!/g, "!");
};
exports.clean = clean;
/**
 * Repeats the mentioned text or number by given number of times
 * @param {string | number} data string or number to repeat mutilple times
 * @param {string} many the number of times to repeat text
 * @returns {string | number}
 */
var rept = function (data, many) {
    var dataType = typeof data;
    var fill = "";
    for (var i = 0; i < many; i++) {
        fill += data;
    }
    return dataType === "number" ? Number(fill) : String(fill);
};
exports.rept = rept;
/**
 * Returns the characters from a text string based on the starting position and number of characters
 * @param {string} data text string that contains the characters to extract
 * @param {number} start specifies the character at which to start the extract
 * @param {number} many number of characters to extract from the start position
 * @returns {string}
 */
var mid = function (data, start, many) {
    return data.slice(start - 1, Infinity).substring(0, many);
};
exports.mid = mid;