Commit 27b113a5 authored by nanahira's avatar nanahira

improve impl of isEqual

parent 981fbe1c
...@@ -223,11 +223,24 @@ export default class YGOProDeck implements YGOProDeckLike { ...@@ -223,11 +223,24 @@ export default class YGOProDeck implements YGOProDeckLike {
} = {}, } = {},
): boolean { ): boolean {
const { ignoreOrder = false } = options; const { ignoreOrder = false } = options;
const fields = ['main', 'extra', 'side'] as const;
const getField = (o: YGOProDeckLike, field: (typeof fields)[number]) =>
o[field] || [];
if (
fields.some(
(field) =>
getField(this, field).length !== getField(other, field).length,
)
) {
return false;
}
const normalize = (arr: number[]) => const normalize = (arr: number[]) =>
ignoreOrder ? [...arr].sort((a, b) => a - b) : arr; ignoreOrder ? [...arr].sort((a, b) => a - b) : arr;
const fields = ['main', 'extra', 'side'] as const;
return fields.every((field) => return fields.every((field) =>
arrayEquals(normalize(this[field]), normalize(other[field] || [])), arrayEquals(
normalize(getField(this, field)),
normalize(getField(other, field)),
),
); );
} }
} }
...@@ -175,9 +175,5 @@ export class BufferReader extends BufferCursor { ...@@ -175,9 +175,5 @@ export class BufferReader extends BufferCursor {
} }
export const arrayEquals = <T>(a: T[], b: T[]): boolean => { export const arrayEquals = <T>(a: T[], b: T[]): boolean => {
if (a.length !== b.length) return false; return a.length === b.length && a.every((v, i) => v === b[i]);
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment