Expand and organise polyfills (#276)

This commit is contained in:
Jaco Greeff
2018-12-18 15:15:10 +01:00
committed by GitHub
parent 7e35b7c365
commit 6d8e78840b
17 changed files with 119 additions and 28 deletions
-2
View File
@@ -4,8 +4,6 @@
import { Logger, Logger$Data } from './types';
import './string/polyfill/padding';
import chalk from 'chalk';
import moment from 'moment';
+22
View File
@@ -0,0 +1,22 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
describe('Array.fill', () => {
let arrayFill: any;
beforeEach(() => {
arrayFill = Array.prototype.fill;
Array.prototype.fill = null as any;
require('./fill');
});
afterEach(() => {
Array.prototype.fill = arrayFill;
});
it('uses the polyfills', () => {
expect([1, 2, 3, 4, 5, 6, 7, 8].fill(5, 3)).toEqual([1, 2, 3, 5, 5, 5, 5, 5]);
});
});
+44
View File
@@ -0,0 +1,44 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
if (!Array.prototype.fill) {
Array.prototype.fill = function fill (value: any, start: number = 0, end?: number) {
// Steps 1-2.
if (!this) {
throw new TypeError('this is null or not defined');
}
const A = Object(this);
// Steps 3-5.
const len = A.length >>> 0;
// Steps 6-7.
const relativeStart = start >> 0;
// Step 8.
let k = relativeStart < 0
? Math.max(len + relativeStart, 0)
: Math.min(relativeStart, len);
// Steps 9-10.
const relativeEnd = end === undefined
? len
: end >> 0;
// Step 11.
const final = relativeEnd < 0
? Math.max(len + relativeEnd, 0)
: Math.min(relativeEnd, len);
// Step 12.
while (k < final) {
A[k] = value;
k++;
}
// Step 13.
return A;
};
}
+10
View File
@@ -0,0 +1,10 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import './fill';
import './padEnd';
import './padStart';
import './setPrototypeOf';
import './textDecoder';
import './textEncoder';
+24
View File
@@ -0,0 +1,24 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
describe('String padEnd', () => {
let stringEnd: (length: number, fill?: string) => string;
beforeEach(() => {
stringEnd = String.prototype.padEnd;
String.prototype.padEnd = null as any;
require('./padEnd');
});
afterEach(() => {
String.prototype.padEnd = stringEnd;
});
it('does padding', () => {
expect('test'.padEnd(8, 'A')).toEqual('testAAAA');
expect('test'.padEnd(8)).toEqual('test ');
});
});
+15
View File
@@ -0,0 +1,15 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
if (!String.prototype.padEnd) {
String.prototype.padEnd = function padEnd (length: number, char: string = ' ') {
let result: string = String(this);
while (result.length < length) {
result = result + char;
}
return result;
};
}
@@ -2,28 +2,22 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
describe('padding', () => {
let stringEnd: (length: number, fill?: string) => string;
describe('String padStart', () => {
let stringStart: (length: number, fill?: string) => string;
beforeEach(() => {
stringEnd = String.prototype.padEnd;
stringStart = String.prototype.padStart;
String.prototype.padEnd = null as any;
String.prototype.padStart = null as any;
require('./padding');
require('./padStart');
});
afterEach(() => {
String.prototype.padEnd = stringEnd;
String.prototype.padStart = stringStart;
});
it('does padding', () => {
expect('test'.padEnd(8, 'A')).toEqual('testAAAA');
expect('test'.padEnd(8)).toEqual('test ');
expect('test'.padStart(8, 'A')).toEqual('AAAAtest');
expect('test'.padStart(8)).toEqual(' test');
});
@@ -2,18 +2,6 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
if (!String.prototype.padEnd) {
String.prototype.padEnd = function padEnd (length: number, char: string = ' ') {
let result: string = String(this);
while (result.length < length) {
result = result + char;
}
return result;
};
}
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart (length: number, char: string = ' ') {
let result: string = String(this);
+1 -1
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { stringToU8a } from './index';
import { stringToU8a } from '../index';
describe('stringToU8a', () => {
it('decodes to an empty string for undefined', () => {
-2
View File
@@ -2,8 +2,6 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import './polyfill/textEncoder';
let encoder: TextEncoder | undefined;
try {
+1 -1
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { u8aToString } from './index';
import { u8aToString } from '../index';
describe('u8aToString', () => {
it('decodes to an empty string for undefined', () => {
-2
View File
@@ -2,8 +2,6 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../string/polyfill/textDecoder';
let decoder: TextDecoder | undefined;
try {