-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(refactor): Misc utils improvements (#142)
- Loading branch information
Showing
5 changed files
with
83 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); |