From 6d8e78840b1bce18526ee3756b6a6c432bf956f7 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 18 Dec 2018 15:15:10 +0100 Subject: [PATCH] Expand and organise polyfills (#276) --- packages/util/src/logger.ts | 2 - packages/util/src/polyfill/fill.spec.ts | 22 ++++++++++ packages/util/src/polyfill/fill.ts | 44 +++++++++++++++++++ packages/util/src/polyfill/index.ts | 10 +++++ packages/util/src/polyfill/padEnd.spec.ts | 24 ++++++++++ packages/util/src/polyfill/padEnd.ts | 15 +++++++ .../padStart.spec.ts} | 10 +---- .../padding.ts => polyfill/padStart.ts} | 12 ----- .../setPrototypeOf.ts} | 0 .../{string => }/polyfill/textDecoder.spec.js | 0 .../src/{string => }/polyfill/textDecoder.ts | 0 .../{string => }/polyfill/textEncoder.spec.js | 0 .../src/{string => }/polyfill/textEncoder.ts | 0 packages/util/src/string/toU8a.spec.js | 2 +- packages/util/src/string/toU8a.ts | 2 - packages/util/src/u8a/toString.spec.js | 2 +- packages/util/src/u8a/toString.ts | 2 - 17 files changed, 119 insertions(+), 28 deletions(-) create mode 100644 packages/util/src/polyfill/fill.spec.ts create mode 100644 packages/util/src/polyfill/fill.ts create mode 100644 packages/util/src/polyfill/index.ts create mode 100644 packages/util/src/polyfill/padEnd.spec.ts create mode 100644 packages/util/src/polyfill/padEnd.ts rename packages/util/src/{string/polyfill/padding.spec.ts => polyfill/padStart.spec.ts} (63%) rename packages/util/src/{string/polyfill/padding.ts => polyfill/padStart.ts} (63%) rename packages/util/src/{polyfill.ts => polyfill/setPrototypeOf.ts} (100%) rename packages/util/src/{string => }/polyfill/textDecoder.spec.js (100%) rename packages/util/src/{string => }/polyfill/textDecoder.ts (100%) rename packages/util/src/{string => }/polyfill/textEncoder.spec.js (100%) rename packages/util/src/{string => }/polyfill/textEncoder.ts (100%) diff --git a/packages/util/src/logger.ts b/packages/util/src/logger.ts index 893ba365a..08c5a705f 100644 --- a/packages/util/src/logger.ts +++ b/packages/util/src/logger.ts @@ -4,8 +4,6 @@ import { Logger, Logger$Data } from './types'; -import './string/polyfill/padding'; - import chalk from 'chalk'; import moment from 'moment'; diff --git a/packages/util/src/polyfill/fill.spec.ts b/packages/util/src/polyfill/fill.spec.ts new file mode 100644 index 000000000..7af848b0b --- /dev/null +++ b/packages/util/src/polyfill/fill.spec.ts @@ -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]); + }); +}); diff --git a/packages/util/src/polyfill/fill.ts b/packages/util/src/polyfill/fill.ts new file mode 100644 index 000000000..08302b538 --- /dev/null +++ b/packages/util/src/polyfill/fill.ts @@ -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; + }; +} diff --git a/packages/util/src/polyfill/index.ts b/packages/util/src/polyfill/index.ts new file mode 100644 index 000000000..36fc83797 --- /dev/null +++ b/packages/util/src/polyfill/index.ts @@ -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'; diff --git a/packages/util/src/polyfill/padEnd.spec.ts b/packages/util/src/polyfill/padEnd.spec.ts new file mode 100644 index 000000000..618bff83e --- /dev/null +++ b/packages/util/src/polyfill/padEnd.spec.ts @@ -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 '); + }); +}); diff --git a/packages/util/src/polyfill/padEnd.ts b/packages/util/src/polyfill/padEnd.ts new file mode 100644 index 000000000..46f0d3ec7 --- /dev/null +++ b/packages/util/src/polyfill/padEnd.ts @@ -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; + }; +} diff --git a/packages/util/src/string/polyfill/padding.spec.ts b/packages/util/src/polyfill/padStart.spec.ts similarity index 63% rename from packages/util/src/string/polyfill/padding.spec.ts rename to packages/util/src/polyfill/padStart.spec.ts index a18126b3b..23d370687 100644 --- a/packages/util/src/string/polyfill/padding.spec.ts +++ b/packages/util/src/polyfill/padStart.spec.ts @@ -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'); }); diff --git a/packages/util/src/string/polyfill/padding.ts b/packages/util/src/polyfill/padStart.ts similarity index 63% rename from packages/util/src/string/polyfill/padding.ts rename to packages/util/src/polyfill/padStart.ts index a51240f12..987ccf1a9 100644 --- a/packages/util/src/string/polyfill/padding.ts +++ b/packages/util/src/polyfill/padStart.ts @@ -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); diff --git a/packages/util/src/polyfill.ts b/packages/util/src/polyfill/setPrototypeOf.ts similarity index 100% rename from packages/util/src/polyfill.ts rename to packages/util/src/polyfill/setPrototypeOf.ts diff --git a/packages/util/src/string/polyfill/textDecoder.spec.js b/packages/util/src/polyfill/textDecoder.spec.js similarity index 100% rename from packages/util/src/string/polyfill/textDecoder.spec.js rename to packages/util/src/polyfill/textDecoder.spec.js diff --git a/packages/util/src/string/polyfill/textDecoder.ts b/packages/util/src/polyfill/textDecoder.ts similarity index 100% rename from packages/util/src/string/polyfill/textDecoder.ts rename to packages/util/src/polyfill/textDecoder.ts diff --git a/packages/util/src/string/polyfill/textEncoder.spec.js b/packages/util/src/polyfill/textEncoder.spec.js similarity index 100% rename from packages/util/src/string/polyfill/textEncoder.spec.js rename to packages/util/src/polyfill/textEncoder.spec.js diff --git a/packages/util/src/string/polyfill/textEncoder.ts b/packages/util/src/polyfill/textEncoder.ts similarity index 100% rename from packages/util/src/string/polyfill/textEncoder.ts rename to packages/util/src/polyfill/textEncoder.ts diff --git a/packages/util/src/string/toU8a.spec.js b/packages/util/src/string/toU8a.spec.js index 12dc54a00..77d1bdb40 100644 --- a/packages/util/src/string/toU8a.spec.js +++ b/packages/util/src/string/toU8a.spec.js @@ -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', () => { diff --git a/packages/util/src/string/toU8a.ts b/packages/util/src/string/toU8a.ts index 16260ee5f..622f6fb72 100644 --- a/packages/util/src/string/toU8a.ts +++ b/packages/util/src/string/toU8a.ts @@ -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 { diff --git a/packages/util/src/u8a/toString.spec.js b/packages/util/src/u8a/toString.spec.js index f8f8e46cd..e908d601c 100644 --- a/packages/util/src/u8a/toString.spec.js +++ b/packages/util/src/u8a/toString.spec.js @@ -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', () => { diff --git a/packages/util/src/u8a/toString.ts b/packages/util/src/u8a/toString.ts index dde22082b..e20b68fa0 100644 --- a/packages/util/src/u8a/toString.ts +++ b/packages/util/src/u8a/toString.ts @@ -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 {