Polyfills for RN support (#275)

* Polyfills for RN support

* Expand polyfill tests
This commit is contained in:
Jaco Greeff
2018-12-18 09:59:02 +01:00
committed by GitHub
parent f8646f1006
commit 7188f6b67a
5 changed files with 73 additions and 0 deletions
@@ -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');
});
});
@@ -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;
};
}