mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-10-22 11:36:18 +00:00
Initial rebrand: @polkadot -> @pezkuwi (14 packages)
- Package namespace: @polkadot/* -> @pezkuwi/* - Repository: polkadot-js/common -> pezkuwichain/pezkuwi-common - Author: Pezkuwi Team <team@pezkuwichain.io> Core packages: - @pezkuwi/util (utilities) - @pezkuwi/util-crypto (crypto primitives) - @pezkuwi/keyring (account management) - @pezkuwi/networks (chain metadata) - @pezkuwi/hw-ledger (Ledger hardware wallet) - @pezkuwi/x-* (10 polyfill packages) Total: 14 packages Upstream: polkadot-js/common v14.0.1
This commit is contained in:
commit
ec06da0ebc
687 files changed
+48096
No files matched your search
@@ -0,0 +1,4 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { floatToU8a } from './toU8a.js';
|
||||
@@ -0,0 +1,52 @@
|
||||
// Copyright 2017-2025 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { u8aToHex } from '../u8a/index.js';
|
||||
import { floatToU8a } from './index.js';
|
||||
|
||||
class ExtNumber extends Number {
|
||||
foo = 'bar';
|
||||
}
|
||||
|
||||
class ExtString extends String {
|
||||
foo = 'bar';
|
||||
}
|
||||
|
||||
// NOTE Hex value outputs created via online conversion tool:
|
||||
// https://www.h-schmidt.net/FloatConverter/IEEE754.html
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
const TESTS: [isLe: boolean | undefined, bitLength: 32 | 64 | undefined, input: String | string | number | Number, output: string][] = [
|
||||
[undefined, undefined, +0.0, '0x00000000'],
|
||||
[undefined, undefined, -0.0, '0x00000080'],
|
||||
[undefined, undefined, 123.456, '0x79e9f642'],
|
||||
[undefined, undefined, '123.456', '0x79e9f642'],
|
||||
[undefined, undefined, new ExtNumber(123.456), '0x79e9f642'],
|
||||
[undefined, undefined, new ExtString(123.456), '0x79e9f642'],
|
||||
[true, 32, new ExtString(123.456), '0x79e9f642'],
|
||||
[true, undefined, Number.NaN, '0x0000c07f'],
|
||||
[false, undefined, -0.0, '0x80000000'],
|
||||
[undefined, 64, +0.0, '0x0000000000000000'],
|
||||
[true, 64, -0.0, '0x0000000000000080'],
|
||||
[false, 64, -0.0, '0x8000000000000000'],
|
||||
[undefined, 64, Number.NaN, '0x000000000000f87f']
|
||||
];
|
||||
|
||||
describe('floatToU8a', (): void => {
|
||||
it('throws on invalid bitLength', (): void => {
|
||||
expect(
|
||||
() => floatToU8a(123, { bitLength: 48 as 32 })
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
describe('conversion tests', (): void => {
|
||||
TESTS.forEach(([isLe, bitLength, input, output], i): void => {
|
||||
it(`#${i}: correctly encodes ${typeof input === 'number' ? input : input.toString()} (typeof=${typeof input})`, (): void => {
|
||||
expect(
|
||||
u8aToHex(floatToU8a(input, { bitLength, isLe }))
|
||||
).toEqual(output);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright 2017-2025 @polkadot/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/ban-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;
|
||||
}
|
||||
Reference in new issue
Block a user