mirror of https://github.com/status-im/js-waku.git
Test hex conversion functions
This commit is contained in:
parent
ee38df8757
commit
27c433f750
|
@ -1,6 +1,6 @@
|
|||
import test from 'ava';
|
||||
|
||||
import { argsToArray, mergeArguments } from './nim_waku';
|
||||
import { argsToArray, bufToHex, mergeArguments, strToHex } from './nim_waku';
|
||||
|
||||
test('Default arguments are correct', (t) => {
|
||||
const args = mergeArguments({});
|
||||
|
@ -36,3 +36,44 @@ test('Passing staticnode argument return default + static node', (t) => {
|
|||
|
||||
t.deepEqual(actual, expected);
|
||||
});
|
||||
|
||||
test('Convert utf-8 string to hex', (t) => {
|
||||
const str = 'This is an utf-8 string.';
|
||||
const expected = '0x5468697320697320616e207574662d3820737472696e672e';
|
||||
|
||||
const actual = strToHex(str);
|
||||
t.deepEqual(actual, expected);
|
||||
});
|
||||
|
||||
test('Convert buffer to hex', (t) => {
|
||||
const buf = Uint8Array.from([
|
||||
0x54,
|
||||
0x68,
|
||||
0x69,
|
||||
0x73,
|
||||
0x20,
|
||||
0x69,
|
||||
0x73,
|
||||
0x20,
|
||||
0x61,
|
||||
0x6e,
|
||||
0x20,
|
||||
0x75,
|
||||
0x74,
|
||||
0x66,
|
||||
0x2d,
|
||||
0x38,
|
||||
0x20,
|
||||
0x73,
|
||||
0x74,
|
||||
0x72,
|
||||
0x69,
|
||||
0x6e,
|
||||
0x67,
|
||||
0x2e,
|
||||
]);
|
||||
const expected = '0x5468697320697320616e207574662d3820737472696e672e';
|
||||
|
||||
const actual = bufToHex(buf);
|
||||
t.deepEqual(actual, expected);
|
||||
});
|
||||
|
|
|
@ -178,14 +178,13 @@ export function mergeArguments(args: Args): Args {
|
|||
return res;
|
||||
}
|
||||
|
||||
// TODO: Test this
|
||||
function strToHex(str: string): string {
|
||||
export function strToHex(str: string): string {
|
||||
let hex: string;
|
||||
try {
|
||||
hex = unescape(encodeURIComponent(str))
|
||||
.split('')
|
||||
.map(function (v) {
|
||||
return v.charCodeAt(0).toString(16).toUpperCase();
|
||||
return v.charCodeAt(0).toString(16);
|
||||
})
|
||||
.join('');
|
||||
} catch (e) {
|
||||
|
@ -195,9 +194,11 @@ function strToHex(str: string): string {
|
|||
return '0x' + hex;
|
||||
}
|
||||
|
||||
// TODO: Test this
|
||||
function bufToHex(buffer: Uint8Array) {
|
||||
return Array.prototype.map
|
||||
.call(buffer, (x) => ('00' + x.toString(16)).slice(-2))
|
||||
.join('');
|
||||
export function bufToHex(buffer: Uint8Array) {
|
||||
return (
|
||||
'0x' +
|
||||
Array.prototype.map
|
||||
.call(buffer, (x) => ('00' + x.toString(16)).slice(-2))
|
||||
.join('')
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue