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
-15
View File
@@ -1,15 +0,0 @@
// 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 isFunction from '../is/function';
import { u8aFromBn } from './index';
describe('u8aFromBn', () => {
it('exists as a function', () => {
expect(
isFunction(u8aFromBn)
).toEqual(true);
});
});
-13
View File
@@ -1,13 +0,0 @@
// 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 u8aFromBn from '../bn/toU8a';
/**
* @name u8aFromBn
* @signature u8aFromBn (value?: BN): Uint8Array
* @summary Creates a Uint8Array value from a BN object.
* @alias bn/toU8a
*/
export default u8aFromBn;
-15
View File
@@ -1,15 +0,0 @@
// 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 isFunction from '../is/function';
import { u8aFromBuffer } from './index';
describe('u8aFromBuffer', () => {
it('exists as a function', () => {
expect(
isFunction(u8aFromBuffer)
).toEqual(true);
});
});
-13
View File
@@ -1,13 +0,0 @@
// 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 u8aFromBuffer from '../buffer/toU8a';
/**
* @name u8aFromBuffer
* @signature u8aFromBuffer (value?: Buffer): Uint8Array
* @summary Creates a Uint8Array value from a Buffer object.
* @alias buffer/toU8a
*/
export default u8aFromBuffer;
-15
View File
@@ -1,15 +0,0 @@
// 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 isFunction from '../is/function';
import { u8aFromHex } from './index';
describe('u8aFromHex', () => {
it('exists as a function', () => {
expect(
isFunction(u8aFromHex)
).toEqual(true);
});
});
-13
View File
@@ -1,13 +0,0 @@
// 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 u8aFromHex from '../hex/toU8a';
/**
* @name u8aFromHex
* @signature u8aFromHex (value?: string): Uint8Array
* @summary Creates a Uint8Array value from a hex string.
* @alias hex/toU8a
*/
export default u8aFromHex;
-20
View File
@@ -1,20 +0,0 @@
// 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 { u8aFromString } from './index';
describe('u8aFromString', () => {
it('encodes a string byte for byte', () => {
expect(
u8aFromString('12345678901234567890123456789012')
).toEqual(
new Uint8Array([
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34,
0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32
])
);
});
});
-26
View File
@@ -1,26 +0,0 @@
// 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.
/**
* @name u8aFromString
* @signature u8aFromString (value: string): UInt8Array
* @summary Creates a Uint8Array object from a string.
* @description
* String input values return the actual encoded `UInt8Array`.
* @example
* import { u8aFromString } from '@polkadot/util';
*
* u8aFromString('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
*/
export default function u8aFromString (value: string): Uint8Array {
const stringArray = [];
for (let index = 0; index < value.length; index++) {
stringArray.push(
value.charCodeAt(index)
);
}
return new Uint8Array(stringArray);
}
-21
View File
@@ -1,21 +0,0 @@
// 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 { u8aFromUtf8 } from './index';
describe('u8aFromUtf8', () => {
it('decodes to an empty string for undefined', () => {
expect(
u8aFromUtf8()
).toEqual(new Uint8Array([]));
});
it('encodes the string correctly', () => {
expect(
u8aFromUtf8('Привет, мир!')
).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
@@ -1,26 +0,0 @@
// 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 u8aFromUtf8
* @signature u8aFromUtf8 (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 { u8aFromUtf8 } from '@polkadot/util';
*
* u8aFromUtf8('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
*/
export default function u8aFromUtf8 (value?: string): Uint8Array {
if (!value) {
return new Uint8Array([]);
}
return encoder.encode(value);
}
+1 -13
View File
@@ -4,17 +4,11 @@
import u8aConcat from './concat';
import u8aFixLength from './fixLength';
import u8aFromBn from './fromBn';
import u8aFromBuffer from './fromBuffer';
import u8aFromHex from './fromHex';
import u8aFromUtf8 from './fromUtf8';
import u8aFromString from './fromString';
import u8aToBn from './toBn';
import u8aToBuffer from './toBuffer';
import u8aToHex from './toHex';
import u8aToString from './toString';
import u8aToU8a from './toU8a';
import u8aToUtf8 from './toUtf8';
/**
* @summary Utility methods to convert to and from `Uint8Array` objects
@@ -22,15 +16,9 @@ import u8aToUtf8 from './toUtf8';
export {
u8aConcat,
u8aFixLength,
u8aFromBn,
u8aFromBuffer,
u8aFromHex,
u8aFromString,
u8aFromUtf8,
u8aToBn,
u8aToBuffer,
u8aToHex,
u8aToString,
u8aToU8a,
u8aToUtf8
u8aToU8a
};
@@ -1,29 +0,0 @@
// 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();
});
});
@@ -1,9 +0,0 @@
// 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;
}
@@ -1,29 +0,0 @@
// 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();
});
});
@@ -1,9 +0,0 @@
// 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;
}
+2 -2
View File
@@ -20,8 +20,8 @@ describe('u8aToString', () => {
it('decodes the buffer correctly', () => {
expect(
u8aToString(
new Uint8Array([21, 23, 45, 67])
new Uint8Array([208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 44, 32, 208, 188, 208, 184, 209, 128, 33])
)
).toEqual('21,23,45,67');
).toEqual('Привет, мир!');
});
});
+8 -4
View File
@@ -2,21 +2,25 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import '../string/polyfill/textDecoder';
const decoder = new TextDecoder('utf-8');
/**
* @name u8aToString
* @signature u8aToString (value?: UInt8Array): string
* @summary Creates a string from a Uint8Array object.
* @summary Creates a utf-8 string from a Uint8Array object.
* @description
* `UInt8Array` input values return the actual decoded string. `null` or `undefined` values returns an empty string.
* `UInt8Array` input values return the actual decoded utf-8 string. `null` or `undefined` values returns an empty string.
* @example
* import { u8aToString } from '@polkadot/util';
*
* u8aToString(new Uint8Array([21,23,45,67])); // 21,23,45,67
* u8aToString(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello
*/
export default function u8aToString (value?: Uint8Array): string {
if (!value || !value.length) {
return '';
}
return value.toString();
return decoder.decode(value);
}
+2 -2
View File
@@ -7,7 +7,7 @@ import isHex from '../is/hex';
import isString from '../is/string';
import bufferToU8a from '../buffer/toU8a';
import hexToU8a from '../hex/toU8a';
import u8aFromUtf8 from './fromUtf8';
import stringToU8a from '../string/toU8a';
/**
* @name u8aToU8a
@@ -35,7 +35,7 @@ export default function u8aToU8a (value?: Array<number> | Buffer | Uint8Array |
return hexToU8a(value);
}
return u8aFromUtf8(value);
return stringToU8a(value);
}
if (Array.isArray(value)) {
-27
View File
@@ -1,27 +0,0 @@
// 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 { u8aToUtf8 } from './index';
describe('u8aToUtf8', () => {
it('decodes to an empty string for undefined', () => {
expect(
u8aToUtf8()
).toEqual('');
});
it('decodes to an empty string for empty buffer', () => {
expect(
u8aToUtf8(new Uint8Array([]))
).toEqual('');
});
it('decodes the buffer correctly', () => {
expect(
u8aToUtf8(
new Uint8Array([208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 44, 32, 208, 188, 208, 184, 209, 128, 33])
)
).toEqual('Привет, мир!');
});
});
-26
View File
@@ -1,26 +0,0 @@
// 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/textDecoder';
const decoder = new TextDecoder('utf-8');
/**
* @name u8aToUtf8
* @signature u8aToUtf8 (value?: UInt8Array): string
* @summary Creates a utf-8 string from a Uint8Array object.
* @description
* `UInt8Array` input values return the actual decoded utf-8 string. `null` or `undefined` values returns an empty string.
* @example
* import { u8aToUtf8 } from '@polkadot/util';
*
* u8aToUtf8(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello
*/
export default function u8aToUtf8 (value?: Uint8Array): string {
if (!value || !value.length) {
return '';
}
return decoder.decode(value);
}