You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
3.1 KiB
87 lines
3.1 KiB
function mathPolyfills() {
|
|
// Converts from degrees to radians.
|
|
Math.radians = function (degrees) {
|
|
return (degrees * Math_PI) / 180.0;
|
|
};
|
|
|
|
// Converts from radians to degrees.
|
|
Math.degrees = function (radians) {
|
|
return (radians * 180.0) / Math_PI;
|
|
};
|
|
}
|
|
|
|
function stringPolyfills() {
|
|
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
|
|
if (!String.prototype.padStart) {
|
|
String.prototype.padStart = function padStart(targetLength, padString) {
|
|
targetLength = targetLength >> 0; //truncate if number, or convert non-number to 0;
|
|
padString = String(typeof padString !== "undefined" ? padString : " ");
|
|
if (this.length >= targetLength) {
|
|
return String(this);
|
|
} else {
|
|
targetLength = targetLength - this.length;
|
|
if (targetLength > padString.length) {
|
|
padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
|
|
}
|
|
return padString.slice(0, targetLength) + String(this);
|
|
}
|
|
};
|
|
}
|
|
|
|
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd
|
|
if (!String.prototype.padEnd) {
|
|
String.prototype.padEnd = function padEnd(targetLength, padString) {
|
|
targetLength = targetLength >> 0; //floor if number or convert non-number to 0;
|
|
padString = String(typeof padString !== "undefined" ? padString : " ");
|
|
if (this.length > targetLength) {
|
|
return String(this);
|
|
} else {
|
|
targetLength = targetLength - this.length;
|
|
if (targetLength > padString.length) {
|
|
padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
|
|
}
|
|
return String(this) + padString.slice(0, targetLength);
|
|
}
|
|
};
|
|
}
|
|
}
|
|
|
|
function objectPolyfills() {
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
|
|
// @ts-ignore
|
|
if (!Object.entries) {
|
|
// @ts-ignore
|
|
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;
|
|
};
|
|
}
|
|
}
|
|
|
|
function initPolyfills() {
|
|
mathPolyfills();
|
|
stringPolyfills();
|
|
objectPolyfills();
|
|
}
|
|
|
|
function initExtensions() {
|
|
String.prototype.replaceAll = function (search, replacement) {
|
|
var target = this;
|
|
return target.split(search).join(replacement);
|
|
};
|
|
}
|
|
|
|
// Fetch polyfill
|
|
import "whatwg-fetch";
|
|
import { Math_PI } from "./builtins";
|
|
|
|
// Other polyfills
|
|
initPolyfills();
|
|
initExtensions();
|