Skip to content

Commit

Permalink
✨ Add equality of Uint8ClampedArray
Browse files Browse the repository at this point in the history
Closes #18
  • Loading branch information
TomokiMiyauci committed May 31, 2021
1 parent abbc99a commit 495666e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
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) )
- [`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) )
- [`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
2 changes: 2 additions & 0 deletions _is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const isBothURL = instanceofFactory(URL);
const isBothURLSearchParams = instanceofFactory(URLSearchParams);
const isBothInt8Array = instanceofFactory(Int8Array);
const isBothUint8Array = instanceofFactory(Uint8Array);
const isBothUint8ClampedArray = instanceofFactory(Uint8ClampedArray);

export {
isBothArray,
Expand All @@ -50,6 +51,7 @@ export {
isBothRegExp,
isBothSet,
isBothUint8Array,
isBothUint8ClampedArray,
isBothURL,
isBothURLSearchParams,
};
8 changes: 8 additions & 0 deletions equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
isBothRegExp,
isBothSet,
isBothUint8Array,
isBothUint8ClampedArray,
isBothURL,
isBothURLSearchParams,
} from "./_is.ts";
Expand Down Expand Up @@ -57,6 +58,7 @@ const equal = <T, U extends T>(a: T, b: U): boolean => {
[isBothSet, equalSet],
[isBothInt8Array, equalInt8Array],
[isBothUint8Array, equalUint8Array],
[isBothUint8ClampedArray, equalUint8ClampedArray],
[isBothURL, equalURL],
[isBothURLSearchParams, equalURLSearchParams],
[isBothObjectExcludeJSON, equalObjectExcludeJson],
Expand Down Expand Up @@ -194,6 +196,11 @@ const equalUint8Array = <T extends Uint8Array, U extends T>(
b: U,
): boolean => equalArray([...a], [...b]);

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

const equalURL = <T extends URL, U extends T>(a: T, b: U): boolean =>
a.toString() === b.toString();
const equalURLSearchParams = <T extends URLSearchParams, U extends T>(
Expand All @@ -217,6 +224,7 @@ export {
equalRegExp,
equalSet,
equalUint8Array,
equalUint8ClampedArray,
equalURL,
equalURLSearchParams,
};
44 changes: 44 additions & 0 deletions equal_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
equalRegExp,
equalSet,
equalUint8Array,
equalUint8ClampedArray,
equalURL,
equalURLSearchParams,
} from "./equal.ts";
Expand Down Expand Up @@ -310,6 +311,48 @@ Deno.test("equalUint8Array", () => {
});
});

Deno.test("equalUint8ClampedArray", () => {
const table: [Uint8ClampedArray, Uint8ClampedArray, boolean][] = [
[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,
],
];
table.forEach(([a, b, expected]) => {
assertEquals(
equalUint8ClampedArray(a, b),
expected,
`equalUint8ClampedArray(${a}, ${b}) -> ${expected}`,
);
});
});

Deno.test("equalRegExp", () => {
const table: [RegExp, RegExp, boolean][] = [
[/s/, /s/, true],
Expand Down Expand Up @@ -1012,6 +1055,7 @@ Deno.test("equal", () => {
],
[new Int8Array(), new Int8Array(), true],
[new Uint8Array(), new Uint8Array(), true],
[new Uint8ClampedArray(), new Uint8ClampedArray(), true],
[new Uint16Array(), new Uint16Array(), false],
];
table.forEach(([a, b, expected]) => {
Expand Down

0 comments on commit 495666e

Please sign in to comment.