@@ -3,7 +3,7 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const u8aToU8a = require('./toU8a');
|
||||
import u8aToU8a from './toU8a';
|
||||
|
||||
/**
|
||||
@name u8aConcat
|
||||
@@ -19,7 +19,7 @@ const u8aToU8a = require('./toU8a');
|
||||
new Uint8Array([4, 5, 6])
|
||||
); // [1, 2, 3, 4, 5, 6]
|
||||
*/
|
||||
module.exports = function u8aConcat (..._list: Array<Uint8Array | string>): Uint8Array {
|
||||
export default function u8aConcat (..._list: Array<Uint8Array | string>): Uint8Array {
|
||||
const list: Array<Uint8Array> = _list.map(u8aToU8a);
|
||||
const length = list.reduce((total, item) => total + item.length, 0);
|
||||
const result = new Uint8Array(length);
|
||||
@@ -31,4 +31,4 @@ module.exports = function u8aConcat (..._list: Array<Uint8Array | string>): Uint
|
||||
|
||||
return result;
|
||||
}, result);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aConcat } = require('./index');
|
||||
import { u8aConcat } from './index';
|
||||
|
||||
describe('u8aConcat', () => {
|
||||
it('concatenates arrays', () => {
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
u8aFixLength('0x12', 16) // => 0x0012
|
||||
u8aFixLength('0x1234', 8) // => 0x12
|
||||
*/
|
||||
module.exports = function u8aFixLength (value: Uint8Array, bitLength: number = -1, atStart?: boolean = false): Uint8Array {
|
||||
export default function u8aFixLength (value: Uint8Array, bitLength: number = -1, atStart?: boolean = false): Uint8Array {
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
|
||||
if (bitLength === -1 || value.length === byteLength) {
|
||||
@@ -36,4 +36,4 @@ module.exports = function u8aFixLength (value: Uint8Array, bitLength: number = -
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aFixLength } = require('./index');
|
||||
import { u8aFixLength } from './index';
|
||||
|
||||
describe('u8aFixLength', () => {
|
||||
it('returns bitLength === -1 as-is', () => {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const u8aFromBn = require('../bn/toU8a');
|
||||
import u8aFromBn from '../bn/toU8a';
|
||||
|
||||
/**
|
||||
@name u8aFromBn
|
||||
@@ -11,4 +11,4 @@ const u8aFromBn = require('../bn/toU8a');
|
||||
@summary Creates a Uint8Array value from a BN object.
|
||||
@alias bn/toU8a
|
||||
*/
|
||||
module.exports = u8aFromBn;
|
||||
export default u8aFromBn;
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
import isFunction from '../is/function';
|
||||
|
||||
const { u8aFromBn } = require('./index');
|
||||
import { u8aFromBn } from './index';
|
||||
|
||||
describe('u8aFromBn', () => {
|
||||
it('exists as a function', () => {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const u8aFromBuffer = require('../buffer/toU8a');
|
||||
import u8aFromBuffer from '../buffer/toU8a';
|
||||
|
||||
/**
|
||||
@name u8aFromBuffer
|
||||
@@ -11,4 +11,4 @@ const u8aFromBuffer = require('../buffer/toU8a');
|
||||
@summary Creates a Uint8Array value from a Buffer object.
|
||||
@alias buffer/toU8a
|
||||
*/
|
||||
module.exports = u8aFromBuffer;
|
||||
export default u8aFromBuffer;
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
import isFunction from '../is/function';
|
||||
|
||||
const { u8aFromBuffer } = require('./index');
|
||||
import { u8aFromBuffer } from './index';
|
||||
|
||||
describe('u8aFromBuffer', () => {
|
||||
it('exists as a function', () => {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const u8aFromHex = require('../hex/toU8a');
|
||||
import u8aFromHex from '../hex/toU8a';
|
||||
|
||||
/**
|
||||
@name u8aFromHex
|
||||
@@ -11,4 +11,4 @@ const u8aFromHex = require('../hex/toU8a');
|
||||
@summary Creates a Uint8Array value from a hex string.
|
||||
@alias hex/toU8a
|
||||
*/
|
||||
module.exports = u8aFromHex;
|
||||
export default u8aFromHex;
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
import isFunction from '../is/function';
|
||||
|
||||
const { u8aFromHex } = require('./index');
|
||||
import { u8aFromHex } from './index';
|
||||
|
||||
describe('u8aFromHex', () => {
|
||||
it('exists as a function', () => {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
u8aFromString('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
|
||||
*/
|
||||
module.exports = function u8aFromString (value: string): Uint8Array {
|
||||
export default function u8aFromString (value: string): Uint8Array {
|
||||
const stringArray = [];
|
||||
|
||||
for (let index = 0; index < value.length; index++) {
|
||||
@@ -24,4 +24,4 @@ module.exports = function u8aFromString (value: string): Uint8Array {
|
||||
}
|
||||
|
||||
return new Uint8Array(stringArray);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aFromString } = require('./index');
|
||||
import { u8aFromString } from './index';
|
||||
|
||||
describe('u8aFromString', () => {
|
||||
it('encodes a string byte for byte', () => {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
require('./polyfill/textEncoder');
|
||||
import './polyfill/textEncoder';
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
@@ -18,11 +18,11 @@ const encoder = new TextEncoder();
|
||||
|
||||
u8aFromUtf8('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
|
||||
*/
|
||||
module.exports = function u8aFromUtf8 (value?: string): Uint8Array {
|
||||
export default function u8aFromUtf8 (value?: string): Uint8Array {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (!value) {
|
||||
return new Uint8Array([]);
|
||||
}
|
||||
|
||||
return encoder.encode(value);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aFromUtf8 } = require('./index');
|
||||
import { u8aFromUtf8 } from './index';
|
||||
|
||||
describe('u8aFromUtf8', () => {
|
||||
it('decodes to an empty string for undefined', () => {
|
||||
|
||||
@@ -3,24 +3,24 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const u8aConcat = require('./concat');
|
||||
const u8aFixLength = require('./fixLength');
|
||||
const u8aFromBn = require('./fromBn');
|
||||
const u8aFromBuffer = require('./fromBuffer');
|
||||
const u8aFromHex = require('./fromHex');
|
||||
const u8aFromUtf8 = require('./fromUtf8');
|
||||
const u8aFromString = require('./fromString');
|
||||
const u8aToBn = require('./toBn');
|
||||
const u8aToBuffer = require('./toBuffer');
|
||||
const u8aToHex = require('./toHex');
|
||||
const u8aToString = require('./toString');
|
||||
const u8aToU8a = require('./toU8a');
|
||||
const u8aToUtf8 = require('./toUtf8');
|
||||
import u8aConcat from './concat';
|
||||
import u8aFixLength from './fixLength';
|
||||
import u8aFromBn from './fromBn';
|
||||
import u8aFromBuffer from './fromBuffer';
|
||||
import u8aFromHex from './fromHex';
|
||||
import u8aFromUtf8 from './fromUtf8';
|
||||
import u8aFromString from './fromString';
|
||||
import u8aToBn from './toBn';
|
||||
import u8aToBuffer from './toBuffer';
|
||||
import u8aToHex from './toHex';
|
||||
import u8aToString from './toString';
|
||||
import u8aToU8a from './toU8a';
|
||||
import u8aToUtf8 from './toUtf8';
|
||||
|
||||
/**
|
||||
@summary Utility methods to convert to and from `Uint8Array` objects
|
||||
*/
|
||||
module.exports = {
|
||||
export {
|
||||
u8aConcat,
|
||||
u8aFixLength,
|
||||
u8aFromBn,
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const BN = require('bn.js');
|
||||
import BN from 'bn.js';
|
||||
|
||||
const hexToBn = require('../hex/toBn');
|
||||
const u8aToHex = require('./toHex');
|
||||
import hexToBn from '../hex/toBn';
|
||||
import u8aToHex from './toHex';
|
||||
|
||||
/**
|
||||
@name u8aToBn
|
||||
@@ -19,9 +19,9 @@ const u8aToHex = require('./toHex');
|
||||
|
||||
u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
|
||||
*/
|
||||
module.exports = function u8aToBn (value?: Uint8Array, isLe: boolean = false): BN {
|
||||
export default function u8aToBn (value?: Uint8Array, isLe: boolean = false): BN {
|
||||
return hexToBn(
|
||||
u8aToHex(value),
|
||||
isLe
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aToBn } = require('./index');
|
||||
import { u8aToBn } from './index';
|
||||
|
||||
describe('u8aToBn', () => {
|
||||
it('converts values', () => {
|
||||
|
||||
@@ -14,10 +14,10 @@
|
||||
|
||||
console.log('Buffer', u8aToBuffer('0x123480001f'));
|
||||
*/
|
||||
module.exports = function u8aToBuffer (value?: Uint8Array): Buffer {
|
||||
export default function u8aToBuffer (value?: Uint8Array): Buffer {
|
||||
if (!value) {
|
||||
return Buffer.from([]);
|
||||
}
|
||||
|
||||
return Buffer.from(value);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aToBuffer } = require('./index');
|
||||
import { u8aToBuffer } from './index';
|
||||
|
||||
describe('u8aToBuffer', () => {
|
||||
it('returns [] when the buffer is null', () => {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
|
||||
*/
|
||||
module.exports = function u8aToHex (value?: Uint8Array | null, bitLength: number = -1, isPrefixed: boolean = true): string {
|
||||
export default function u8aToHex (value?: Uint8Array | null, bitLength: number = -1, isPrefixed: boolean = true): string {
|
||||
const prefix = isPrefixed
|
||||
? '0x'
|
||||
: '';
|
||||
@@ -34,4 +34,4 @@ module.exports = function u8aToHex (value?: Uint8Array | null, bitLength: number
|
||||
return value.reduce((result, item) => {
|
||||
return result + `0${item.toString(16)}`.slice(-2);
|
||||
}, prefix);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aToHex } = require('./index');
|
||||
import { u8aToHex } from './index';
|
||||
|
||||
describe('u8aToHex', () => {
|
||||
it('returns empty as 0x', () => {
|
||||
|
||||
@@ -14,10 +14,10 @@
|
||||
|
||||
u8aToString(new Uint8Array([21,23,45,67])); // 21,23,45,67
|
||||
*/
|
||||
module.exports = function u8aToString (value?: Uint8Array): string {
|
||||
export default function u8aToString (value?: Uint8Array): string {
|
||||
if (!value || !value.length) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return value.toString();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aToString } = require('./index');
|
||||
import { u8aToString } from './index';
|
||||
|
||||
describe('u8aToString', () => {
|
||||
it('decodes to an empty string for undefined', () => {
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const isBuffer = require('../is/buffer');
|
||||
const isHex = require('../is/hex');
|
||||
const isString = require('../is/string');
|
||||
const bufferToU8a = require('../buffer/toU8a');
|
||||
const hexToU8a = require('../hex/toU8a');
|
||||
const u8aFromUtf8 = require('./fromUtf8');
|
||||
import isBuffer from '../is/buffer';
|
||||
import isHex from '../is/hex';
|
||||
import isString from '../is/string';
|
||||
import bufferToU8a from '../buffer/toU8a';
|
||||
import hexToU8a from '../hex/toU8a';
|
||||
import u8aFromUtf8 from './fromUtf8';
|
||||
|
||||
/**
|
||||
@name u8aToU8a
|
||||
@@ -22,7 +22,7 @@ const u8aFromUtf8 = require('./fromUtf8');
|
||||
u8aToU8a(new Uint8Array([0x12, 0x34]); // => Uint8Array([0x12, 0x34])
|
||||
u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34])
|
||||
*/
|
||||
module.exports = function u8aToU8a (value?: Buffer | Uint8Array | string | null): Uint8Array {
|
||||
export default function u8aToU8a (value?: Buffer | Uint8Array | string | null): Uint8Array {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (!value) {
|
||||
return new Uint8Array(0);
|
||||
@@ -45,4 +45,4 @@ module.exports = function u8aToU8a (value?: Buffer | Uint8Array | string | null)
|
||||
|
||||
// $FlowFixMe type is determined
|
||||
return value;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aToU8a } = require('./index');
|
||||
import { u8aToU8a } from './index';
|
||||
|
||||
describe('u8aToU8a', () => {
|
||||
it('returns an empty Uint8Array when null provided', () => {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
require('./polyfill/textDecoder');
|
||||
import './polyfill/textDecoder';
|
||||
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
|
||||
@@ -18,10 +18,10 @@ const decoder = new TextDecoder('utf-8');
|
||||
|
||||
u8aToUtf8(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello
|
||||
*/
|
||||
module.exports = function u8aToUtf8 (value?: Uint8Array): string {
|
||||
export default function u8aToUtf8 (value?: Uint8Array): string {
|
||||
if (!value || !value.length) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return decoder.decode(value);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aToUtf8 } = require('./index');
|
||||
import { u8aToUtf8 } from './index';
|
||||
|
||||
describe('u8aToUtf8', () => {
|
||||
it('decodes to an empty string for undefined', () => {
|
||||
|
||||
Reference in New Issue
Block a user