From d1d654ebac2d51e3841675faeb56480e440f622f Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Tue, 5 Mar 2024 18:08:09 +0100 Subject: Initial commit --- frontend/src/gql/Utils.ts | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 frontend/src/gql/Utils.ts (limited to 'frontend/src/gql/Utils.ts') diff --git a/frontend/src/gql/Utils.ts b/frontend/src/gql/Utils.ts new file mode 100644 index 0000000..dd21bbe --- /dev/null +++ b/frontend/src/gql/Utils.ts @@ -0,0 +1,74 @@ +import equal from 'fast-deep-equal'; +import * as gql from './graphql'; + +export type OmitIdentifiers = Omit; +export type RequiredName = 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) + ); +} -- cgit v1.2.3-2-gb3c3