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
@@ -1,30 +0,0 @@
// 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('padding', () => {
let stringEnd: (length: number, fill?: string) => string;
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');
});
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');
});
});
@@ -1,27 +0,0 @@
// 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;
};
}
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart (length: number, char: string = ' ') {
let result: string = String(this);
while (result.length < length) {
result = char + result;
}
return result;
};
}
@@ -1,29 +0,0 @@
// 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('TextDecoder', () => {
let origTD;
beforeEach(() => {
origTD = global.TextDecoder;
});
afterEach(() => {
global.TextDecoder = origTD;
});
it('polyfills with no exceptions (without TextDecoder)', () => {
global.TextDecoder = null;
expect(require('./textDecoder')).toBeDefined();
expect(global.TextDecoder).toBeDefined();
});
it('polyfills with no exceptions (with TextDecoder)', () => {
global.TextDecoder = require('util').TextDecoder;
expect(require('./textDecoder')).toBeDefined();
expect(global.TextDecoder).toBeDefined();
});
});
@@ -1,13 +0,0 @@
// 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.
// tslint:disable-next-line
if (typeof TextDecoder === 'undefined') {
try {
// @ts-ignore
global.TextDecoder = require('util').TextDecoder;
} catch (error) {
// noop
}
}
@@ -1,29 +0,0 @@
// 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('TextEncoder', () => {
let origTE;
beforeEach(() => {
origTE = global.TextEncoder;
});
afterEach(() => {
global.TextEncoder = origTE;
});
it('polyfills with no exceptions (without TextEncoder)', () => {
global.TextEncoder = null;
expect(require('./textEncoder')).toBeDefined();
expect(global.TextEncoder).toBeDefined();
});
it('polyfills with no exceptions (with TextEncoder)', () => {
global.TextEncoder = require('util').TextEncoder;
expect(require('./textEncoder')).toBeDefined();
expect(global.TextEncoder).toBeDefined();
});
});
@@ -1,13 +0,0 @@
// 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.
// tslint:disable-next-line
if (typeof TextEncoder === 'undefined') {
try {
// @ts-ignore For the Node.js case
global.TextEncoder = require('util').TextEncoder;
} catch (error) {
// noop
}
}
+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 {