@tetcoin/util
Version:
A collection of useful utilities for @tetcoin
40 lines (32 loc) • 1.23 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = numberToU8a;
var _toU8a = _interopRequireDefault(require("../hex/toU8a"));
var _toHex = _interopRequireDefault(require("./toHex"));
// 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.
/**
* @name numberToU8a
* @summary Creates a Uint8Array object from a number.
* @description
* `null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `number` input values return the actual bytes value converted to a `Uint8Array`. With `bitLength`, it converts the value to the equivalent size.
* @example
* <BR>
*
* ```javascript
* import { numberToU8a } from '@tetcoin/util';
*
* numberToU8a(0x1234); // => [0x12, 0x34]
* ```
*/
function numberToU8a(value) {
let bitLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -1;
if (!value || isNaN(value)) {
return new Uint8Array([]);
}
return (0, _toU8a.default)((0, _toHex.default)(value, bitLength));
}