Remove from* variants, move fromString to stringTo (#233)

* Remove from* variants, move fromString to stringTo

* Bump
This commit is contained in:
Jaco Greeff
2018-10-18 10:30:14 +02:00
committed by GitHub
parent 35579c4d70
commit 57eddba598
56 changed files with 69 additions and 378 deletions
+2
View File
@@ -5,6 +5,7 @@
import stringCamelCase from './camelCase';
import stringLowerFirst from './lowerFirst';
import stringShorten from './shorten';
import stringToU8a from './toU8a';
import stringUpperFirst from './upperFirst';
/**
@@ -14,5 +15,6 @@ export {
stringCamelCase,
stringLowerFirst,
stringShorten,
stringToU8a,
stringUpperFirst
};
@@ -0,0 +1,29 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the ISC 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();
});
});
@@ -0,0 +1,9 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// tslint:disable-next-line
if (typeof TextDecoder === 'undefined') {
// @ts-ignore
global.TextDecoder = require('util').TextDecoder;
}
@@ -0,0 +1,29 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the ISC 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();
});
});
@@ -0,0 +1,9 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// tslint:disable-next-line
if (typeof TextEncoder === 'undefined') {
// @ts-ignore
global.TextEncoder = require('util').TextEncoder;
}
+21
View File
@@ -0,0 +1,21 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { stringToU8a } from './index';
describe('stringToU8a', () => {
it('decodes to an empty string for undefined', () => {
expect(
stringToU8a()
).toEqual(new Uint8Array([]));
});
it('encodes the string correctly', () => {
expect(
stringToU8a('Привет, мир!')
).toEqual(
new Uint8Array([208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 44, 32, 208, 188, 208, 184, 209, 128, 33])
);
});
});
+26
View File
@@ -0,0 +1,26 @@
// Copyright 2017-2018 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import './polyfill/textEncoder';
const encoder = new TextEncoder();
/**
* @name stringToU8a
* @signature stringToU8a (value?: string): UInt8Array
* @summary Creates a Uint8Array object from a utf-8 string.
* @description
* String input values return the actual encoded `UInt8Array`. `null` or `undefined` values returns an empty encoded array.
* @example
* import { stringToU8a } from '@polkadot/util';
*
* stringToU8a('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
*/
export default function stringToU8a (value?: string): Uint8Array {
if (!value) {
return new Uint8Array([]);
}
return encoder.encode(value);
}