summaryrefslogblamecommitdiffstatshomepage
path: root/frontend/src/gql/Utils.ts
blob: dd21bbeb6018ac49509b89501344fcf283947a0c (plain) (tree)









































































                                                                                                      
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)
	);
}