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
+1 -1
View File
@@ -9,5 +9,5 @@
"packages": [
"packages/*"
],
"version": "0.31.7"
"version": "0.32.0"
}
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/db",
"version": "0.31.7",
"version": "0.32.0",
"description": "Implementation of a basic sync in-memory and on-disk databases with overlays",
"main": "index.js",
"keywords": [
@@ -29,8 +29,8 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/db#readme",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/trie-hash": "^0.31.7",
"@polkadot/util": "^0.31.7",
"@polkadot/trie-hash": "^0.32.0",
"@polkadot/util": "^0.32.0",
"@types/mkdirp": "^0.5.2",
"@types/snappy": "^6.0.0",
"lru_map": "^0.3.3",
+1 -1
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import toU8a from '@polkadot/util/u8a/fromString';
import toU8a from '@polkadot/util/u8a/toU8a';
import LruDb from './LruDb';
import MemoryDb from './MemoryDb';
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import toU8a from '@polkadot/util/u8a/fromString';
import toU8a from '@polkadot/util/string/toU8a';
import MemoryDb from './MemoryDb';
import TransactionDb from './TransactionDb';
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/keyring",
"version": "0.31.7",
"version": "0.32.0",
"description": "Keyring management",
"main": "index.js",
"engines": {
@@ -31,8 +31,8 @@
"homepage": "https://github.com/polkadot-js/client/tree/master/packages/keyring#readme",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/util": "^0.31.7",
"@polkadot/util-crypto": "^0.31.7",
"@polkadot/util": "^0.32.0",
"@polkadot/util-crypto": "^0.32.0",
"@types/bs58": "^3.0.30",
"bs58": "^4.0.1"
}
+1 -1
View File
@@ -3,7 +3,7 @@
// of the ISC license. See the LICENSE file for details.
import hexToU8a from '@polkadot/util/hex/toU8a';
import u8aFromString from '@polkadot/util/u8a/fromString';
import u8aFromString from '@polkadot/util/string/toU8a';
import Keyring from './index';
import setPrefix from './address/setPrefix';
+2 -2
View File
@@ -2,9 +2,9 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import stringToU8a from '@polkadot/util/string/toU8a';
import u8aConcat from '@polkadot/util/u8a/concat';
import u8aFixLength from '@polkadot/util/u8a/fixLength';
import u8aFromString from '@polkadot/util/u8a/fromString';
import assert from '@polkadot/util/assert';
import naclDecrypt from '@polkadot/util-crypto/nacl/decrypt';
import naclFromSeed from '@polkadot/util-crypto/nacl/keypair/fromSeed';
@@ -24,7 +24,7 @@ export default function decode (passphrase?: string, _encrypted?: Uint8Array) {
? (naclDecrypt(
encrypted.subarray(24),
encrypted.subarray(0, 24),
u8aFixLength(u8aFromString(passphrase), 256, true)) as Uint8Array)
u8aFixLength(stringToU8a(passphrase), 256, true)) as Uint8Array)
: encrypted;
assert(encoded, `Unable to unencrypt using the supplied passphrase`);
+2 -2
View File
@@ -2,9 +2,9 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import stringToU8a from '@polkadot/util/string/toU8a';
import u8aConcat from '@polkadot/util/u8a/concat';
import u8aFixLength from '@polkadot/util/u8a/fixLength';
import u8aFromString from '@polkadot/util/u8a/fromString';
import naclEncrypt from '@polkadot/util-crypto/nacl/encrypt';
import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults';
@@ -21,7 +21,7 @@ export default function encode (secretKey: Uint8Array, passphrase?: string): Uin
return encoded;
}
const { encrypted, nonce } = naclEncrypt(encoded, u8aFixLength(u8aFromString(passphrase), 256, true));
const { encrypted, nonce } = naclEncrypt(encoded, u8aFixLength(stringToU8a(passphrase), 256, true));
return u8aConcat(nonce, encrypted);
}
+2 -2
View File
@@ -4,12 +4,12 @@
import { KeyringInstance } from './types';
import u8aFromString from '@polkadot/util/u8a/fromString';
import stringToU8a from '@polkadot/util/string/toU8a';
import Keyring from './index';
function padSeed (seed: string): Uint8Array {
return u8aFromString(seed.padEnd(32, ' '));
return stringToU8a(seed.padEnd(32, ' '));
}
const SEEDS: { [index: string ]: Uint8Array } = {
+6 -6
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/trie-db",
"version": "0.31.7",
"version": "0.32.0",
"description": "This is an implementation of the modified merkle patricia tree as speficed in Ethereum's yellow paper, adpated for Polkadot",
"main": "index.js",
"keywords": [
@@ -29,13 +29,13 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-db#readme",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/trie-hash": "^0.31.7",
"@polkadot/util": "^0.31.7",
"@polkadot/util-crypto": "^0.31.7",
"@polkadot/util-rlp": "^0.31.7"
"@polkadot/trie-hash": "^0.32.0",
"@polkadot/util": "^0.32.0",
"@polkadot/util-crypto": "^0.32.0",
"@polkadot/util-rlp": "^0.32.0"
},
"devDependencies": {
"@polkadot/db": "^0.31.7",
"@polkadot/db": "^0.32.0",
"ethereumjs-testing": "1.0.4"
}
}
+4 -4
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/trie-hash",
"version": "0.31.7",
"version": "0.32.0",
"description": "Creation of triehash values",
"main": "index.js",
"keywords": [
@@ -29,8 +29,8 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-hash#readme",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/util": "^0.31.7",
"@polkadot/util-crypto": "^0.31.7",
"@polkadot/util-rlp": "^0.31.7"
"@polkadot/util": "^0.32.0",
"@polkadot/util-crypto": "^0.32.0",
"@polkadot/util-rlp": "^0.32.0"
}
}
+1 -1
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import u8aFromString from '@polkadot/util/u8a/fromString';
import u8aFromString from '@polkadot/util/string/toU8a';
import hexToU8a from '@polkadot/util/hex/toU8a';
import { trieRoot } from './index';
+1 -1
View File
@@ -3,7 +3,7 @@
// of the ISC license. See the LICENSE file for details.
import hexToU8a from '@polkadot/util/hex/toU8a';
import u8aFromString from '@polkadot/util/u8a/fromString';
import u8aFromString from '@polkadot/util/string/toU8a';
import { trieRootOrdered } from './index';
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import u8aFromString from '@polkadot/util/u8a/fromString';
import u8aFromString from '@polkadot/util/string/toU8a';
import asNibbles from './asNibbles';
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import u8aFromString from '@polkadot/util/u8a/fromString';
import stringToU8a from '@polkadot/util/string/toU8a';
import fromNibbles from './fromNibbles';
@@ -13,7 +13,7 @@ describe('fromNibbles', () => {
new Uint8Array([4, 1])
)
).toEqual(
u8aFromString('A')
stringToU8a('A')
);
});
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util-crypto",
"version": "0.31.7",
"version": "0.32.0",
"description": "A collection of useful crypto utilities for @polkadot",
"main": "index.js",
"keywords": [
@@ -29,7 +29,7 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/util": "^0.31.7",
"@polkadot/util": "^0.32.0",
"@types/bip39": "^2.4.0",
"bip39": "^2.5.0",
"blakejs": "^1.1.0",
@@ -3,7 +3,7 @@
// of the ISC license. See the LICENSE file for details.
import hexToU8a from '@polkadot/util/hex/toU8a';
import u8aFromString from '@polkadot/util/u8a/fromString';
import stringToU8a from '@polkadot/util/string/toU8a';
import { keccakAsU8a } from './index';
@@ -30,7 +30,7 @@ describe('keccakAsU8a', () => {
it('returns an hex representation (Uint8Array)', () => {
expect(
keccakAsU8a(
u8aFromString(input)
stringToU8a(input)
)
).toEqual(output);
});
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import u8aFromString from '@polkadot/util/u8a/fromString';
import u8aFromString from '@polkadot/util/string/toU8a';
import { naclKeypairFromSeed } from '../index';
@@ -4,7 +4,7 @@
import { KeypairType } from '../../types';
import u8aFromUtf8 from '@polkadot/util/u8a/fromUtf8';
import stringToU8a from '@polkadot/util/string/toU8a';
import blake2AsU8a from '../../blake2/asU8a';
import fromSeed from './fromSeed';
@@ -23,7 +23,7 @@ import fromSeed from './fromSeed';
export default function naclKeypairFromString (value: string): KeypairType {
return fromSeed(
blake2AsU8a(
u8aFromUtf8(value),
stringToU8a(value),
256
)
);
+1 -1
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import u8aFromString from '@polkadot/util/u8a/fromString';
import u8aFromString from '@polkadot/util/string/toU8a';
import { naclSign, naclKeypairFromSeed } from './index';
+1 -1
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import u8aFromString from '@polkadot/util/u8a/fromString';
import u8aFromString from '@polkadot/util/string/toU8a';
import { naclVerify, naclKeypairFromSeed } from './index';
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util-rlp",
"version": "0.31.7",
"version": "0.32.0",
"description": "RLP encoding and decoding",
"main": "index.js",
"keywords": [
@@ -31,7 +31,7 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-hash#readme",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/util": "^0.31.7"
"@polkadot/util": "^0.32.0"
},
"devDependencies": {
"ethereumjs-testing": "1.0.4"
+2 -2
View File
@@ -15,7 +15,7 @@ import isString from '@polkadot/util/is/string';
import isU8a from '@polkadot/util/is/u8a';
import isUndefined from '@polkadot/util/is/undefined';
import numberToU8a from '@polkadot/util/number/toU8a';
import u8aFromUtf8 from '@polkadot/util/u8a/fromUtf8';
import stringToU8a from '@polkadot/util/string/toU8a';
type Encoder = {
check: (value: any) => boolean,
@@ -31,7 +31,7 @@ function convertString (value: string): Uint8Array {
return hexToU8a(value);
}
return u8aFromUtf8(value);
return stringToU8a(value);
}
function convertU8a (value: Uint8Array): Uint8Array {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util",
"version": "0.31.7",
"version": "0.32.0",
"description": "A collection of useful utilities for @polkadot",
"main": "index.js",
"keywords": [
-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 { bufferFromU8a } from './index';
describe('bufferFromU8a', () => {
it('exists as a function', () => {
expect(
isFunction(bufferFromU8a)
).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 bufferFromU8a from '../u8a/toBuffer';
/**
* @name bufferFromU8a
* @signature bufferFromU8a (value?: string): Buffer
* @summary Creates a Buffer object from a Uint8Array.
* @alias u8a/toBuffer
*/
export default bufferFromU8a;
-2
View File
@@ -2,13 +2,11 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import bufferFromU8a from './fromU8a';
import bufferToU8a from './toU8a';
/**
* @summary Utility methods to convert to and from `Buffer` objects
*/
export {
bufferFromU8a,
bufferToU8a
};
-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 { hexFromBn } from './index';
describe('hexFromBn', () => {
it('exists as a function', () => {
expect(
isFunction(hexFromBn)
).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 hexFromBn from '../bn/toHex';
/**
* @name hexFromBn
* @signature hexFromBn (value?: BN): string
* @summary Creates a hex value from a BN.js bignumber object.
* @alias bn/toHex
*/
export default hexFromBn;
-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 { hexFromNumber } from './index';
describe('hexFromNumber', () => {
it('exists as a function', () => {
expect(
isFunction(hexFromNumber)
).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 hexFromNumber from '../number/toHex';
/**
* @name hexFromNumber
* @signature hexFromNumber (value?: number): string
* @summary Creates a hex value from a number.
* @alias number/toHex
*/
export default hexFromNumber;
-4
View File
@@ -4,8 +4,6 @@
import hexAddPrefix from './addPrefix';
import hexFixLength from './fixLength';
import hexFromBn from './fromBn';
import hexFromNumber from './fromNumber';
import hexHasPrefix from './hasPrefix';
import hexStripPrefix from './stripPrefix';
import hexToBn from './toBn';
@@ -18,8 +16,6 @@ import hexToU8a from './toU8a';
export {
hexAddPrefix,
hexFixLength,
hexFromBn,
hexFromNumber,
hexHasPrefix,
hexStripPrefix,
hexToBn,
-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 { numberFromHex } from './index';
describe('numberFromHex', () => {
it('exists as a function', () => {
expect(
isFunction(numberFromHex)
).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 numberFromHex from '../hex/toNumber';
/**
* @name numberFromHex
* @signature numberFromHex (value?: Hex): number
* @summary Creates a Number value from a Buffer object.
* @alias hex/toNumber
*/
export default numberFromHex;
-2
View File
@@ -2,7 +2,6 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import numberFromHex from './fromHex';
import numberToHex from './toHex';
import numberToU8a from './toU8a';
@@ -10,7 +9,6 @@ import numberToU8a from './toU8a';
* @summary Utility methods to convert to and from `number` values
*/
export {
numberFromHex,
numberToHex,
numberToU8a
};
+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
};
@@ -2,18 +2,18 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { u8aFromUtf8 } from './index';
import { stringToU8a } from './index';
describe('u8aFromUtf8', () => {
describe('stringToU8a', () => {
it('decodes to an empty string for undefined', () => {
expect(
u8aFromUtf8()
stringToU8a()
).toEqual(new Uint8Array([]));
});
it('encodes the string correctly', () => {
expect(
u8aFromUtf8('Привет, мир!')
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])
);
@@ -7,17 +7,17 @@ import './polyfill/textEncoder';
const encoder = new TextEncoder();
/**
* @name u8aFromUtf8
* @signature u8aFromUtf8 (value?: string): UInt8Array
* @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 { u8aFromUtf8 } from '@polkadot/util';
* import { stringToU8a } from '@polkadot/util';
*
* u8aFromUtf8('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
* stringToU8a('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
*/
export default function u8aFromUtf8 (value?: string): Uint8Array {
export default function stringToU8a (value?: string): Uint8Array {
if (!value) {
return new Uint8Array([]);
}
-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);
}
+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
};
+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);
}