Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add get link thumbnail function #582

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/item/linkItem/linkItem.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, expect, it } from 'vitest';

import { ItemType } from '../itemType.js';
import { buildLinkExtra, getLinkThumbnailUrl } from './linkItem.js';

describe('getLinkThumbnailUrl', () => {
it('returns thumbnail over icon', () => {
pyphilia marked this conversation as resolved.
Show resolved Hide resolved
const extra = buildLinkExtra({
url: 'url',
thumbnails: ['thumbnai'],
icons: ['icon'],
});
expect(getLinkThumbnailUrl(extra)).to.eq(
extra[ItemType.LINK].thumbnails![0],
);
});

it('return icon for empty thumbnail', () => {
const extra = buildLinkExtra({
url: 'url',
icons: ['icon'],
});
expect(getLinkThumbnailUrl(extra)).to.eq(extra[ItemType.LINK].icons![0]);
const extra1 = {
[ItemType.LINK]: {
url: 'url',
thumbnails: [],
icons: ['icon'],
},
};
expect(getLinkThumbnailUrl(extra1)).to.eq(extra[ItemType.LINK].icons![0]);
});

it('returns undefined when nothing is defined', () => {
const extra = buildLinkExtra({
url: 'string',
thumbnails: [],
icons: [],
});
expect(getLinkThumbnailUrl(extra)).to.be.undefined;
const extra1 = buildLinkExtra({ url: 'string', thumbnails: [] });
expect(getLinkThumbnailUrl(extra1)).to.be.undefined;
const extra2 = buildLinkExtra({ url: 'string' });
expect(getLinkThumbnailUrl(extra2)).to.be.undefined;
const extra3 = buildLinkExtra({ url: 'string', icons: [] });
expect(getLinkThumbnailUrl(extra3)).to.be.undefined;
});
});
14 changes: 14 additions & 0 deletions src/item/linkItem/linkItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ export const getLinkExtra = <U extends LinkItemExtra>(
extra: U,
): U[typeof ItemType.LINK] => extra[ItemType.LINK];

export const getLinkThumbnailUrl = <U extends LinkItemExtra>(
extra: U,
): string | undefined => {
const { thumbnails, icons } = getLinkExtra(extra);

if (thumbnails && thumbnails.length > 0) {
return thumbnails[0];
}
if (icons && icons.length > 0) {
return icons[0];
}
return;
};

export const buildLinkExtra = (
extra: LinkItemExtraProperties,
): LinkItemExtra => ({
Expand Down