Skip to content

Commit

Permalink
feat(refactor): Misc utils improvements (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
rossbulat authored Nov 4, 2024
1 parent aaf213e commit a31932f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 27 deletions.
2 changes: 1 addition & 1 deletion library/utils/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
24 changes: 24 additions & 0 deletions library/utils/src/convert.ts
Original file line number Diff line number Diff line change
@@ -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;
};
1 change: 1 addition & 0 deletions library/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
SPDX-License-Identifier: GPL-3.0-only */

export * from "./base";
export * from "./convert";
export * from "./unit";
27 changes: 1 addition & 26 deletions library/utils/src/unit.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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.
Expand Down
56 changes: 56 additions & 0 deletions library/utils/tests/convert.test.ts
Original file line number Diff line number Diff line change
@@ -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
});
});

0 comments on commit a31932f

Please sign in to comment.