UNPKG

824 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.floatToU8a = void 0;
4/**
5 * @name floatToU8a
6 * @description Converts a float into a U8a representation (While we don't use BE in SCALE
7 * we still allow for either representation, although, as elsewhere, isLe is default)
8 */
9function floatToU8a(value = 0.0, { bitLength = 32, isLe = true } = {}) {
10 if (bitLength !== 32 && bitLength !== 64) {
11 throw new Error('Invalid bitLength provided, expected 32 or 64');
12 }
13 const result = new Uint8Array(bitLength / 8);
14 const dv = new DataView(result.buffer, result.byteOffset);
15 if (bitLength === 32) {
16 dv.setFloat32(0, Number(value), isLe);
17 }
18 else {
19 dv.setFloat64(0, Number(value), isLe);
20 }
21 return result;
22}
23exports.floatToU8a = floatToU8a;