From a31932f3b1df39c0e1ff8d66ebbea426d9cbf95b Mon Sep 17 00:00:00 2001 From: Ross Bulat Date: Mon, 4 Nov 2024 20:23:15 +0700 Subject: [PATCH] feat(refactor): Misc utils improvements (#142) --- library/utils/package.json | 2 +- library/utils/src/convert.ts | 24 +++++++++++++ library/utils/src/index.ts | 1 + library/utils/src/unit.ts | 27 +------------- library/utils/tests/convert.test.ts | 56 +++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 27 deletions(-) create mode 100644 library/utils/src/convert.ts create mode 100644 library/utils/tests/convert.test.ts diff --git a/library/utils/package.json b/library/utils/package.json index 444f693..e6e5f02 100644 --- a/library/utils/package.json +++ b/library/utils/package.json @@ -1,7 +1,7 @@ { "name": "@w3ux/utils-source", "license": "GPL-3.0-only", - "version": "1.1.0", + "version": "1.1.1-beta.9", "type": "module", "scripts": { "clear": "rm -rf node_modules dist tsconfig.tsbuildinfo", diff --git a/library/utils/src/convert.ts b/library/utils/src/convert.ts new file mode 100644 index 0000000..5224a6f --- /dev/null +++ b/library/utils/src/convert.ts @@ -0,0 +1,24 @@ +// /* @license Copyright 2024 w3ux authors & contributors +// SPDX-License-Identifier: GPL-3.0-only */ + +/** + * Concatenates multiple Uint8Array instances into a single Uint8Array. + * + * @param {Uint8Array[]} u8as - An array of Uint8Array instances to concatenate. + * @returns {Uint8Array} A new Uint8Array containing all the input arrays concatenated. + */ +export const u8aConcat = (...u8as: Uint8Array[]): Uint8Array => { + // Calculate the total length of the resulting Uint8Array + const totalLength = u8as.reduce((sum, u8a) => sum + u8a.length, 0); + + // Create a new Uint8Array with the total length + const result = new Uint8Array(totalLength); + + let offset = 0; // Initialize the offset for placing elements + for (const u8a of u8as) { + result.set(u8a, offset); // Set the current Uint8Array at the current offset + offset += u8a.length; // Update the offset for the next Uint8Array + } + + return result; +}; diff --git a/library/utils/src/index.ts b/library/utils/src/index.ts index a44a131..cec58ab 100644 --- a/library/utils/src/index.ts +++ b/library/utils/src/index.ts @@ -2,4 +2,5 @@ SPDX-License-Identifier: GPL-3.0-only */ export * from "./base"; +export * from "./convert"; export * from "./unit"; diff --git a/library/utils/src/unit.ts b/library/utils/src/unit.ts index f75188c..50b9ec8 100644 --- a/library/utils/src/unit.ts +++ b/library/utils/src/unit.ts @@ -1,10 +1,9 @@ /* @license Copyright 2024 w3ux authors & contributors SPDX-License-Identifier: GPL-3.0-only */ -import { u8aToString, u8aUnwrapBytes } from "@polkadot/util"; import type { MutableRefObject, RefObject } from "react"; import { AnyObject } from "./types"; -import { ellipsisFn, rmCommas } from "./base"; +import { rmCommas } from "./base"; import { AnyJson } from "@w3ux/types"; import { AccountId } from "@polkadot-api/substrate-bindings"; @@ -177,30 +176,6 @@ export const isValidAddress = (address: string): boolean => { } }; -/** - * @name determinePoolDisplay - * @summary A pool will be displayed with either its set metadata or its address. - */ -export const determinePoolDisplay = (address: string, batchItem: AnyJson) => { - // default display value - const defaultDisplay = ellipsisFn(address, 6); - - // fallback to address on empty metadata string - let display = batchItem ?? defaultDisplay; - - // check if super identity has been byte encoded - const displayAsBytes = u8aToString(u8aUnwrapBytes(display)); - if (displayAsBytes !== "") { - display = displayAsBytes; - } - // if still empty string, default to clipped address - if (display === "") { - display = defaultDisplay; - } - - return display; -}; - /** * @name extractUrlValue * @summary Extracts a URL value from a URL string. diff --git a/library/utils/tests/convert.test.ts b/library/utils/tests/convert.test.ts new file mode 100644 index 0000000..fd6836e --- /dev/null +++ b/library/utils/tests/convert.test.ts @@ -0,0 +1,56 @@ +// /* @license Copyright 2024 w3ux authors & contributors +// SPDX-License-Identifier: GPL-3.0-only */ + +import { describe, expect, test } from "vitest"; +import * as fn from "../src/index"; + +describe("u8aConcat", () => { + test("should concatenate multiple Uint8Array instances", () => { + const u8a1 = new Uint8Array([1, 2, 3]); + const u8a2 = new Uint8Array([4, 5, 6]); + const u8a3 = new Uint8Array([7, 8]); + + const result = fn.u8aConcat(u8a1, u8a2, u8a3); + expect(result).toEqual(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8])); + }); + + test("should handle an empty Uint8Array", () => { + const u8a1 = new Uint8Array([1, 2, 3]); + const u8a2 = new Uint8Array([]); // Empty array + + const result = fn.u8aConcat(u8a1, u8a2); + expect(result).toEqual(new Uint8Array([1, 2, 3])); // Should return the first array + }); + + test("should handle multiple empty Uint8Arrays", () => { + const u8a1 = new Uint8Array([]); + const u8a2 = new Uint8Array([]); + + const result = fn.u8aConcat(u8a1, u8a2); + expect(result).toEqual(new Uint8Array([])); // Should return an empty array + }); + + test("should handle single Uint8Array", () => { + const u8a1 = new Uint8Array([9, 10, 11]); + + const result = fn.u8aConcat(u8a1); + expect(result).toEqual(u8a1); // Should return the same array + }); + + test("should handle concatenation of different lengths", () => { + const u8a1 = new Uint8Array([1, 2]); + const u8a2 = new Uint8Array([3]); + const u8a3 = new Uint8Array([4, 5, 6]); + + const result = fn.u8aConcat(u8a1, u8a2, u8a3); + expect(result).toEqual(new Uint8Array([1, 2, 3, 4, 5, 6])); // Should return concatenated result + }); + + test("should handle Uint8Arrays with non-consecutive values", () => { + const u8a1 = new Uint8Array([0, 255]); + const u8a2 = new Uint8Array([128, 64]); + + const result = fn.u8aConcat(u8a1, u8a2); + expect(result).toEqual(new Uint8Array([0, 255, 128, 64])); // Should concatenate properly + }); +});