Skip to content

Instantly share code, notes, and snippets.

@blizzrdof77
Last active September 19, 2022 20:02

Revisions

  1. blizzrdof77 revised this gist Sep 19, 2022. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions String.prototypeHelpers.js
    Original file line number Diff line number Diff line change
    @@ -6,12 +6,12 @@
    */

    /* Easier way to check if a string contains a substring */
    String.prototype.contains = function(search) {
    String.prototype.contains = String.prototype.contains || function(search) {
    return (this.indexOf(search) !== -1);
    };

    /* Check if a string contains any substring within an array of passed strings */
    String.prototype.containsAny = function(arr) {
    String.prototype.containsAny = String.prototype.containsAny || function(arr) {
    for (var i = 0; i < arr.length; i++) {
    if (this.indexOf(arr[i]) > -1) {
    return true;
    @@ -20,7 +20,7 @@ String.prototype.containsAny = function(arr) {
    return false;
    };

    String.prototype.replaceAll = function(find, replacement) {
    String.prototype.replaceAll = String.prototype.replaceAll || function(find, replacement) {
    return this.replace(new RegExp(find, 'g'), replacement);
    };

    @@ -30,34 +30,34 @@ String.prototype.replaceAll = function(find, replacement) {
    * @type String
    * @return String
    */
    String.prototype.toTitleCase = function() {
    String.prototype.toTitleCase = String.prototype.toTitleCase || function() {
    return this.replace(/\w\S*/g, function(txt) {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
    };

    String.prototype.camelCaseToDashed = function() {
    String.prototype.camelCaseToDashed = String.prototype.camelCaseToDashed || function() {
    return this.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
    };

    String.prototype.toProperCase = function() {
    String.prototype.toProperCase = String.prototype.toProperCase || function() {
    return this.replace(/\w\S*/g, function(txt) {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
    };

    String.prototype.toCamelCase = function() {
    String.prototype.toCamelCase = String.prototype.toCamelCase || function() {
    return this.replaceAll('-', '_').replaceAll('_', ' ').replace(/(?:^\w|\-[A-Z]|\b\w)/g, function(letter, index) {
    return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
    }).replace(/\s+/g, '').replace('-', '');
    };

    String.prototype.toKebabCase = function(uppercase = false) {
    String.prototype.toKebabCase = String.prototype.toKebabCase || function(uppercase = false) {
    let str = this.toCamelCase().camelCaseToDashed();
    return (uppercase ? str.toUpperCase() : str);
    };

    String.prototype.toSnakeCase = function(capitalize = false) {
    String.prototype.toSnakeCase = String.prototype.toSnakeCase || function(capitalize = false) {
    let str = this.toKebabCase().replaceAll('-', '_');
    return (capitalize ? str.replaceAll('_', ' ').toTitleCase().replaceAll(' ', '_') : str);
    };
    };
  2. blizzrdof77 revised this gist Sep 19, 2022. 1 changed file with 16 additions and 10 deletions.
    26 changes: 16 additions & 10 deletions String.prototypeHelpers.js
    Original file line number Diff line number Diff line change
    @@ -6,12 +6,12 @@
    */

    /* Easier way to check if a string contains a substring */
    String.prototype.contains = String.prototype.contains || function(search) {
    String.prototype.contains = function(search) {
    return (this.indexOf(search) !== -1);
    };

    /* Check if a string contains any substring within an array of passed strings */
    String.prototype.containsAny = String.prototype.containsAny || function(arr) {
    String.prototype.containsAny = function(arr) {
    for (var i = 0; i < arr.length; i++) {
    if (this.indexOf(arr[i]) > -1) {
    return true;
    @@ -20,7 +20,7 @@ String.prototype.containsAny = String.prototype.containsAny || function(arr) {
    return false;
    };

    String.prototype.replaceAll = String.prototype.replaceAll || function(find, replacement) {
    String.prototype.replaceAll = function(find, replacement) {
    return this.replace(new RegExp(find, 'g'), replacement);
    };

    @@ -30,28 +30,34 @@ String.prototype.replaceAll = String.prototype.replaceAll || function(find, repl
    * @type String
    * @return String
    */
    String.prototype.toTitleCase = String.prototype.toTitleCase || function() {
    String.prototype.toTitleCase = function() {
    return this.replace(/\w\S*/g, function(txt) {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
    };

    String.prototype.camelCaseToDashed = String.prototype.camelCaseToDashed || function() {
    String.prototype.camelCaseToDashed = function() {
    return this.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
    };

    String.prototype.toProperCase = String.prototype.toProperCase || function() {
    String.prototype.toProperCase = function() {
    return this.replace(/\w\S*/g, function(txt) {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
    };

    String.prototype.toCamelCase = String.prototype.toCamelCase || function() {
    return this.replace(/(?:^\w|\-[A-Z]|\b\w)/g, function(letter, index) {
    String.prototype.toCamelCase = function() {
    return this.replaceAll('-', '_').replaceAll('_', ' ').replace(/(?:^\w|\-[A-Z]|\b\w)/g, function(letter, index) {
    return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
    }).replace(/\s+/g, '').replace('-', '');
    };

    String.prototype.toDashedCase = String.prototype.toDashedCase || function() {
    return this.toCamelCase().camelCaseToDashed();
    String.prototype.toKebabCase = function(uppercase = false) {
    let str = this.toCamelCase().camelCaseToDashed();
    return (uppercase ? str.toUpperCase() : str);
    };

    String.prototype.toSnakeCase = function(capitalize = false) {
    let str = this.toKebabCase().replaceAll('-', '_');
    return (capitalize ? str.replaceAll('_', ' ').toTitleCase().replaceAll(' ', '_') : str);
    };
  3. blizzrdof77 revised this gist Jul 18, 2018. 1 changed file with 20 additions and 8 deletions.
    28 changes: 20 additions & 8 deletions String.prototypeHelpers.js
    Original file line number Diff line number Diff line change
    @@ -4,11 +4,23 @@
    * @param String search
    * @return Bool
    */
    String.prototype.contains = function(search) {

    /* Easier way to check if a string contains a substring */
    String.prototype.contains = String.prototype.contains || function(search) {
    return (this.indexOf(search) !== -1);
    };

    String.prototype.replaceAll = function(find, replacement) {
    /* Check if a string contains any substring within an array of passed strings */
    String.prototype.containsAny = String.prototype.containsAny || function(arr) {
    for (var i = 0; i < arr.length; i++) {
    if (this.indexOf(arr[i]) > -1) {
    return true;
    }
    }
    return false;
    };

    String.prototype.replaceAll = String.prototype.replaceAll || function(find, replacement) {
    return this.replace(new RegExp(find, 'g'), replacement);
    };

    @@ -18,28 +30,28 @@ String.prototype.replaceAll = function(find, replacement) {
    * @type String
    * @return String
    */
    String.prototype.toTitleCase = function() {
    String.prototype.toTitleCase = String.prototype.toTitleCase || function() {
    return this.replace(/\w\S*/g, function(txt) {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
    };

    String.prototype.camelCaseToDashed = function() {
    String.prototype.camelCaseToDashed = String.prototype.camelCaseToDashed || function() {
    return this.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
    };

    String.prototype.toProperCase = function() {
    String.prototype.toProperCase = String.prototype.toProperCase || function() {
    return this.replace(/\w\S*/g, function(txt) {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
    };

    String.prototype.toCamelCase = function() {
    String.prototype.toCamelCase = String.prototype.toCamelCase || function() {
    return this.replace(/(?:^\w|\-[A-Z]|\b\w)/g, function(letter, index) {
    return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
    }).replace(/\s+/g, '').replace('-', '');
    };

    String.prototype.toDashedCase = function() {
    String.prototype.toDashedCase = String.prototype.toDashedCase || function() {
    return this.toCamelCase().camelCaseToDashed();
    };
    };
  4. blizzrdof77 revised this gist Jan 5, 2018. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion String.prototypeHelpers.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /**
    * Check if string contains a provided substring
    * Helper String Methods/Extensions to the JS String Object
    *
    * @param String search
    * @return Bool
    @@ -8,6 +8,16 @@ String.prototype.contains = function(search) {
    return (this.indexOf(search) !== -1);
    };

    String.prototype.replaceAll = function(find, replacement) {
    return this.replace(new RegExp(find, 'g'), replacement);
    };

    /**
    * Case conversion helper methods
    *
    * @type String
    * @return String
    */
    String.prototype.toTitleCase = function() {
    return this.replace(/\w\S*/g, function(txt) {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  5. blizzrdof77 created this gist Jan 4, 2018.
    35 changes: 35 additions & 0 deletions String.prototypeHelpers.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    /**
    * Check if string contains a provided substring
    *
    * @param String search
    * @return Bool
    */
    String.prototype.contains = function(search) {
    return (this.indexOf(search) !== -1);
    };

    String.prototype.toTitleCase = function() {
    return this.replace(/\w\S*/g, function(txt) {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
    };

    String.prototype.camelCaseToDashed = function() {
    return this.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
    };

    String.prototype.toProperCase = function() {
    return this.replace(/\w\S*/g, function(txt) {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
    };

    String.prototype.toCamelCase = function() {
    return this.replace(/(?:^\w|\-[A-Z]|\b\w)/g, function(letter, index) {
    return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
    }).replace(/\s+/g, '').replace('-', '');
    };

    String.prototype.toDashedCase = function() {
    return this.toCamelCase().camelCaseToDashed();
    };