Last active
September 20, 2023 13:34
-
-
Save jgphilpott/c4e0c275c808ae1a01386942dfc2a225 to your computer and use it in GitHub Desktop.
A collection of JavaScript String prototype updates.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty | |
# Info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String | |
# Source: https://gist.github.com/jgphilpott/c4e0c275c808ae1a01386942dfc2a225 | |
Object.defineProperty String.prototype, "lower", | |
value: -> | |
this.trim().toLowerCase() | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "upper", | |
value: -> | |
this.trim().toUpperCase() | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "sort", | |
value: -> | |
this.trim().split("").sort().join("") | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "clear", | |
value: -> | |
this.replace /.*/s, "" | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "empty", | |
value: -> | |
this.length is 0 | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "boolean", | |
value: -> | |
Boolean this.trim().length | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "number", | |
value: -> | |
Number this | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "string", | |
value: -> | |
String this | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "array", | |
value: -> | |
Array this | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "object", | |
value: -> | |
Object this | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "capitalize", | |
value: -> | |
this.trim().charAt(0).upper() + this.lower().slice(1) | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "camelize", | |
value: -> | |
this.lower().replace(/[^a-zA-ZÀ-ÖØ-öø-ÿ0-9]+(.)/g, (match, char) -> char.upper()) | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "pascalize", | |
value: -> | |
(" " + this.lower()).replace(/[^a-zA-ZÀ-ÖØ-öø-ÿ0-9]+(.)/g, (match, char) -> char.upper()) | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "snakify", | |
value: -> | |
this.trim().replace(/\W+/g, " ").split(/ |\B(?=[A-Z])/).map((word) -> word.lower()).join("_") | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "kebabify", | |
value: -> | |
this.trim().replace(/\W+/g, " ").split(/ |\B(?=[A-Z])/).map((word) -> word.lower()).join("-") | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty String.prototype, "titlize", | |
value: -> | |
this.trim().charAt(0).upper() + this.lower().replace(/[^a-zA-ZÀ-ÖØ-öø-ÿ0-9]+(.)/g, (match, char) -> " " + char.upper()).slice(1) | |
configurable: false, | |
enumerable: false, | |
writable: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty | |
// Info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String | |
// Source: https://gist.github.com/jgphilpott/c4e0c275c808ae1a01386942dfc2a225 | |
Object.defineProperty(String.prototype, "lower", { | |
value: function() { | |
return this.trim().toLowerCase(); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "upper", { | |
value: function() { | |
return this.trim().toUpperCase(); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "sort", { | |
value: function() { | |
return this.trim().split("").sort().join(""); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "clear", { | |
value: function() { | |
return this.replace(/.*/s, ""); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "empty", { | |
value: function() { | |
return this.length === 0; | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "boolean", { | |
value: function() { | |
return Boolean(this.trim().length); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "number", { | |
value: function() { | |
return Number(this); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "string", { | |
value: function() { | |
return String(this); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "array", { | |
value: function() { | |
return Array(this); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "object", { | |
value: function() { | |
return Object(this); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "capitalize", { | |
value: function() { | |
return this.trim().charAt(0).upper() + this.lower().slice(1); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "camelize", { | |
value: function() { | |
return this.lower().replace(/[^a-zA-ZÀ-ÖØ-öø-ÿ0-9]+(.)/g, function(match, char) { | |
return char.upper(); | |
}); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "pascalize", { | |
value: function() { | |
return (" " + this.lower()).replace(/[^a-zA-ZÀ-ÖØ-öø-ÿ0-9]+(.)/g, function(match, char) { | |
return char.upper(); | |
}); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "snakify", { | |
value: function() { | |
return this.trim().replace(/\W+/g, " ").split(/ |\B(?=[A-Z])/).map(function(word) { | |
return word.lower(); | |
}).join("_"); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "kebabify", { | |
value: function() { | |
return this.trim().replace(/\W+/g, " ").split(/ |\B(?=[A-Z])/).map(function(word) { | |
return word.lower(); | |
}).join("-"); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(String.prototype, "titlize", { | |
value: function() { | |
return this.trim().charAt(0).upper() + this.lower().replace(/[^a-zA-ZÀ-ÖØ-öø-ÿ0-9]+(.)/g, function(match, char) { | |
return " " + char.upper(); | |
}).slice(1); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prototyping
This gist is part of a collection of gists that extend JavaScript data prototypes such as
Boolean
,Number
,String
,Array
andObject
. See below for links to all the gists: