Align NaCl functions with Rust tests (#72)
* Align nacl functions with Rust tests * Match tests from Rust
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@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]
|
||||
*/
|
||||
module.exports = function u8aFromString (value: string): Uint8Array {
|
||||
const stringArray = [];
|
||||
|
||||
for (let index = 0; index < value.length; index++) {
|
||||
stringArray.push(
|
||||
value.charCodeAt(index)
|
||||
);
|
||||
}
|
||||
|
||||
return new Uint8Array(stringArray);
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aFromString } = require('./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
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -6,6 +6,7 @@
|
||||
const u8aFromBuffer = require('./fromBuffer');
|
||||
const u8aFromHex = require('./fromHex');
|
||||
const u8aFromUtf8 = require('./fromUtf8');
|
||||
const u8aFromString = require('./fromString');
|
||||
const u8aToBuffer = require('./toBuffer');
|
||||
const u8aToUtf8 = require('./toUtf8');
|
||||
|
||||
@@ -15,6 +16,7 @@ const u8aToUtf8 = require('./toUtf8');
|
||||
module.exports = {
|
||||
u8aFromBuffer,
|
||||
u8aFromHex,
|
||||
u8aFromString,
|
||||
u8aFromUtf8,
|
||||
u8aToBuffer,
|
||||
u8aToUtf8
|
||||
|
||||
Reference in New Issue
Block a user