Skip to content

Commit

Permalink
Remove more redundant v1 code
Browse files Browse the repository at this point in the history
  • Loading branch information
drzax committed Nov 17, 2023
1 parent cb4fff9 commit 2a0a3d4
Showing 1 changed file with 0 additions and 91 deletions.
91 changes: 0 additions & 91 deletions src/lib/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,98 +135,7 @@ function getImages(doc: unknown, targetWidths: number[] = DEFAULT_TARGET_WIDTHS)
};
}

if (isLegacyTerminusImageDocument(doc)) {
const images = doc.media.image.primary.complete;
const { title, alt, id: cmid, caption, attribution, canonicalURL } = doc;
return {
alt,
cmid,
title,
caption,
attribution,
canonicalURL,
renditions: images.map(({ ratio, width, height, url }) => {
return {
ratio: ratio || getRatioString(width, height),
width,
height,
url,
isUndersizedBinary: false
};
})
};
}

throw new Error('The document passed was not a valid terminus image document');
}

/*
* ========================================================
* Stuff below here is transitional and can be removed once
* the transition to CM10 and Terminus v2 is complete.
* ========================================================
*/
type LegacyTerminusImageDocument = {
id: string;
title?: string;
alt?: string;
caption?: string;
attribution?: string;
canonicalURL: string;
media: {
image: {
primary: {
complete: LegacyTerminusImageDocumentImages;
};
};
};
};

type LegacyTerminusImageDocumentImages = LegacyTerminusImageDocumentImage[];
type LegacyTerminusImageDocumentImage = {
width: number;
height: number;
url: string;
ratio?: string;
};

const greatestCommonFactor = (a: number, b: number) => {
while (a) {
const t = a;
a = b % a;
b = t;
}
return b;
};

const getRatioString = (width: number, height: number) => {
const gcf = greatestCommonFactor(width, height);
return `${width / gcf}x${height / gcf}`;
};

const isLegacyTerminusImageDocument = (obj: unknown): obj is LegacyTerminusImageDocument => {
if (!isObject(obj)) return false;
if (obj.docType !== 'Image' && obj.docType !== 'ImageProxy' && obj.docType !== 'CustomImage') return false;
if (typeof obj.id !== 'string') return false;
if (!isObject(obj.media)) return false;
if (!isObject(obj.media.image)) return false;
if (!isObject(obj.media.image.primary)) return false;
if (!isLegacyTerminusImageDocumentImages(obj.media.image.primary.complete)) return false;
return true;
};

const isLegacyTerminusImageDocumentImage = (obj: unknown): obj is LegacyTerminusImageDocumentImage => {
if (!isObject(obj)) return false;
if (typeof obj.width !== 'number') return false;
if (typeof obj.height !== 'number') return false;
if (typeof obj.url !== 'string') return false;
return true;
};

const isLegacyTerminusImageDocumentImages = (obj: unknown): obj is LegacyTerminusImageDocumentImages => {
if (!Array.isArray(obj)) return false;
if (!obj.every(isLegacyTerminusImageDocumentImage)) return false;
return true;
};

export { getImages };

0 comments on commit 2a0a3d4

Please sign in to comment.