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