Skip to content

Commit

Permalink
feat: add utils
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelodelain committed Mar 15, 2024
1 parent 8e82d9d commit bab29c3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { getResponseAlternateLinks } from './roadiz/get-response-alternate-links
export * from './roadiz/document'
export * from './roadiz/entity'
export * from './roadiz/node-sources'
export * from './roadiz/embed'
35 changes: 35 additions & 0 deletions utils/roadiz/embed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export function getEmbedSrc(id: string, platform: string, params: Record<string, string> = {}): string | undefined {
if (platform === 'vimeo') return getVimeoEmbedSrc(id, params)
if (platform === 'youtube') return getYouTubeEmbedSrc(id, params)
if (platform === 'deezer') return getDeezerEmbedSrc(id, params)
if (platform === 'spotify') return getSpotifyEmbedSrc(id, params)
if (platform === 'soundcloud') return getSoundcloudEmbedSrc(id, params)
}

export function getVimeoEmbedSrc(id: string, params: Record<string, string> = {}): string {
return getEmbedUrl(`https://player.vimeo.com/video/${id}`, params)
}

export function getYouTubeEmbedSrc(id: string, params: Record<string, string> = {}): string {
return getEmbedUrl(`https://www.youtube-nocookie.com/embed/${id}`, params)
}

export function getDeezerEmbedSrc(id: string, params: Record<string, string> = {}): string {
return getEmbedUrl(`https://widget.deezer.com/widget/light/${id}`, params)
}

export function getSpotifyEmbedSrc(id: string, params: Record<string, string> = {}): string {
return getEmbedUrl(`https://open.spotify.com/embed/${id}`, params)
}

export function getSoundcloudEmbedSrc(id: string, params: Record<string, string> = {}): string {
return getEmbedUrl(`https://w.soundcloud.com/player/?url=${id}`, params)
}

export function getEmbedUrl(url: string, params: Record<string, string> = {}) {
const encodedParams = encodeUrlParams(params)

if (encodedParams.length) url += '?' + encodedParams

return url
}
8 changes: 8 additions & 0 deletions utils/to-boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function toBoolean(value: unknown): boolean {
return typeof value === 'string'
? (value as string).toLowerCase() === 'true' ||
(value as string).toLowerCase() === 'yes' ||
(value as string).toLowerCase() === 'on' ||
(value as string).toLowerCase() === '1'
: !!value
}
5 changes: 5 additions & 0 deletions utils/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function encodeUrlParams(params: object): string {
return Object.entries(params)
.map((kv) => kv.map(encodeURIComponent).join('='))
.join('&')
}

0 comments on commit bab29c3

Please sign in to comment.