Skip to content

Commit 501da09

Browse files
committed
add arrayEquals to utils
1 parent b7c6ad8 commit 501da09

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

packages/utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solid-primitives/utils",
3-
"version": "2.1.0",
3+
"version": "2.1.1",
44
"description": "A bunch of reactive utility types and functions, for building primitives with Solid.js",
55
"author": "Damian Tarnawski @thetarnav <gthetarnav@gmail.com>",
66
"license": "MIT",

packages/utils/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ export function isObject(value: any): value is AnyObject {
4747

4848
export const compare = (a: any, b: any): number => (a < b ? -1 : a > b ? 1 : 0);
4949

50+
/**
51+
* Check shallow array equality
52+
*/
53+
export const arrayEquals = (a: readonly unknown[], b: readonly unknown[]): boolean =>
54+
a === b || (a.length === b.length && a.every((e, i) => e === b[i]));
55+
5056
/**
5157
* Returns a function that will call all functions in the order they were chained with the same arguments.
5258
*/

packages/utils/test/index.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createComputed, createRoot } from "solid-js";
2-
import { createStaticStore, handleDiffArray } from "../src";
2+
import { createStaticStore, handleDiffArray, arrayEquals } from "../src";
33
import { suite } from "uvu";
44
import * as assert from "uvu/assert";
55

@@ -145,3 +145,19 @@ da("calls callbacks for added and removed items", () => {
145145
});
146146

147147
da.run();
148+
149+
const ae = suite("arrayEquals");
150+
151+
ae("arrayEquals", () => {
152+
const _1: any[] = [];
153+
assert.ok(arrayEquals(_1, _1));
154+
assert.ok(arrayEquals(_1, []));
155+
assert.ok(arrayEquals([1, 2, 3], [1, 2, 3]));
156+
assert.ok(arrayEquals([1, 2, _1], [1, 2, _1]));
157+
158+
assert.not.ok(arrayEquals([1, 2, 3], [1, 2, 3, 4]));
159+
assert.not.ok(arrayEquals([1, 2, 3], [1, 0, 3]));
160+
assert.not.ok(arrayEquals([1, 2, _1], [1, 2, []]));
161+
});
162+
163+
ae.run();

0 commit comments

Comments
 (0)