Skip to content

Commit

Permalink
string <-> hex conversion utilities (#470)
Browse files Browse the repository at this point in the history
* string <-> hex conversion utilities

* Add tests
  • Loading branch information
jacogr authored Sep 12, 2019
1 parent 891a855 commit 00374ef
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
1 change: 1 addition & 0 deletions packages/util/src/hex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
19 changes: 19 additions & 0 deletions packages/util/src/hex/toString.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
26 changes: 26 additions & 0 deletions packages/util/src/hex/toString.ts
Original file line number Diff line number Diff line change
@@ -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
* <BR>
*
* ```javascript
* import { hexToString } from '@polkadot/util';
*
* hexToU8a('0x68656c6c6f'); // hello
* ```
*/
export default function hexToString (_value?: string | null): string {
return u8aToString(
hexToU8a(_value)
);
}
2 changes: 1 addition & 1 deletion packages/util/src/hex/toU8a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/util/src/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
19 changes: 19 additions & 0 deletions packages/util/src/string/toHex.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
26 changes: 26 additions & 0 deletions packages/util/src/string/toHex.ts
Original file line number Diff line number Diff line change
@@ -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
* <BR>
*
* ```javascript
* import { stringToHex } from '@polkadot/util';
*
* stringToU8a('hello'); // 0x68656c6c6f
* ```
*/
export default function stringToHex (value?: string): string {
return u8aToHex(
stringToU8a(value)
);
}

0 comments on commit 00374ef

Please sign in to comment.