Skip to content

Instantly share code, notes, and snippets.

@kjvalencik
Created April 30, 2014 17:20
Show Gist options
  • Save kjvalencik/56865daa04fb48c78213 to your computer and use it in GitHub Desktop.
Save kjvalencik/56865daa04fb48c78213 to your computer and use it in GitHub Desktop.
Utilize lodash for polyfill of methods required by React
/* global _ */
// Based on https://github.com/es-shims/es5-shim
;(function () {
"use strict";
/* Provides the following polyfills
*
* Array.isArray
* Array.prototype.forEach
* Array.prototype.indexOf
* Array.prototype.some
* Date.now
* Function.prototype.bind
* Object.keys
*/
var properlyBoxesContext = function properlyBoxed (method) {
var properlyBoxes = true;
if (method) {
method.call("foo", function () {
if ("object" !== typeof arguments[2]) {
properlyBoxes = false;
}
});
}
return !!method && properlyBoxes;
};
if (!Array.isArray) {
Array.isArray = _.isArray;
}
if (!Array.prototype.forEach || !properlyBoxesContext(Array.prototype.forEach)) {
Array.prototype.forEach = function forEach(callback, thisArg) {
return _.each(this, callback, thisArg);
};
}
if (!Array.prototype.indexOf || ([0, 1].indexOf(1, 2) != -1)) {
Array.prototype.indexOf = function indexOf(array, value, fromIndex) {
return _.indexOf(this, array, value, fromIndex);
};
}
if (!Array.prototype.some || !properlyBoxesContext(Array.prototype.some)) {
Array.prototype.some = function some(collection, callback, thisArg) {
return _.some(this, collection, callback, thisArg);
};
}
if (!Date.now) {
Date.now = function now() {
return (new Date()).getTime();
};
}
if (!Function.prototype.bind) {
Function.prototype.bind = (function () {
var curried = _.curry(_.bind);
return function bind() {
return curried(this).apply(this, arguments);
};
}());
}
if (!Object.keys) {
Object.keys = _.keys;
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment