Skip to content

Commit

Permalink
✨ Add equality of TypedArray
Browse files Browse the repository at this point in the history
Closes #19
  • Loading branch information
TomokiMiyauci committed May 31, 2021
1 parent 495666e commit 439993b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 107 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) )
Expand Down
29 changes: 23 additions & 6 deletions _is.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2021-present the Equal authors. All rights reserved. MIT license.
import {
and,
ifElse,
isArray,
isFunction,
isJSONObject,
Expand Down Expand Up @@ -33,25 +34,41 @@ 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 = <T, U extends T>(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,
isBothObjectExcludeJSON,
isBothPrimitive,
isBothRegExp,
isBothSet,
isBothUint8Array,
isBothUint8ClampedArray,
isBothTypedArray,
isBothURL,
isBothURLSearchParams,
};
33 changes: 18 additions & 15 deletions equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import {
isBothDate,
isBothError,
isBothFunction,
isBothInt8Array,
isBothJSONObject,
isBothMap,
isBothNumber,
isBothObjectExcludeJSON,
isBothPrimitive,
isBothRegExp,
isBothSet,
isBothUint8Array,
isBothUint8ClampedArray,
isBothTypedArray,
isBothURL,
isBothURLSearchParams,
} from "./_is.ts";
Expand Down Expand Up @@ -56,9 +54,7 @@ const equal = <T, U extends T>(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],
Expand Down Expand Up @@ -191,14 +187,22 @@ const equalInt8Array = <T extends Int8Array, U extends T>(
b: U,
): boolean => equalArray([...a], [...b]);

const equalUint8Array = <T extends Uint8Array, U extends T>(
a: T,
b: U,
): boolean => equalArray([...a], [...b]);

const equalUint8ClampedArray = <T extends Uint8ClampedArray, U extends T>(
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 = <T extends URL, U extends T>(a: T, b: U): boolean =>
Expand All @@ -223,8 +227,7 @@ export {
equalObjectExcludeJson,
equalRegExp,
equalSet,
equalUint8Array,
equalUint8ClampedArray,
equalTypedArray,
equalURL,
equalURLSearchParams,
};
129 changes: 44 additions & 85 deletions equal_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import {
equalDate,
equalError,
equalFunction,
equalInt8Array,
equalJsonObject,
equalKeyValueTuple,
equalKeyValueTupleNoOrder,
equalMap,
equalObjectExcludeJson,
equalRegExp,
equalSet,
equalUint8Array,
equalUint8ClampedArray,
equalTypedArray,
equalURL,
equalURLSearchParams,
} from "./equal.ts";
Expand Down Expand Up @@ -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}`,
);
});
});
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 439993b

Please sign in to comment.