From 7188f6b67a58c45e5ad96a8a5075db221e159abe Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 18 Dec 2018 09:59:02 +0100 Subject: [PATCH] Polyfills for RN support (#275) * Polyfills for RN support * Expand polyfill tests --- packages/util/src/index.ts | 2 ++ packages/util/src/logger.ts | 2 ++ packages/util/src/polyfill.ts | 12 ++++++++ .../util/src/string/polyfill/padding.spec.ts | 30 +++++++++++++++++++ packages/util/src/string/polyfill/padding.ts | 27 +++++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 packages/util/src/polyfill.ts create mode 100644 packages/util/src/string/polyfill/padding.spec.ts create mode 100644 packages/util/src/string/polyfill/padding.ts diff --git a/packages/util/src/index.ts b/packages/util/src/index.ts index ef16fcb66..93efbb63d 100644 --- a/packages/util/src/index.ts +++ b/packages/util/src/index.ts @@ -2,6 +2,8 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import './polyfill'; + /** * @summary Utility methods for this package are split into groups */ diff --git a/packages/util/src/logger.ts b/packages/util/src/logger.ts index 08c5a705f..893ba365a 100644 --- a/packages/util/src/logger.ts +++ b/packages/util/src/logger.ts @@ -4,6 +4,8 @@ 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.ts b/packages/util/src/polyfill.ts new file mode 100644 index 000000000..d95ee2ece --- /dev/null +++ b/packages/util/src/polyfill.ts @@ -0,0 +1,12 @@ +// 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. + +// React Native does not have Object.setPrototypeOf +if (!Object.setPrototypeOf) { + Object.setPrototypeOf = function (obj: any, proto: any) { + obj.__proto__ = proto; + + return obj; + }; +} diff --git a/packages/util/src/string/polyfill/padding.spec.ts b/packages/util/src/string/polyfill/padding.spec.ts new file mode 100644 index 000000000..a18126b3b --- /dev/null +++ b/packages/util/src/string/polyfill/padding.spec.ts @@ -0,0 +1,30 @@ +// 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'); + }); +}); diff --git a/packages/util/src/string/polyfill/padding.ts b/packages/util/src/string/polyfill/padding.ts new file mode 100644 index 000000000..a51240f12 --- /dev/null +++ b/packages/util/src/string/polyfill/padding.ts @@ -0,0 +1,27 @@ +// 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; + }; +}