Skip to content

Instantly share code, notes, and snippets.

@hipi
Last active May 5, 2018 00:04
Show Gist options
  • Save hipi/1403d7f356a4233f2dee000f9aad6d34 to your computer and use it in GitHub Desktop.
Save hipi/1403d7f356a4233f2dee000f9aad6d34 to your computer and use it in GitHub Desktop.
Object.assign, Array.includes polyfills
// Object.assign, Array.includes polyfills
"function" != typeof Object.assign && Object.defineProperty(Object, "assign", {
value: function(t, e) {
"use strict";
if (null == t) throw new TypeError("Cannot convert undefined or null to object");
for (var r = Object(t), n = 1; n < arguments.length; n++) {
var o = arguments[n];
if (null != o)
for (var i in o) Object.prototype.hasOwnProperty.call(o, i) && (r[i] = o[i])
}
return r
},
writable: !0,
configurable: !0
}), Array.prototype.includes || Object.defineProperty(Array.prototype, "includes", {
value: function(t, e) {
if (null == this) throw new TypeError('"this" is null or not defined');
var r = Object(this),
n = r.length >>> 0;
if (0 === n) return !1;
for (var o, i, s = 0 | e, u = Math.max(s >= 0 ? s : n - Math.abs(s), 0); u < n;) {
if ((o = r[u]) === (i = t) || "number" == typeof o && "number" == typeof i && isNaN(o) && isNaN(i)) return !0;
u++
}
return !1
}
}),
function(t, e) {
"object" == typeof exports && "undefined" != typeof module ? module.exports = e() : "function" == typeof define && define.amd ? define(e) : t.ES6Promise = e()
}(this, function() {
"use strict";
function t(t) {
return "function" == typeof t
}
function e() {
var t = setTimeout;
return function() {
return t(r, 1)
}
}
function r() {
for (var t = 0; t < w; t += 2) {
(0, M[t])(M[t + 1]), M[t] = void 0, M[t + 1] = void 0
}
w = 0
}
function n(t, e) {
var r = arguments,
n = this,
o = new this.constructor(i);
void 0 === o[C] && y(o);
var s = n._state;
return s ? function() {
var t = r[s - 1];
j(function() {
return d(s, o, t, n._result)
})
}() : h(n, o, t, e), o
}
function o(t) {
if (t && "object" == typeof t && t.constructor === this) return t;
var e = new this(i);
return c(e, t), e
}
function i() {}
function s(t) {
try {
return t.then
} catch (t) {
return q.error = t, q
}
}
function u(e, r, i) {
r.constructor === e.constructor && i === n && r.constructor.resolve === o ? function(t, e) {
e._state === Y ? f(t, e._result) : e._state === k ? l(t, e._result) : h(e, void 0, function(e) {
return c(t, e)
}, function(e) {
return l(t, e)
})
}(e, r) : i === q ? (l(e, q.error), q.error = null) : void 0 === i ? f(e, r) : t(i) ? function(t, e, r) {
j(function(t) {
var n = !1,
o = function(t, e, r, n) {
try {
t.call(e, r, n)
} catch (t) {
return t
}
}(r, e, function(r) {
n || (n = !0, e !== r ? c(t, r) : f(t, r))
}, function(e) {
n || (n = !0, l(t, e))
}, t._label);
!n && o && (n = !0, l(t, o))
}, t)
}(e, r, i) : f(e, r)
}
function c(t, e) {
t === e ? l(t, new TypeError("You cannot resolve a promise with itself")) : function(t) {
var e = typeof t;
return null !== t && ("object" === e || "function" === e)
}(e) ? u(t, e, s(e)) : f(t, e)
}
function a(t) {
t._onerror && t._onerror(t._result), p(t)
}
function f(t, e) {
t._state === N && (t._result = e, t._state = Y, 0 !== t._subscribers.length && j(p, t))
}
function l(t, e) {
t._state === N && (t._state = k, t._result = e, j(a, t))
}
function h(t, e, r, n) {
var o = t._subscribers,
i = o.length;
t._onerror = null, o[i] = e, o[i + Y] = r, o[i + k] = n, 0 === i && t._state && j(p, t)
}
function p(t) {
var e = t._subscribers,
r = t._state;
if (0 !== e.length) {
for (var n = void 0, o = void 0, i = t._result, s = 0; s < e.length; s += 3) n = e[s], o = e[s + r], n ? d(r, n, o, i) : o(i);
t._subscribers.length = 0
}
}
function v() {
this.error = null
}
function d(e, r, n, o) {
var i = t(n),
s = void 0,
u = void 0,
a = void 0,
h = void 0;
if (i) {
if ((s = function(t, e) {
try {
return t(e)
} catch (t) {
return F.error = t, F
}
}(n, o)) === F ? (h = !0, u = s.error, s.error = null) : a = !0, r === s) return void l(r, new TypeError("A promises callback cannot return that same promise."))
} else s = o, a = !0;
r._state !== N || (i && a ? c(r, s) : h ? l(r, u) : e === Y ? f(r, s) : e === k && l(r, s))
}
function y(t) {
t[C] = D++, t._state = void 0, t._result = void 0, t._subscribers = []
}
function _(t, e) {
this._instanceConstructor = t, this.promise = new t(i), this.promise[C] || y(this.promise), m(e) ? (this.length = e.length, this._remaining = e.length, this._result = new Array(this.length), 0 === this.length ? f(this.promise, this._result) : (this.length = this.length || 0, this._enumerate(e), 0 === this._remaining && f(this.promise, this._result))) : l(this.promise, new Error("Array Methods must be provided an Array"))
}
function b(t) {
this[C] = D++, this._result = this._state = void 0, this._subscribers = [], i !== t && ("function" != typeof t && function() {
throw new TypeError("You must pass a resolver function as the first argument to the promise constructor")
}(), this instanceof b ? function(t, e) {
try {
e(function(e) {
c(t, e)
}, function(e) {
l(t, e)
})
} catch (e) {
l(t, e)
}
}(this, t) : function() {
throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.")
}())
}
var m = Array.isArray ? Array.isArray : function(t) {
return "[object Array]" === Object.prototype.toString.call(t)
},
w = 0,
g = void 0,
A = void 0,
j = function(t, e) {
M[w] = t, M[w + 1] = e, 2 === (w += 2) && (A ? A(r) : x())
},
O = "undefined" != typeof window ? window : void 0,
E = O || {},
S = E.MutationObserver || E.WebKitMutationObserver,
P = "undefined" == typeof self && "undefined" != typeof process && "[object process]" === {}.toString.call(process),
T = "undefined" != typeof Uint8ClampedArray && "undefined" != typeof importScripts && "undefined" != typeof MessageChannel,
M = new Array(1e3),
x = void 0;
x = P ? function() {
return process.nextTick(r)
} : S ? function() {
var t = 0,
e = new S(r),
n = document.createTextNode("");
return e.observe(n, {
characterData: !0
}),
function() {
n.data = t = ++t % 2
}
}() : T ? function() {
var t = new MessageChannel;
return t.port1.onmessage = r,
function() {
return t.port2.postMessage(0)
}
}() : void 0 === O && "function" == typeof require ? function() {
try {
var t = require("vertx");
return void 0 !== (g = t.runOnLoop || t.runOnContext) ? function() {
g(r)
} : e()
} catch (t) {
return e()
}
}() : e();
var C = Math.random().toString(36).substring(16),
N = void 0,
Y = 1,
k = 2,
q = new v,
F = new v,
D = 0;
return _.prototype._enumerate = function(t) {
for (var e = 0; this._state === N && e < t.length; e++) this._eachEntry(t[e], e)
}, _.prototype._eachEntry = function(t, e) {
var r = this._instanceConstructor,
c = r.resolve;
if (c === o) {
var a = s(t);
if (a === n && t._state !== N) this._settledAt(t._state, e, t._result);
else if ("function" != typeof a) this._remaining--, this._result[e] = t;
else if (r === b) {
var f = new r(i);
u(f, t, a), this._willSettleAt(f, e)
} else this._willSettleAt(new r(function(e) {
return e(t)
}), e)
} else this._willSettleAt(c(t), e)
}, _.prototype._settledAt = function(t, e, r) {
var n = this.promise;
n._state === N && (this._remaining--, t === k ? l(n, r) : this._result[e] = r), 0 === this._remaining && f(n, this._result)
}, _.prototype._willSettleAt = function(t, e) {
var r = this;
h(t, void 0, function(t) {
return r._settledAt(Y, e, t)
}, function(t) {
return r._settledAt(k, e, t)
})
}, b.all = function(t) {
return new _(this, t).promise
}, b.race = function(t) {
var e = this;
return new e(m(t) ? function(r, n) {
for (var o = t.length, i = 0; i < o; i++) e.resolve(t[i]).then(r, n)
} : function(t, e) {
return e(new TypeError("You must pass an array to race."))
})
}, b.resolve = o, b.reject = function(t) {
var e = new this(i);
return l(e, t), e
}, b._setScheduler = function(t) {
A = t
}, b._setAsap = function(t) {
j = t
}, b._asap = j, b.prototype = {
constructor: b,
then: n,
catch: function(t) {
return this.then(null, t)
}
}, b.polyfill = function() {
var t = void 0;
if ("undefined" != typeof global) t = global;
else if ("undefined" != typeof self) t = self;
else try {
t = Function("return this")()
} catch (t) {
throw new Error("polyfill failed because global object is unavailable in this environment")
}
var e = t.Promise;
if (e) {
var r = null;
try {
r = Object.prototype.toString.call(e.resolve())
} catch (t) {}
if ("[object Promise]" === r && !e.cast) return
}
t.Promise = b
}, b.Promise = b, b.polyfill(), b
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment