From: Lady Date: Sat, 24 Jun 2023 23:47:06 +0000 (-0700) Subject: Add various is⸺ functions to binary.js X-Git-Tag: 0.4.0~10 X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/8e60c62bf7309b49422ee0dd2a58ea94bbf7fb55?ds=sidebyside Add various is⸺ functions to binary.js --- diff --git a/binary.js b/binary.js index dcf32e5..d5642bd 100644 --- 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. diff --git a/binary.test.js b/binary.test.js index 1a0920f..9d8c169 100644 --- a/binary.test.js +++ b/binary.test.js @@ -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()) {