};
const { exec: reExec } = rePrototype;
+const { slice: bufferSlice } = bufferPrototype;
const getBufferByteLength =
Object.getOwnPropertyDescriptor(bufferPrototype, "byteLength").get;
+const { slice: sharedBufferSlice } = sharedBufferPrototype;
const getSharedBufferByteLength =
Object.getOwnPropertyDescriptor(sharedBufferPrototype, "byteLength")
.get;
"",
);
+/**
+ * Returns a slice of the provided value according to the algorithm of
+ * `ArrayBuffer::slice` (or `SharedArrayBuffer::slice`).
+ *
+ * ☡ This function throws if the provided value is not an array buffer.
+ */
+export const arrayBufferSlice = ($, start, end, ...args) =>
+ call(
+ isSharedArrayBuffer($) ? sharedBufferSlice : bufferSlice,
+ $,
+ [
+ start,
+ end,
+ ...objectCreate(
+ argumentIterablePrototype,
+ { args: { value: args } },
+ ),
+ ],
+ );
+
/**
* Returns an ArrayBuffer generated from the provided base16 string.
*
it,
} from "./dev-deps.js";
import {
+ arrayBufferSlice,
base16Binary,
base16String,
base32Binary,
}],
]);
+describe("arrayBufferSlice", () => {
+ it("[[Call]] slices an `ArrayBuffer`", () => {
+ const baseBuffer = Uint8Array.from([2, 3, 1, 9, 8, 5]).buffer;
+ assertEquals(
+ new Uint8Array(arrayBufferSlice(baseBuffer, 1, 4)),
+ Uint8Array.from([3, 1, 9]),
+ );
+ });
+
+ it("[[Call]] slices an `SharedArrayBuffer`", () => {
+ const baseBuffer = new SharedArrayBuffer(6);
+ new Uint8Array(baseBuffer).set([2, 3, 1, 9, 8, 5], 0);
+ assertEquals(
+ new Uint8Array(arrayBufferSlice(baseBuffer, 1, 4)),
+ Uint8Array.from([3, 1, 9]),
+ );
+ });
+
+ it("[[Call]] throws for others", () => {
+ [
+ undefined,
+ null,
+ true,
+ Symbol(),
+ 27,
+ 98n,
+ {},
+ [],
+ () => {},
+ new Proxy({}, {}),
+ "string",
+ new DataView(new ArrayBuffer()),
+ new Uint8Array(),
+ ].forEach((value) =>
+ assertThrows(() => arrayBufferSlice(value, 0, 0))
+ );
+ });
+});
+
describe("base16Binary", () => {
it("[[Call]] returns the correct data", () => {
assertEquals(