From 439993b076f29fa1d9dc33275733df73c36f8f4f Mon Sep 17 00:00:00 2001 From: TomokiMiyauci Date: Mon, 31 May 2021 18:07:39 +0900 Subject: [PATCH] :sparkles: Add equality of TypedArray Closes #19 --- README.md | 2 +- _is.ts | 29 +++++++++--- equal.ts | 33 +++++++------ equal_test.ts | 129 +++++++++++++++++--------------------------------- 4 files changed, 86 insertions(+), 107 deletions(-) diff --git a/README.md b/README.md index 5ece863..9dd28fa 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,7 @@ equal(+0, -0) // true The following objects work correctly. - [`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) -- [`Typed Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays) ( [Int8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array), [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array), [Uint8ClampedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray) ) +- [`Typed Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays) ( [`Int8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array), [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array), [`Uint8ClampedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray), [`Int16Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int16Array), [`Uint16Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array), [`Int32Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array), [`Uint32Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array), [`Float32Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array), [`Float64Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array), [`BigInt64Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array), [`BigUint64Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigUint64Array) ) - [`Object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) - [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) - [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) ( [`EvalError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError), [`RangeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError), [`ReferenceError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError), [`SyntaxError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError), [`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError), [`URIError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/URIError), [`AggregateError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) ) diff --git a/_is.ts b/_is.ts index b3273c2..4457e71 100644 --- a/_is.ts +++ b/_is.ts @@ -1,6 +1,7 @@ // Copyright 2021-present the Equal authors. All rights reserved. MIT license. import { and, + ifElse, isArray, isFunction, isJSONObject, @@ -33,16 +34,33 @@ const isBothMap = instanceofFactory(Map); const isBothSet = instanceofFactory(Set); const isBothURL = instanceofFactory(URL); const isBothURLSearchParams = instanceofFactory(URLSearchParams); -const isBothInt8Array = instanceofFactory(Int8Array); -const isBothUint8Array = instanceofFactory(Uint8Array); -const isBothUint8ClampedArray = instanceofFactory(Uint8ClampedArray); +const isBothTypedArray = (a: T, b: U): [boolean, boolean] => { + const result = [ + Int8Array, + Uint8Array, + Uint8ClampedArray, + Int16Array, + Uint16Array, + Int32Array, + Uint32Array, + Float32Array, + Float64Array, + BigInt64Array, + BigUint64Array, + ] + .some((obj) => { + const [f1, f2] = instanceofFactory(obj)(a, b); + return and(f1, f2); + }); + + return ifElse(result, [true, true], [false, false]); +}; export { isBothArray, isBothDate, isBothError, isBothFunction, - isBothInt8Array, isBothJSONObject, isBothMap, isBothNumber, @@ -50,8 +68,7 @@ export { isBothPrimitive, isBothRegExp, isBothSet, - isBothUint8Array, - isBothUint8ClampedArray, + isBothTypedArray, isBothURL, isBothURLSearchParams, }; diff --git a/equal.ts b/equal.ts index 8b2b9dd..385dec9 100644 --- a/equal.ts +++ b/equal.ts @@ -5,7 +5,6 @@ import { isBothDate, isBothError, isBothFunction, - isBothInt8Array, isBothJSONObject, isBothMap, isBothNumber, @@ -13,8 +12,7 @@ import { isBothPrimitive, isBothRegExp, isBothSet, - isBothUint8Array, - isBothUint8ClampedArray, + isBothTypedArray, isBothURL, isBothURLSearchParams, } from "./_is.ts"; @@ -56,9 +54,7 @@ const equal = (a: T, b: U): boolean => { [isBothError, equalError], [isBothMap, equalMap], [isBothSet, equalSet], - [isBothInt8Array, equalInt8Array], - [isBothUint8Array, equalUint8Array], - [isBothUint8ClampedArray, equalUint8ClampedArray], + [isBothTypedArray, equalTypedArray], [isBothURL, equalURL], [isBothURLSearchParams, equalURLSearchParams], [isBothObjectExcludeJSON, equalObjectExcludeJson], @@ -191,14 +187,22 @@ const equalInt8Array = ( b: U, ): boolean => equalArray([...a], [...b]); -const equalUint8Array = ( - a: T, - b: U, -): boolean => equalArray([...a], [...b]); - -const equalUint8ClampedArray = ( +const equalTypedArray = < + T extends + | Int8Array + | Uint8Array + | Uint8ClampedArray + | Int16Array + | Uint16Array + | Int32Array + | Uint32Array + | Float32Array + | Float64Array + | BigInt64Array + | BigUint64Array, +>( a: T, - b: U, + b: T, ): boolean => equalArray([...a], [...b]); const equalURL = (a: T, b: U): boolean => @@ -223,8 +227,7 @@ export { equalObjectExcludeJson, equalRegExp, equalSet, - equalUint8Array, - equalUint8ClampedArray, + equalTypedArray, equalURL, equalURLSearchParams, }; diff --git a/equal_test.ts b/equal_test.ts index c7561e5..abeadcf 100644 --- a/equal_test.ts +++ b/equal_test.ts @@ -7,7 +7,6 @@ import { equalDate, equalError, equalFunction, - equalInt8Array, equalJsonObject, equalKeyValueTuple, equalKeyValueTupleNoOrder, @@ -15,8 +14,7 @@ import { equalObjectExcludeJson, equalRegExp, equalSet, - equalUint8Array, - equalUint8ClampedArray, + equalTypedArray, equalURL, equalURLSearchParams, } from "./equal.ts"; @@ -261,94 +259,48 @@ Deno.test("equalArray", () => { }); }); -Deno.test("equalInt8Array", () => { - const table: [Int8Array, Int8Array, boolean][] = [ +Deno.test("equalTypedArray", () => { + type ArrayLike = + | Int8Array + | Uint8Array + | Uint8ClampedArray + | Int16Array + | Uint16Array + | Int32Array + | Uint32Array + | Float32Array + | Float64Array + | BigInt64Array + | BigUint64Array; + const table: [ArrayLike, ArrayLike, boolean][] = [ [new Int8Array(), new Int8Array(), true], - [new Int8Array([]), new Int8Array(), true], - [new Int8Array(), new Int8Array([]), true], - [new Int8Array([]), new Int8Array([]), true], - [new Int8Array(0), new Int8Array(0), true], - [new Int8Array(0), new Int8Array(), true], - [new Int8Array(), new Int8Array(0), true], - [new Int8Array(0), new Int8Array(1), false], - [new Int8Array(1), new Int8Array(1), true], - [new Int8Array([1, 2, 3]), new Int8Array([1, 2, 3]), true], - [new Int8Array([127, -128, 0]), new Int8Array([127, -128, 0]), true], - [new Int8Array([127, -128, 0]), new Int8Array([127, -128, 1]), false], - [new Int8Array([127, -128, 0]), new Int8Array([127, -128, 0, 1]), false], - ]; - table.forEach(([a, b, expected]) => { - assertEquals( - equalInt8Array(a, b), - expected, - `equalInt8Array(${a}, ${b}) -> ${expected}`, - ); - }); -}); - -Deno.test("equalUint8Array", () => { - const table: [Uint8Array, Uint8Array, boolean][] = [ + [new Int8Array([21, 31]), new Int8Array([21, 31]), true], [new Uint8Array(), new Uint8Array(), true], - [new Uint8Array([]), new Uint8Array(), true], - [new Uint8Array(), new Uint8Array([]), true], - [new Uint8Array([]), new Uint8Array([]), true], - [new Uint8Array(0), new Uint8Array(0), true], - [new Uint8Array(0), new Uint8Array(), true], - [new Uint8Array(), new Uint8Array(0), true], - [new Uint8Array(0), new Uint8Array(1), false], - [new Uint8Array(1), new Uint8Array(1), true], - [new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3]), true], - [new Uint8Array([255, -128, 0]), new Uint8Array([255, -128, 0]), true], - [new Uint8Array([255, -128, 0]), new Uint8Array([127, -128, 1]), false], - [new Uint8Array([255, 0]), new Uint8Array([255, 0, 0]), false], - ]; - table.forEach(([a, b, expected]) => { - assertEquals( - equalUint8Array(a, b), - expected, - `equalUint8Array(${a}, ${b}) -> ${expected}`, - ); - }); -}); - -Deno.test("equalUint8ClampedArray", () => { - const table: [Uint8ClampedArray, Uint8ClampedArray, boolean][] = [ + [new Uint8Array([21, 31]), new Uint8Array([21, 31]), true], [new Uint8ClampedArray(), new Uint8ClampedArray(), true], - [new Uint8ClampedArray([]), new Uint8ClampedArray(), true], - [new Uint8ClampedArray(), new Uint8ClampedArray([]), true], - [new Uint8ClampedArray([]), new Uint8ClampedArray([]), true], - [new Uint8ClampedArray(0), new Uint8ClampedArray(0), true], - [new Uint8ClampedArray(0), new Uint8ClampedArray(), true], - [new Uint8ClampedArray(), new Uint8ClampedArray(0), true], - [new Uint8ClampedArray(0), new Uint8ClampedArray(1), false], - [new Uint8ClampedArray(1), new Uint8ClampedArray(1), true], - [new Uint8ClampedArray([1, 2, 3]), new Uint8ClampedArray([1, 2, 3]), true], - [ - new Uint8ClampedArray([255, -128, 0]), - new Uint8ClampedArray([255, -128, 0]), - true, - ], - [ - new Uint8ClampedArray([256]), - new Uint8ClampedArray([255]), - true, - ], - [ - new Uint8ClampedArray([255, -128, 0]), - new Uint8ClampedArray([127, -128, 1]), - false, - ], - [ - new Uint8ClampedArray([255, 0]), - new Uint8ClampedArray([255, 0, 0]), - false, - ], + [new Uint8ClampedArray([21, 31]), new Uint8ClampedArray([21, 31]), true], + [new Int16Array(), new Int16Array(), true], + [new Int16Array([21, 31]), new Int16Array([21, 31]), true], + [new Uint16Array(), new Uint16Array(), true], + [new Uint16Array([21, 31]), new Uint16Array([21, 31]), true], + [new Int32Array(), new Int32Array(), true], + [new Int32Array([21, 31]), new Int32Array([21, 31]), true], + [new Uint32Array(), new Uint32Array(), true], + [new Uint32Array([21, 31]), new Uint32Array([21, 31]), true], + [new Float32Array(), new Float32Array(), true], + [new Float32Array([21, 31]), new Float32Array([21, 31]), true], + [new Float64Array(), new Float64Array(), true], + [new Float64Array([21, 31]), new Float64Array([21, 31]), true], + [new BigInt64Array(), new BigInt64Array(), true], + [new BigInt64Array([21n, 31n]), new BigInt64Array([21n, 31n]), true], + [new BigUint64Array(), new BigUint64Array(), true], + [new BigUint64Array([0n, 31n]), new BigUint64Array([0n, 31n]), true], ]; table.forEach(([a, b, expected]) => { assertEquals( - equalUint8ClampedArray(a, b), + equalTypedArray(a, b), expected, - `equalUint8ClampedArray(${a}, ${b}) -> ${expected}`, + `equalTypedArray(${a}, ${b}) -> ${expected}`, ); }); }); @@ -1056,7 +1008,14 @@ Deno.test("equal", () => { [new Int8Array(), new Int8Array(), true], [new Uint8Array(), new Uint8Array(), true], [new Uint8ClampedArray(), new Uint8ClampedArray(), true], - [new Uint16Array(), new Uint16Array(), false], + [new Int16Array(), new Int16Array(), true], + [new Uint16Array(), new Uint16Array(), true], + [new Int32Array(), new Int32Array(), true], + [new Uint32Array(), new Uint32Array(), true], + [new Float32Array(), new Float32Array(), true], + [new Float64Array(), new Float64Array(), true], + [new BigInt64Array(), new BigInt64Array(), true], + [new BigUint64Array(), new BigUint64Array(), true], ]; table.forEach(([a, b, expected]) => { assertEquals(