Last active
December 17, 2015 01:29
-
-
Save garystorey/5528364 to your computer and use it in GitHub Desktop.
JavaScript 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
| /* STRING */ | |
| if ( typeof String.prototype.trim !== 'function' ) { | |
| String.prototype.trim = function () { | |
| return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"); | |
| }; | |
| } | |
| if (typeof String.prototype.toProperCase !== 'function' ) { | |
| String.prototype.toProperCase = function () { | |
| return this.toLowerCase().replace(/^(.)|\s(.)/g, function ($1) { return $1.toUpperCase(); }); | |
| }; | |
| } | |
| if (typeof String.prototype.pad !== 'function' ) { | |
| String.prototype.pad = function (i){ | |
| var s = this; | |
| while( s.length < i ) { s = "0" + s; } | |
| return s; | |
| } | |
| } | |
| if (typeof String.prototype.startsWith !== 'function' ) { | |
| String.prototype.startsWith = function(str){ | |
| return (this.match("^"+str)==str) | |
| } | |
| } | |
| if (typeof String.prototype.endsWith !== 'function' ) { | |
| String.prototype.endsWith = function(str) { | |
| return (this.match(str+"$")==str) | |
| } | |
| } | |
| if (typeof String.prototype.replaceAll !== 'function' ) { | |
| String.prototype.replaceAll = function (stringToFind, stringToReplace) { | |
| if (stringToFind === stringToReplace) return this; | |
| var temp = this; | |
| var index = temp.indexOf(stringToFind); | |
| while (index != -1) { | |
| temp = temp.replace(stringToFind, stringToReplace); | |
| index = temp.indexOf(stringToFind); | |
| } | |
| return temp; | |
| }; | |
| } | |
| if (typeof String.prototype.countChar !== 'function' ) { | |
| String.prototype.countChar = function(cha){ | |
| var count = 0; | |
| var chars = this.split('').forEach(function(ch){ | |
| if(cha === ch) | |
| count++; | |
| }); | |
| return count; | |
| } | |
| } | |
| /* DATE */ | |
| /* For use with MS Sharepoint REST API's */ | |
| if (typeof Date.prototype.spdate !== 'function' ) { | |
| Date.prototype.spdate = function(){ | |
| return "datetime '"+this.getFullYear()+"-"+(this.getMonth()+1).toString().pad(2)+"-"+ this.getDate().toString.pad(2)+"'"; | |
| }; | |
| } | |
| if (typeof Date.prototype.spdatetime !== 'function' ) { | |
| Date.prototype.spdatetime = function(){ | |
| var str=''; | |
| str += "datetime '"+this.getFullYear()+"-"+(this.getMonth()+1).toString().pad(2)+"-"+ this.getDate().toString.pad(2)+""; | |
| str += "T"+ this.getHours().toString().pad(2) +":" + this.getMinutes().toString().pad(2) + ":" +this.getSeconds().toString().pad(2)+"'"; | |
| return str; | |
| }; | |
| } | |
| /* ARRAY */ | |
| if (typeof Array.prototype.forEach !== 'function' ) { | |
| Array.prototype.forEach = function(fn, scope) { | |
| for (var i = 0, len = this.length; i < len; ++i) { | |
| fn.call(scope || this, this[i], i, this); | |
| } | |
| } | |
| } | |
| if (typeof Array.prototype.unique !== 'function' ) { | |
| Array.prototype.unique = function() { | |
| var a = [], l = this.length, b = 0, c = 0, d = 0, found = false; | |
| for(; b < l; b++) { | |
| found = false; d = a.length; | |
| for (; c < d; c++) { | |
| if (this[b] === a[c]) { found = true; break; } | |
| } | |
| if (!found) { a.push(this[b]); } | |
| } | |
| return a; | |
| }; | |
| } | |
| /* DOMTOKENLIST - used by classList | |
| This function allows you to add multiple classes with one call | |
| */ | |
| DOMTokenList.prototype.addmany = function(classes) { | |
| var classes = classes.split(' '), | |
| i = 0, | |
| ii = classes.length; | |
| for(i; i<ii; i++) { | |
| if(!this.contains(classes[i])) { | |
| this.add(classes[i]); | |
| } | |
| } | |
| } | |
| DOMTokenList.prototype.removemany = function(classes) { | |
| var classes = classes.split(' '), | |
| i = 0, | |
| ii = classes.length; | |
| for(i; i<ii; i++) { | |
| if(this.contains(classes[i])) { | |
| this.remove(classes[i]); | |
| } | |
| } | |
| } | |
| /* FUNCTION */ | |
| if (typeof Function.prototype.bind !== 'function') { | |
| Function.prototype.bind = function bind(context_this) { | |
| if (Object.prototype.toString.call(this) === "[object Function]") { | |
| var | |
| arguments_trimmed = Array.prototype.slice.call(arguments, 1), | |
| function_to_bind = this, | |
| function_noop = function () {} | |
| ; | |
| var function_bound = function () { | |
| var arguments_all = arguments_trimmed.concat(Array.prototype.slice.call(arguments)); | |
| if (this instanceof function_noop && context_this) { | |
| return function_to_bind.apply(this, arguments_all); | |
| } else { | |
| return function_to_bind.apply(context_this, arguments_all); | |
| } | |
| }; | |
| function_noop.prototype = this.prototype; | |
| function_bound.prototype = new function_noop(); | |
| return function_bound; | |
| } else { | |
| throw new TypeError("Function.prototype.bind - the object that you passed in is not callable."); | |
| } | |
| }; | |
| } | |
| if (typeof Function.prototype.compose !== 'function' ) { | |
| Function.prototype.compose = function(argFunction) { | |
| var invokingFunction = this; | |
| return function() { | |
| return invokingFunction.call(this,argFunction.apply(this,arguments)); | |
| }; | |
| }; | |
| } | |
| if (typeof Function.prototype.curry !== 'function' ) { | |
| Function.prototype.curry = function() { | |
| function toArray(vals) { | |
| return Array.prototype.slice.call(vals); | |
| } | |
| if (arguments.length<1) { | |
| return this; //nothing to curry with - return function | |
| } | |
| var __method = this; | |
| var args = toArray(arguments); | |
| return function() { | |
| return __method.apply(this, args.concat(toArray(arguments))); | |
| }; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment