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

fix: Accept type string for location and address #1122

Merged
merged 1 commit into from Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/types.ts
Expand Up @@ -95,7 +95,7 @@ export interface VirtualLocation {
url: string;
}

export type Location = Place | VirtualLocation;
export type Location = string | Place | VirtualLocation;

export type EventStatus =
| 'EventCancelled'
Expand Down Expand Up @@ -162,13 +162,15 @@ export interface OpenGraphMedia {
secureUrl?: string;
}

export interface Address {
streetAddress: string;
addressLocality: string;
addressRegion?: string;
postalCode: string;
addressCountry: string;
}
export type Address =
| string
| {
streetAddress: string;
addressLocality: string;
addressRegion?: string;
postalCode: string;
addressCountry: string;
};

export interface Video {
name: string;
Expand Down
4 changes: 4 additions & 0 deletions src/utils/schema/__tests__/setAddress.test.ts
Expand Up @@ -22,6 +22,10 @@ describe('setAddress', () => {
expect(setAddress(undefined)).toBeUndefined();
});

test('should accepts simple string and returns it', () => {
expect(setAddress('AddressText')).toBe('AddressText');
});

test('single address returns correctly', () => {
const data = setAddress(addressOne);

Expand Down
37 changes: 37 additions & 0 deletions src/utils/schema/__tests__/setLocation.test.ts
@@ -0,0 +1,37 @@
import { Place, VirtualLocation } from 'src/types';
import { setLocation } from '../setLocation';

const virtualLocation: VirtualLocation = {
name: 'Virtual Location',
sameAs: 'https://example.com',
url: 'https://example.com',
};

const place: Place = {
name: 'Place',
address: 'Address String',
};

describe('setLocation', () => {
test('should return undefined if location is undefined', () => {
expect(setLocation(undefined)).toBeUndefined();
});

test('should accepts simple string and returns it', () => {
expect(setLocation('LocationText')).toBe('LocationText');
});

test('returns VirtualLocation correctly', () => {
expect(setLocation(virtualLocation)).toMatchObject({
'@type': 'VirtualLocation',
...virtualLocation,
});
});

test('returns Place correctly', () => {
expect(setLocation(place)).toMatchObject({
'@type': 'Place',
...place,
});
});
});
3 changes: 2 additions & 1 deletion src/utils/schema/setAddress.ts
Expand Up @@ -2,7 +2,7 @@ import { Address } from 'src/types';

export function setAddress(address?: Address | Address[]) {
if (!address) return undefined;

if (!Array.isArray(address)) return toPostalAddress(address);

// If array of one address, replace with single address
Expand All @@ -13,5 +13,6 @@ export function setAddress(address?: Address | Address[]) {
}

function toPostalAddress(address: Address) {
if (typeof address === 'string') return address;
return { '@type': 'PostalAddress', ...address };
}
6 changes: 5 additions & 1 deletion src/utils/schema/setLocation.ts
@@ -1,11 +1,15 @@
import { Location, Place, VirtualLocation } from 'src/types';
import { setAddress } from './setAddress';

export function setLocation(location: Location) {
export function setLocation(location?: Location) {
if (!location) {
return undefined;
}

if (typeof location === 'string') {
return location;
}

if ('url' in location) {
return setVirtualLocation(location);
} else {
Expand Down