string <-> hex conversion utilities (#470)
* string <-> hex conversion utilities * Add tests
This commit is contained in:
@@ -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';
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user