mirror of
https://github.com/discourse/discourse.git
synced 2024-12-14 12:53:46 +08:00
115 lines
3.3 KiB
JavaScript
115 lines
3.3 KiB
JavaScript
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
|
|
if (!Object.entries) {
|
|
Object.entries = function( obj ){
|
|
var ownProps = Object.keys( obj ),
|
|
i = ownProps.length,
|
|
resArray = new Array(i); // preallocate the Array
|
|
while (i--)
|
|
resArray[i] = [ownProps[i], obj[ownProps[i]]];
|
|
|
|
return resArray;
|
|
};
|
|
}
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
|
|
if (typeof Object.assign !== 'function') {
|
|
// Must be writable: true, enumerable: false, configurable: true
|
|
Object.defineProperty(Object, "assign", {
|
|
value: function assign(target) { // .length of function is 2
|
|
'use strict';
|
|
if (target == null) { // TypeError if undefined or null
|
|
throw new TypeError('Cannot convert undefined or null to object');
|
|
}
|
|
|
|
var to = Object(target);
|
|
|
|
for (var index = 1; index < arguments.length; index++) {
|
|
var nextSource = arguments[index];
|
|
|
|
if (nextSource != null) { // Skip over if undefined or null
|
|
for (var nextKey in nextSource) {
|
|
// Avoid bugs when hasOwnProperty is shadowed
|
|
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
|
|
to[nextKey] = nextSource[nextKey];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return to;
|
|
},
|
|
writable: true,
|
|
configurable: true
|
|
});
|
|
}
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Polyfill
|
|
/* eslint-disable */
|
|
if (!Array.prototype.includes) {
|
|
Object.defineProperty(Array.prototype, 'includes', {
|
|
value: function(searchElement, fromIndex) {
|
|
|
|
if (this == null) {
|
|
throw new TypeError('"this" is null or not defined');
|
|
}
|
|
|
|
// 1. Let O be ? ToObject(this value).
|
|
var o = Object(this);
|
|
|
|
// 2. Let len be ? ToLength(? Get(O, "length")).
|
|
var len = o.length >>> 0;
|
|
|
|
// 3. If len is 0, return false.
|
|
if (len === 0) {
|
|
return false;
|
|
}
|
|
|
|
// 4. Let n be ? ToInteger(fromIndex).
|
|
// (If fromIndex is undefined, this step produces the value 0.)
|
|
var n = fromIndex | 0;
|
|
|
|
// 5. If n ≥ 0, then
|
|
// a. Let k be n.
|
|
// 6. Else n < 0,
|
|
// a. Let k be len + n.
|
|
// b. If k < 0, let k be 0.
|
|
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
|
|
|
|
function sameValueZero(x, y) {
|
|
return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
|
|
}
|
|
|
|
// 7. Repeat, while k < len
|
|
while (k < len) {
|
|
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
|
|
// b. If SameValueZero(searchElement, elementK) is true, return true.
|
|
if (sameValueZero(o[k], searchElement)) {
|
|
return true;
|
|
}
|
|
// c. Increase k by 1.
|
|
k++;
|
|
}
|
|
|
|
// 8. Return false
|
|
return false;
|
|
}
|
|
});
|
|
}
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill
|
|
if (!String.prototype.includes) {
|
|
Object.defineProperty(String.prototype, 'includes', {
|
|
value: function(search, start) {
|
|
if (typeof start !== 'number') {
|
|
start = 0
|
|
}
|
|
|
|
if (start + search.length > this.length) {
|
|
return false
|
|
} else {
|
|
return this.indexOf(search, start) !== -1
|
|
}
|
|
}
|
|
})
|
|
}
|
|
/* eslint-enable */
|