]> Lady’s Gitweb - Pisces/commitdiff
Add various is⸺ functions to binary.js
authorLady <redacted>
Sat, 24 Jun 2023 23:47:06 +0000 (16:47 -0700)
committerLady <redacted>
Sat, 24 Jun 2023 23:47:06 +0000 (16:47 -0700)
binary.js
binary.test.js

index dcf32e5f85fc439e8c5e326f17695ed531eb5520..d5642bdc0e8d523e2f5db370ddb749c501879ee3 100644 (file)
--- a/binary.js
+++ b/binary.js
@@ -643,6 +643,18 @@ export const filenameSafeBase64String = ($, ...$s) =>
  */
 export const { isView: isArrayBufferView } = Buffer;
 
+/** Returns whether the provided value is an array buffer. */
+export const isArrayBuffer = ($) => {
+  try {
+    // Try to see if the provided argument has array buffer internal
+    // slots and return true if so.
+    return call(getBufferByteLength, $, []), true;
+  } catch {
+    // The provided argument does not have array buffer internal slots.
+    return false;
+  }
+};
+
 /**
  * Returns whether the provided value is a base16 string.
  *
@@ -697,6 +709,18 @@ export const isBase64 = ($) => {
   }
 };
 
+/** Returns whether the provided value is a data view. */
+export const isDataView = ($) => {
+  try {
+    // Try to see if the provided argument has data view internal slots
+    // and return true if so.
+    return call(getViewBuffer, $, []), true;
+  } catch {
+    // The provided argument does not have data view internal slots.
+    return false;
+  }
+};
+
 /**
  * Returns whether the provided value is a filename‐safe base64 string.
  *
@@ -716,6 +740,18 @@ export const isFilenameSafeBase64 = ($) => {
   }
 };
 
+/** Returns whether the provided value is a typed array. */
+export const isTypedArray = ($) => {
+  try {
+    // Try to see if the provided argument has typed array internal
+    // slots and return true if so.
+    return call(getTypedArrayBuffer, $, []), true;
+  } catch {
+    // The provided argument does not have typed array internal slots.
+    return false;
+  }
+};
+
 /**
  * Returns whether the provided value is a W·R·M·G (Crockford) base32
  * string. Check digits are not supported.
index 1a0920f38a9a43025d64dc2f9173648d0ece4ee2..9d8c16993b8630711d05ebfa006e0e461e4cac40 100644 (file)
@@ -24,11 +24,14 @@ import {
   base64String,
   filenameSafeBase64Binary,
   filenameSafeBase64String,
+  isArrayBuffer,
   isArrayBufferView,
   isBase16,
   isBase32,
   isBase64,
+  isDataView,
   isFilenameSafeBase64,
+  isTypedArray,
   isWRMGBase32,
   toArrayBuffer,
   wrmgBase32Binary,
@@ -580,6 +583,36 @@ describe("filenameSafeBase64String", () => {
   });
 });
 
+describe("isArrayBuffer", () => {
+  it("[[Call]] returns true for array buffers", () => {
+    assertStrictEquals(
+      isArrayBuffer(new ArrayBuffer()),
+      true,
+    );
+  });
+
+  it("[[Call]] returns false for others", () => {
+    [
+      undefined,
+      null,
+      true,
+      Symbol(),
+      27,
+      98n,
+      {},
+      [],
+      () => {},
+      new Proxy({}, {}),
+      "string",
+      new SharedArrayBuffer(),
+      new DataView(new ArrayBuffer()),
+      new Uint8Array(),
+    ].forEach((value) =>
+      assertStrictEquals(isArrayBuffer(value), false)
+    );
+  });
+});
+
 describe("isArrayBufferView", () => {
   it("[[Call]] returns true for data views", () => {
     assertStrictEquals(
@@ -698,6 +731,34 @@ describe("isBase64", () => {
   });
 });
 
+describe("isDataView", () => {
+  it("[[Call]] returns true for data views", () => {
+    assertStrictEquals(
+      isDataView(new DataView(new ArrayBuffer())),
+      true,
+    );
+  });
+
+  it("[[Call]] returns false for others", () => {
+    [
+      undefined,
+      null,
+      true,
+      Symbol(),
+      27,
+      98n,
+      {},
+      [],
+      () => {},
+      new Proxy({}, {}),
+      "string",
+      new ArrayBuffer(),
+      new SharedArrayBuffer(),
+      new Uint8Array(),
+    ].forEach((value) => assertStrictEquals(isDataView(value), false));
+  });
+});
+
 describe("isFilenameSafeBase64", () => {
   it("[[Call]] returns true for filename‐safe base64 strings", () => {
     for (const { base64 } of data.values()) {
@@ -731,6 +792,40 @@ describe("isFilenameSafeBase64", () => {
   });
 });
 
+describe("isTypedArray", () => {
+  it("[[Call]] returns true for typed arrays", () => {
+    assertStrictEquals(
+      isTypedArray(new Uint8Array()),
+      true,
+    );
+    assertStrictEquals(
+      isTypedArray(new BigInt64Array()),
+      true,
+    );
+  });
+
+  it("[[Call]] returns false for others", () => {
+    [
+      undefined,
+      null,
+      true,
+      Symbol(),
+      27,
+      98n,
+      {},
+      [],
+      () => {},
+      new Proxy({}, {}),
+      "string",
+      new ArrayBuffer(),
+      new SharedArrayBuffer(),
+      new DataView(new ArrayBuffer()),
+    ].forEach((value) =>
+      assertStrictEquals(isTypedArray(value), false)
+    );
+  });
+});
+
 describe("isWRMGBase32", () => {
   it("[[Call]] returns true for W·R·M·G base32 strings", () => {
     for (const { wrmg } of data.values()) {
This page took 0.175988 seconds and 4 git commands to generate.