From 5e2b8d3be0d974f0f1e66437633a8006fc265174 Mon Sep 17 00:00:00 2001
From: naskya <m@naskya.net>
Date: Tue, 26 Mar 2024 19:43:11 +0900
Subject: [PATCH] chore: add a function to compare two arrays

---
 packages/backend/src/prelude/array.ts | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/packages/backend/src/prelude/array.ts b/packages/backend/src/prelude/array.ts
index 71a24c89b7..20ffbcd339 100644
--- a/packages/backend/src/prelude/array.ts
+++ b/packages/backend/src/prelude/array.ts
@@ -3,7 +3,6 @@ import type { EndoRelation, Predicate } from "./relation.js";
 /**
  * Count the number of elements that satisfy the predicate
  */
-
 export function countIf<T>(f: Predicate<T>, xs: T[]): number {
 	return xs.filter(f).length;
 }
@@ -60,6 +59,21 @@ export function maximum(xs: number[]): number {
 	return Math.max(...xs);
 }
 
+/**
+ * Returns if two arrays are equal
+ *
+ * examples:
+ *   equal([1, 2, 3], [1, 2, 3]) === true
+ *   equal([1, 2, 3], [1, 3, 2]) === false
+ *   equal([1, 2, 3], [1, 2])    === false
+ */
+export function equal<T>(lhs: T[], rhs: T[]): boolean {
+	return (
+		lhs.length === rhs.length &&
+		lhs.every((value, index) => value === rhs[index])
+	);
+}
+
 /**
  * Splits an array based on the equivalence relation.
  * The concatenation of the result is equal to the argument.