Created
April 4, 2012 16:57
-
-
Save nicerobot/2303834 to your computer and use it in GitHub Desktop.
JavaScript Object and String enhancements. Each usable standalone.
This file contains 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
if (!Object.prototype.sizeof) { | |
Object.defineProperty(Object.prototype, 'sizeof', { | |
value: function() { | |
var counter = 0; | |
for (index in this) counter++; | |
return counter; | |
}, | |
enumerable: false | |
}); | |
} |
This file contains 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
if (!String.prototype.htmlEntities) { | |
Object.defineProperty(String.prototype, 'htmlEntities', { | |
value: function() { | |
return String(this).replace(/&/g, '&').replace(/>/g, '>').replace(/"/g, '"'); | |
}, | |
enumerable: false | |
}); | |
} |
This file contains 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
if (!String.prototype.isHex) { | |
Object.defineProperty(String.prototype, 'isHex', { | |
value: function() { | |
return this.substring(0, 1) == '#' && (this.length == 4 || this.length == 7) && /^[0-9a-fA-F]+$/.test(this.slice(1)); | |
}, | |
enumerable: false | |
}); | |
} |
This file contains 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
if (!String.prototype.pxToInt) { | |
Object.defineProperty(String.prototype, 'pxToInt', { | |
value: function() { | |
return parseInt(this.split('px')[0]); | |
}, | |
enumerable: false | |
}); | |
} |
This file contains 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
if (!String.prototype.reverse) { | |
Object.defineProperty(String.prototype, 'reverse', { | |
value: function() { | |
return this.split('').reverse().join(''); | |
}, | |
enumerable: false | |
}); | |
} |
This file contains 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
if (!String.prototype.stripNonAlpha) { | |
Object.defineProperty(String.prototype, 'stripNonAlpha', { | |
value: function() { | |
return this.replace(/[^A-Za-z ]+/g, ""); | |
}, | |
enumerable: false | |
}); | |
} |
This file contains 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
if (!String.prototype.trim) { | |
Object.defineProperty(String.prototype, 'trim', { | |
value: function() { | |
return this.replace(/^\s*/, "").replace(/\s*$/, ""); | |
}, | |
enumerable: false | |
}); | |
} |
This file contains 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
if (!String.prototype.wordCount) { | |
Object.defineProperty(String.prototype, 'wordCount', { | |
value: function() { | |
return this.split(' ').length; | |
}, | |
enumerable: false | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment