Created
September 6, 2013 10:44
-
-
Save Witiko/6462240 to your computer and use it in GitHub Desktop.
A library adding support of various ecma5 functions to an ecma3-compliant interpreter
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
(function(global, isNaN, undefined) { | |
var arrLikeString = !!"1"[0], has, get; | |
if(!arrLikeString) { | |
has = function(arr, item, isStr) { | |
return isStr?item < arr.length: | |
item in arr; | |
}; | |
get = function(arr, item, isStr) { | |
return isStr?arr.charAt(item):arr[item]; | |
}; | |
} | |
Function.isFunction = function(f) { | |
return typeof f === "function" || | |
f instanceof Function; | |
}; | |
if(!Function.isFunction(Date.now)) | |
Date.now = function() { | |
return new Date().getTime(); | |
}; | |
if(!Function.isFunction(Array.isArray)) | |
Array.isArray = function(array) { | |
return array instanceof Array; | |
}; | |
Boolean.isBoolean = function(b) { | |
return typeof b === "boolean" || | |
b instanceof Boolean; | |
}; | |
Number.isNumber = function(n) { | |
return !isNaN(n) && | |
(typeof n === "number" || | |
n instanceof Number); | |
}; | |
String.isString = function(s) { | |
return typeof s === "string" || | |
s instanceof String; | |
}; | |
if(!Function.isFunction(Array.prototype.indexOf)) | |
Array.prototype.indexOf = function(needle, from) { | |
if(this === undefined || this === null) throw new TypeError; | |
for(var i = !from || from < 0?0:from, | |
l = this.length; i !== l; i++) | |
if(this[i] === needle) return i; return -1; | |
}; | |
if(!Function.isFunction(Array.prototype.lastIndexOf)) | |
Array.prototype.lastIndexOf = function(needle, from) { | |
if(this === undefined || this === null) throw new TypeError; | |
for(var l = this.length, | |
i = !from || from < 0?0:(from >= l?l - 1:from); i >= 0; i--) | |
if(this[i] === needle) return i; return -1; | |
}; | |
if(!Function.isFunction(Array.prototype.every)) | |
Array.prototype.every = arrLikeString?function(f, context) { | |
if(!this) return; | |
if(!Function.isFunction(f)) throw new TypeError; | |
for(var i = 0, l = this.length; i !== l; i++) | |
if((i in this && !f.call(context, this[i], i, this))) | |
return false; return true; | |
}:function(f, context) { | |
if(!this) return; | |
if(!Function.isFunction(f)) throw new TypeError; | |
for(var i = 0, l = this.length, | |
isStr = isString(this); i !== l; i++) | |
if((has(this, i, isStr) && !f.call(context, get(this, i, isStr), i, this))) | |
return false; return true; | |
}; | |
if(!Function.isFunction(Array.prototype.map)) | |
Array.prototype.map = arrLikeString?function(f, context) { | |
if(!this) return; | |
if(!Function.isFunction(f)) throw new TypeError; | |
for(var i = 0, | |
l = this.length, | |
res = new Array(l); i !== l; i++) | |
if(i in this) res[i] = f.call(context, this[i], i, this); | |
return res; | |
}:function(f, context) { | |
if(!this) return; | |
if(!Function.isFunction(f)) throw new TypeError; | |
for(var i = 0, | |
l = this.length, | |
isStr = isString(this), | |
res = new Array(l); i !== l; i++) | |
if(has(this, i, isStr)) res[i] = f.call(context, get(this, i, isStr), i, this); | |
return res; | |
}; | |
if(!Function.isFunction(Array.prototype.some)) | |
Array.prototype.some = arrLikeString?function(f, context) { | |
if(!this) return; | |
if(!Function.isFunction(f)) throw new TypeError; | |
for(var i = 0, l = this.length; i !== l; i++) | |
if((i in this && f.call(context, this[i], i, this))) | |
return true; return false; | |
}:function(f, context) { | |
if(!this) return; | |
if(!Function.isFunction(f)) throw new TypeError; | |
for(var i = 0, l = this.length, | |
isStr = isString(this); i !== l; i++) | |
if((has(this, i, isStr) && f.call(context, get(this, i, isStr), i, this))) | |
return true; return false; | |
}; | |
if(!Function.isFunction(Array.prototype.filter)) | |
Array.prototype.filter = arrLikeString?function(f, context) { | |
if(!this) return; | |
if(!Function.isFunction(f)) throw new TypeError; | |
for(var i = 0, value, | |
l = this.length, | |
res = []; i !== l; i++) | |
if((i in this && f.call(context, value = this[i], i, this))) | |
res.push(value); return res; | |
}:function(f, context) { | |
if(!this) return; | |
if(!Function.isFunction(f)) throw new TypeError; | |
for(var i = 0, value, | |
l = this.length, | |
isStr = isString(this), | |
res = []; i !== l; i++) | |
if((has(this, i, isStr) && f.call(context, value = get(this, i, isStr), i, this))) | |
res.push(value); return res; | |
}; | |
if(!Function.isFunction(Array.prototype.forEach)) | |
Array.prototype.forEach = arrLikeString?function(f, context) { | |
if(!this) return; | |
if(!Function.isFunction(f)) throw new TypeError; | |
for(var i = 0, l = this.length; i !== l; i++) | |
if(i in this) f.call(context, this[i], i, this); | |
}:function(f, context) { | |
if(!this) return; | |
if(!Function.isFunction(f)) throw new TypeError; | |
for(var i = 0, l = this.length, | |
isStr = isString(this); i !== l; i++) | |
if(has(this, i, isStr)) | |
f.call(context, get(this, i, isStr), i, this); | |
}; | |
if(!Function.isFunction(Array.prototype.reduce)) | |
Array.prototype.reduce = arrLikeString?function(f, curr) { | |
var l = this.length, noCurr = isNaN(Number(curr)), i = noCurr?1:0; | |
if(!Function.isFunction(f) || | |
(!l && noCurr)) throw new TypeError; | |
if(noCurr) curr = this[0]; | |
while(i !== l) | |
if(i in this) curr = f(curr, this[i], i++, this); | |
return curr; | |
}:function(f, curr) { | |
var l = this.length, noCurr = isNaN(Number(curr)), | |
isStr = isString(this), i = noCurr?1:0; | |
if(!Function.isFunction(f) || | |
(!l && noCurr)) throw new TypeError; | |
if(noCurr) curr = this[0]; | |
while(i !== l) | |
if(has(this, i, isStr)) curr = f(curr, get(this, i, isStr), i++, this); | |
return curr; | |
}; | |
if(!Function.isFunction(Array.prototype.reduceRight)) | |
Array.prototype.reduceRight = arrLikeString?function(f, curr) { | |
var l = this.length, noCurr = isNaN(Number(curr)), i = l - (noCurr?2:1); | |
if(!Function.isFunction(f) || | |
(!l && noCurr)) throw new TypeError; | |
if(noCurr) curr = this[l - 1]; | |
while(i !== -1) | |
if(i in this) curr = f(curr, this[i], i--, this); | |
return curr; | |
}:function(f, curr) { | |
var l = this.length, noCurr = isNaN(Number(curr)), | |
isStr = isString(this), i = l - (noCurr?2:1); | |
if(!Function.isFunction(f) || | |
(!l && noCurr)) throw new TypeError; | |
if(noCurr) curr = this[l - 1]; | |
while(i !== -1) | |
if(has(this, i, isStr)) curr = f(curr, get(this, i, isStr), i--, this); | |
return curr; | |
}; | |
if(!Function.isFunction(Function.prototype.bind)) | |
Function.prototype.bind = function(context) { | |
var boundArguments = arguments.length > 1?Array.prototype.slice.call( | |
arguments, 1 | |
):false, boundFunction = this; | |
return function() { | |
return boundFunction.apply( | |
context, boundArguments?boundArguments.concat( | |
Array.prototype.slice.call( | |
arguments, 0 | |
) | |
):arguments | |
); | |
}; | |
}; | |
if(!Function.isFunction(Object.create)) // Incomplete fallback, ignores the properties | |
Object.create = function(object/*, descriptors*/) { // descriptors object part. | |
var f = new Function; | |
f.prototype = object; | |
return new f; | |
}; | |
if(!Function.isFunction(String.prototype.trim)) | |
String.prototype.trim = function() { | |
this.replace(/^\s+|\s+$/g, ""); | |
}; | |
if(!Function.isFunction(String.prototype.trimLeft)) | |
String.prototype.trimLeft = function() { | |
this.replace(/^\s+/g, ""); | |
}; | |
if(!Function.isFunction(String.prototype.trimRight)) | |
String.prototype.trimLeft = function() { | |
this.replace(/\s+$/g, ""); | |
}; | |
if(!Function.isFunction(Date.prototype.toISOString)) | |
Date.prototype.toISOString = (function() {return function() { | |
return this.getUTCFullYear() + '-' + | |
f(this.getUTCMonth() + 1) + '-' + | |
f(this.getUTCDate()) + 'T' + | |
f(this.getUTCHours()) + ':' + | |
f(this.getUTCMinutes()) + ':' + | |
f(this.getUTCSeconds()) + 'Z'; | |
}; function f(n) { | |
return n < 10 ? '0' + n : n; | |
}})(); | |
(function() { | |
var origParse = Date.parse, numericKeys = [1, 4, 5, 6, 7, 10, 11]; | |
Date.parse = function (date) { | |
var timestamp, struct, minutesOffset = 0; | |
if((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date))) { | |
for(var i = 0, k; k = numericKeys[i]; i++) { | |
struct[k] = +struct[k] || 0; | |
} struct[2] = (+struct[2] || 1) - 1; | |
struct[3] = +struct[3] || 1; | |
if(struct[8] !== 'Z' && struct[9] !== undefined) { | |
minutesOffset = struct[10] * 60 + struct[11]; | |
if(struct[9] === '+') { | |
minutesOffset = 0 - minutesOffset; | |
} | |
} timestamp = Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]); | |
} else timestamp = origParse?origParse(date):NaN; | |
return timestamp; | |
}; | |
})(); | |
if(!Function.isFunction(Date.UTC)) | |
Date.UTC = function(y, m, d, h, min, s, ms) { | |
var date = new Date(); | |
date.setUTCFullYear(y, m, d); | |
date.setUTCHours(h, min, s, ms); | |
return date.getTime(); | |
}; | |
if(!("JSON" in global)) | |
JSON = { | |
parse: function(json) { | |
return eval("(" + json + ")"); | |
}//, stringify overly complex | |
}; | |
(function(shorthands, local) { | |
for(var obj in shorthands) { | |
(function(obj, arr) { | |
arr.forEach(function(name) { | |
if(!Function.isFunction(obj[name])) | |
obj[name] = function(context) { | |
return obj.prototype[name].call.apply( | |
obj.prototype[name], arguments); | |
}; | |
}); | |
})(global[obj], shorthands[obj]) | |
} | |
})({ | |
Array: ["concat", "join", "push", "pop", "shift", "unshift", "slice", "splice", "reverse", "sort", "indexOf", "lastIndexOf", "forEach", "map", "filter", "every", "some", "reduce", "reduceRight"], | |
String: ["charAt", "charCodeAt", "concat", "indexOf", "lastIndexOf", "localeCompare", "match", "quote", "replace", "search", "slice", "split", "substr", "substring", "toLocaleLowerCase", "toLocaleUpperCase", "toLowerCase", "toUpperCase", "trim", "trimLeft", "trimRight"] | |
}, { | |
Array: Array, | |
String: String | |
}); | |
})(this, this.isNaN); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment