Skip to content

Commit

Permalink
chore: rename and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Aug 19, 2024
1 parent 34dcf3a commit c640e2d
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/timeline-state-resolver-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export enum ActionExecutionResultCode {
}

/** This resolves to a string, where parts can be defined by the datastore */
export interface TemplateString{
export interface TemplateString {
/** The string template. Example: "http://google.com?q={{searchString}}" */
key: string
/** Values for the arguments in the key string. Example: { searchString: "TSR" } */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeviceType, StringInterpolation } from '..'
import { DeviceType, TemplateString } from '..'

export enum TimelineContentTypeCasparCg { // CasparCG-state
MEDIA = 'media',
Expand Down Expand Up @@ -122,7 +122,7 @@ export interface TimelineContentCCGInput extends TimelineContentCasparCGBase, Ti
export interface TimelineContentCCGHTMLPage extends TimelineContentCasparCGBase, TimelineContentCCGProducerBase {
type: TimelineContentTypeCasparCg.HTMLPAGE
/** The URL to load */
url: string | StringInterpolation
url: string | TemplateString
}
export interface TimelineContentCCGTemplate extends TimelineContentCasparCGBase, TimelineContentCCGProducerBase {
type: TimelineContentTypeCasparCg.TEMPLATE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { DeviceType, HTTPSendCommandContent, StringInterpolation } from '..'
import { DeviceType, HTTPSendCommandContent, TemplateString } from '..'

export type TimelineContentHTTPSendAny = TimelineContentHTTPRequest
export interface TimelineContentHTTPSendBase {
deviceType: DeviceType.HTTPSEND
}

export interface HTTPSendCommandContentExt extends Omit<HTTPSendCommandContent, 'url'> {
url: string | StringInterpolation
url: string | TemplateString
}

export type TimelineContentHTTPRequest = TimelineContentHTTPSendBase & HTTPSendCommandContentExt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeviceType, StringInterpolation } from '..'
import { DeviceType, TemplateString } from '..'

export enum TimelineContentTypeSofieChef {
URL = 'url',
Expand All @@ -13,5 +13,5 @@ export interface TimelineContentSofieChef {
export interface TimelineContentSofieChefScene extends TimelineContentSofieChef {
type: TimelineContentTypeSofieChef.URL

url: string | StringInterpolation
url: string | TemplateString
}
34 changes: 34 additions & 0 deletions packages/timeline-state-resolver/src/__tests__/lib.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { interpolateTemplateString, interpolateTemplateStringIfNeeded } from '../lib'

describe('interpolateTemplateString', () => {
test('basic input', () => {
expect(interpolateTemplateString('Hello there {{name}}', { name: 'Bob' })).toEqual('Hello there Bob')
})

test('missing arg', () => {
expect(interpolateTemplateString('Hello there {{name}}', {})).toEqual('Hello there name')
})

test('repeated arg', () => {
expect(interpolateTemplateString('Hello there {{name}} {{name}} {{name}}', { name: 'Bob' })).toEqual(
'Hello there Bob Bob Bob'
)
})
})

describe('interpolateTemplateStringIfNeeded', () => {
test('string input', () => {
const input = 'Hello there'

expect(interpolateTemplateStringIfNeeded(input)).toEqual(input)
})

test('object input', () => {
expect(
interpolateTemplateStringIfNeeded({
key: 'Hello there {{name}}',
args: { name: 'Bob' },
})
).toEqual('Hello there Bob')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import {
actionNotFoundMessage,
deepMerge,
endTrace,
interpolateTranslationIfNeeded,
interpolateTemplateStringIfNeeded,
literal,
startTrace,
t,
Expand Down Expand Up @@ -396,7 +396,7 @@ export class CasparCGDevice extends DeviceWithState<State, DeviceOptionsCasparCG
cgStop: content.useStopCommand,
})
} else if (content.type === TimelineContentTypeCasparCg.HTMLPAGE) {
const commandUrl = interpolateTranslationIfNeeded(content.url)
const commandUrl = interpolateTemplateStringIfNeeded(content.url)

stateLayer = literal<HtmlPageLayer>({
id: layer.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from 'timeline-state-resolver-types'
import _ = require('underscore')
import got, { OptionsOfTextResponseBody, RequestError } from 'got'
import { interpolateTranslationIfNeeded, t } from '../../lib'
import { interpolateTemplateStringIfNeeded, t } from '../../lib'
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent'
import CacheableLookup from 'cacheable-lookup'

Expand Down Expand Up @@ -221,7 +221,7 @@ export class HTTPSendDevice extends Device<HTTPSendOptions, HttpSendDeviceState,
headers: command.content.headers,
}

const commandUrl: string = interpolateTranslationIfNeeded(command.content.url)
const commandUrl: string = interpolateTemplateStringIfNeeded(command.content.url)

const parsedUrl = new URL(commandUrl)
if (!this.options.noProxy?.includes(parsedUrl.host)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
DeviceType,
} from 'timeline-state-resolver-types'
import type { SofieChefState } from '.'
import { interpolateTranslationIfNeeded } from '../../lib'
import { interpolateTemplateStringIfNeeded } from '../../lib'

export function buildSofieChefState(
timelineState: Timeline.TimelineState<TSRTimelineContent>,
Expand All @@ -23,7 +23,7 @@ export function buildSofieChefState(
const content = layerState.content

if (mapping && content.deviceType === DeviceType.SOFIE_CHEF) {
const commandUrl = interpolateTranslationIfNeeded(content.url)
const commandUrl = interpolateTemplateStringIfNeeded(content.url)

sofieChefState.windows[mapping.options.windowId] = {
url: commandUrl,
Expand Down
8 changes: 4 additions & 4 deletions packages/timeline-state-resolver/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ActionExecutionResultCode,
TimelineDatastoreReferences,
ActionExecutionResult,
StringInterpolation,
TemplateString,
} from 'timeline-state-resolver-types'
import * as _ from 'underscore'
import { PartialDeep } from 'type-fest'
Expand Down Expand Up @@ -312,7 +312,7 @@ export function cloneDeep<T>(input: T): T {
/**
* Interpolate a translation style string
*/
export function interpolateTranslation(key: string, args: { [key: string]: any } | undefined): string {
export function interpolateTemplateString(key: string, args: { [key: string]: any } | undefined): string {
if (!args || typeof key !== 'string') {
return String(key)
}
Expand All @@ -326,7 +326,7 @@ export function interpolateTranslation(key: string, args: { [key: string]: any }
return interpolated
}

export function interpolateTranslationIfNeeded(str: string | StringInterpolation): string {
export function interpolateTemplateStringIfNeeded(str: string | TemplateString): string {
if (typeof str === 'string') return str
return interpolateTranslation(str.key, str.args)
return interpolateTemplateString(str.key, str.args)
}

0 comments on commit c640e2d

Please sign in to comment.