Files
pezkuwi-common/packages/util/src/float/toU8a.ts
T
Satoshi Qazi Muhammed c46ce1a9e6 Move to @pezkuwi/dev 0.86 (eslint 10 toolchain)
- @pezkuwi/dev, dev-test, dev-ts ^0.86.3
- rethrown errors carry the original as `cause` (hw-ledger, address decode)
- optional chain in isEthereumAddress, includes() in bip39 validation
- logger calls the lazy formatter through a typed function
- x-global: an assertion to the type the value already has removed,
  with the comment and directive that explained it
- type-only re-exports of modules that emit no JavaScript
- disable directives renamed for the new rule names, or removed where
  the rule no longer reports
2026-10-09 16:32:28 +04:00

31 lines
955 B
TypeScript

// Copyright 2017-2026 @pezkuwi/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
interface Options {
bitLength?: 32 | 64;
isLe?: boolean;
}
/**
* @name floatToU8a
* @description Converts a float into a U8a representation (While we don't use BE in SCALE
* we still allow for either representation, although, as elsewhere, isLe is default)
*/
// eslint-disable-next-line @typescript-eslint/no-wrapper-object-types
export function floatToU8a (value: String | string | number | Number = 0.0, { bitLength = 32, isLe = true }: Options = {}): Uint8Array {
if (bitLength !== 32 && bitLength !== 64) {
throw new Error('Invalid bitLength provided, expected 32 or 64');
}
const result = new Uint8Array(bitLength / 8);
const dv = new DataView(result.buffer, result.byteOffset);
if (bitLength === 32) {
dv.setFloat32(0, Number(value), isLe);
} else {
dv.setFloat64(0, Number(value), isLe);
}
return result;
}