all files / dist/ index.js

82.54% Statements 52/63
92.59% Branches 25/27
83.33% Functions 5/6
82.54% Lines 52/63
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                                                                                                                           
"use strict";
// ! index.js
// ! version : 1.0.0
// ! authors : Mukesh Soni
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Remove extra whitespace and special characters from string
 * Convert string according to selected method
 */
/**
 * Convert string into lower case format
 * @param str
 */
function lowerCase(str) {
    let formattedString = '';
    if (typeof str !== 'undefined' && str !== null && str.trim() !== '') {
        formattedString = str.trim();
        formattedString = formattedString.replace(/[^a-zA-Z0-9 ]/g, '');
        formattedString = formattedString.replace(/  +/g, ' ');
        formattedString = formattedString.toLowerCase();
    }
    return (formattedString);
}
exports.lowerCase = lowerCase;
/**
 * Convert string into upper case format
 * @param str
 */
function upperCase(str) {
    let formattedString = '';
    if (typeof str !== 'undefined' && str !== null && str.trim() !== '') {
        formattedString = str.trim();
        formattedString = formattedString.replace(/[^a-zA-Z0-9 ]/g, '');
        formattedString = formattedString.replace(/  +/g, ' ');
        formattedString = formattedString.toUpperCase();
    }
    return (formattedString);
}
exports.upperCase = upperCase;
/**
 * format string and relace white space with seprator
 * @param str
 * @param seprator
 */
function convertString(str, seprator) {
    let formattedString = '';
    if (typeof str !== 'undefined' && str !== null && str.trim() !== '') {
        formattedString = str.trim();
        formattedString = formattedString.replace(/[^a-zA-Z0-9 ]/g, '');
        formattedString = formattedString.replace(/  +/g, ' ');
        formattedString = formattedString.split(' ').join(seprator);
    }
    return (formattedString);
}
exports.convertString = convertString;
/**
 * format string into seo friendly url slug string
 * @param str
 */
function urlSlug(str) {
    let formattedString = '';
    if (typeof str !== 'undefined' && str !== null && str.trim() !== '') {
        formattedString = str.trim();
        formattedString = formattedString.replace(/[^a-zA-Z0-9 ]/g, '');
        formattedString = formattedString.replace(/  +/g, ' ');
        formattedString = formattedString.replace(/\s+/g, '-');
        formattedString = formattedString.toLowerCase();
    }
    return (formattedString);
}
exports.urlSlug = urlSlug;
/**
 * format string info unique seo friendly url slug string
 * update unix timestamp value at end of string
 * @param str
 */
function urlSlugUnqiue(str) {
    let formattedString = '';
    if (typeof str !== 'undefined' && str !== null && str.trim() !== '') {
        const timestamp = Math.round((new Date()).getTime());
        formattedString = str.trim();
        formattedString = formattedString.replace(/[^a-zA-Z0-9 ]/g, '');
        formattedString = formattedString.replace(/  +/g, ' ');
        formattedString = formattedString.replace(/\s+/g, '-');
        formattedString = formattedString.toLowerCase();
        formattedString = formattedString + '-' + timestamp;
    }
    return (formattedString);
}
exports.urlSlugUnqiue = urlSlugUnqiue;
/**
 * Generate random string
 * @param length
 * Default length for string is 8
 */
function randomString(length = 8, allowSpecialCharacters = true) {
    let charset;
    if (allowSpecialCharacters) {
        let mixCharSet = '';
        mixCharSet += 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        mixCharSet += '!#$%&()*+,-./:;<=>?@[\]^_{|}~';
        charset = mixCharSet;
    }
    else {
        charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    }
    let randomStringValue = '';
    for (let i = 0, n = charset.length; i < length; ++i) {
        randomStringValue += charset.charAt(Math.floor(Math.random() * n));
    }
    return (randomStringValue);
}
exports.randomString = randomString;