summaryrefslogtreecommitdiffstatshomepage
path: root/frontend/src/gql/Utils.ts
blob: dd21bbeb6018ac49509b89501344fcf283947a0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import equal from 'fast-deep-equal';
import * as gql from './graphql';

export type OmitIdentifiers<T> = Omit<T, 'id' | '__typename'>;
export type RequiredName<T> = T & { name: string };

export function isSuccess(object: any): object is gql.Success {
	if (object.__typename === undefined) {
		return false;
	}

	return object.__typename.endsWith('Success') && (object as gql.Success).message !== undefined;
}

export function isError(object: any): object is gql.Error {
	if (object.__typename === undefined) {
		return false;
	}
	return object.__typename.endsWith('Error') && (object as gql.Error).message !== undefined;
}

type Item = {
	id: number | string;
	name: string;
};

export function itemEquals(a: Item, b: Item) {
	return a.name == b.name;
}

function assocEquals(as: Item[], bs: Item[]) {
	return equal(
		as.map((a) => a.id),
		bs.map((b) => b.id)
	);
}

function stringEquals(a: string | null | undefined, b: string | null | undefined) {
	return (a ? a : null) == (b ? b : null);
}

export function tagEquals(a: gql.FullTag, b: gql.FullTag) {
	return (
		itemEquals(a, b) &&
		stringEquals(a.description, b.description) &&
		assocEquals(a.namespaces, b.namespaces)
	);
}

export function comicEquals(
	a: gql.FullComicFragment | undefined,
	b: gql.FullComicFragment | undefined
) {
	if (a === undefined) return b === undefined;
	if (b === undefined) return a === undefined;

	return (
		stringEquals(a.title, b.title) &&
		stringEquals(a.originalTitle, b.originalTitle) &&
		stringEquals(a.url, b.url) &&
		stringEquals(a.date, b.date) &&
		a.category == b.category &&
		a.rating == b.rating &&
		a.censorship == b.censorship &&
		a.language == b.language &&
		a.direction == b.direction &&
		a.layout == b.layout &&
		assocEquals(a.artists, b.artists) &&
		assocEquals(a.circles, b.circles) &&
		assocEquals(a.characters, b.characters) &&
		assocEquals(a.tags, b.tags) &&
		assocEquals(a.worlds, b.worlds)
	);
}