Skip to content

Commit

Permalink
fix: Accept type string for location and address (#1122)
Browse files Browse the repository at this point in the history
  • Loading branch information
nandenjin committed Dec 3, 2022
1 parent 94abb9e commit 0dbcef4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
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

0 comments on commit 0dbcef4

Please sign in to comment.