From 00374effc558c11f9b1755eae10846a5c8abe4c0 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 12 Sep 2019 13:48:06 +0200 Subject: [PATCH] string <-> hex conversion utilities (#470) * string <-> hex conversion utilities * Add tests --- CHANGELOG.md | 4 ++++ packages/util/src/hex/index.ts | 1 + packages/util/src/hex/toString.spec.ts | 19 +++++++++++++++++++ packages/util/src/hex/toString.ts | 26 ++++++++++++++++++++++++++ packages/util/src/hex/toU8a.ts | 2 +- packages/util/src/string/index.ts | 1 + packages/util/src/string/toHex.spec.ts | 19 +++++++++++++++++++ packages/util/src/string/toHex.ts | 26 ++++++++++++++++++++++++++ 8 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 packages/util/src/hex/toString.spec.ts create mode 100644 packages/util/src/hex/toString.ts create mode 100644 packages/util/src/string/toHex.spec.ts create mode 100644 packages/util/src/string/toHex.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 87aafddcfa..64c84d4fdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.4.0-beta.x + +- Added `stringToHex` and `hexToString` conversion utilities + # 1.3.1 - Remove the `ExtError` class, always prefer the standard JS `Error` object for errors. This would bre a breaking change for any appliactions using `ExtError` diff --git a/packages/util/src/hex/index.ts b/packages/util/src/hex/index.ts index acf2fe50fa..a11248194e 100644 --- a/packages/util/src/hex/index.ts +++ b/packages/util/src/hex/index.ts @@ -12,4 +12,5 @@ export { default as hexHasPrefix } from './hasPrefix'; export { default as hexStripPrefix } from './stripPrefix'; export { default as hexToBn } from './toBn'; export { default as hexToNumber } from './toNumber'; +export { default as hexToString } from './toString'; export { default as hexToU8a } from './toU8a'; diff --git a/packages/util/src/hex/toString.spec.ts b/packages/util/src/hex/toString.spec.ts new file mode 100644 index 0000000000..bfecee68f3 --- /dev/null +++ b/packages/util/src/hex/toString.spec.ts @@ -0,0 +1,19 @@ +// Copyright 2017-2019 @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. + +import { hexToString } from '.'; + +describe('hexToString', (): void => { + it('converts an empty to ""', (): void => { + expect( + hexToString() + ).toEqual(''); + }); + + it('converts to a string from hex', (): void => { + expect( + hexToString('0x68656c6c6f') + ).toEqual('hello'); + }); +}); diff --git a/packages/util/src/hex/toString.ts b/packages/util/src/hex/toString.ts new file mode 100644 index 0000000000..db8ad539be --- /dev/null +++ b/packages/util/src/hex/toString.ts @@ -0,0 +1,26 @@ +// Copyright 2017-2019 @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. + +import u8aToString from '../u8a/toString'; +import hexToU8a from './toU8a'; + +/** + * @name hexToU8a + * @summary Creates a Uint8Array object from a hex string. + * @description + * Hex input values return the actual bytes value converted to a string. Anything that is not a hex string (including the `0x` prefix) throws an error. + * @example + *
+ * + * ```javascript + * import { hexToString } from '@polkadot/util'; + * + * hexToU8a('0x68656c6c6f'); // hello + * ``` + */ +export default function hexToString (_value?: string | null): string { + return u8aToString( + hexToU8a(_value) + ); +} diff --git a/packages/util/src/hex/toU8a.ts b/packages/util/src/hex/toU8a.ts index 2195ae45f2..c4e33e5edf 100644 --- a/packages/util/src/hex/toU8a.ts +++ b/packages/util/src/hex/toU8a.ts @@ -8,7 +8,7 @@ import hexStripPrefix from './stripPrefix'; /** * @name hexToU8a - * @summary Creates a Buffer object from a hex string. + * @summary Creates a Uint8Array object from a hex string. * @description * `null` inputs returns an empty `Uint8Array` result. Hex input values return the actual bytes value converted to a Uint8Array. Anything that is not a hex string (including the `0x` prefix) throws an error. * @example diff --git a/packages/util/src/string/index.ts b/packages/util/src/string/index.ts index a7857759d6..30d4523562 100644 --- a/packages/util/src/string/index.ts +++ b/packages/util/src/string/index.ts @@ -9,5 +9,6 @@ export { default as stringCamelCase } from './camelCase'; export { default as stringLowerFirst } from './lowerFirst'; export { default as stringShorten } from './shorten'; +export { default as stringToHex } from './toHex'; export { default as stringToU8a } from './toU8a'; export { default as stringUpperFirst } from './upperFirst'; diff --git a/packages/util/src/string/toHex.spec.ts b/packages/util/src/string/toHex.spec.ts new file mode 100644 index 0000000000..b5f18187fd --- /dev/null +++ b/packages/util/src/string/toHex.spec.ts @@ -0,0 +1,19 @@ +// Copyright 2017-2019 @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. + +import { stringToHex } from '.'; + +describe('hexToString', (): void => { + it('converts an empty to ""', (): void => { + expect( + stringToHex() + ).toEqual('0x'); + }); + + it('converts to a hex from string', (): void => { + expect( + stringToHex('hello') + ).toEqual('0x68656c6c6f'); + }); +}); diff --git a/packages/util/src/string/toHex.ts b/packages/util/src/string/toHex.ts new file mode 100644 index 0000000000..06bf6c03cb --- /dev/null +++ b/packages/util/src/string/toHex.ts @@ -0,0 +1,26 @@ +// Copyright 2017-2019 @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. + +import u8aToHex from '../u8a/toHex'; +import stringToU8a from './toU8a'; + +/** + * @name stringToHex + * @summary Creates a hex string from a utf-8 string + * @description + * String input values return the actual encoded hex value. + * @example + *
+ * + * ```javascript + * import { stringToHex } from '@polkadot/util'; + * + * stringToU8a('hello'); // 0x68656c6c6f + * ``` + */ +export default function stringToHex (value?: string): string { + return u8aToHex( + stringToU8a(value) + ); +}