diff --git a/packages/utils/index.ts b/packages/utils/index.ts index 37d472706..c8a25176a 100644 --- a/packages/utils/index.ts +++ b/packages/utils/index.ts @@ -17,16 +17,12 @@ export { getLastEventNormal, getLastNormal, getLastGroupNormal, - getNext, getNextGroupNormal, getNextEvent, getNextEventNormal, getNextNormal, - getPrevious, - getPreviousEvent, getPreviousEventNormal, getPreviousNormal, - getPreviousGroup, getPreviousGroupNormal, swapEventData, } from './src/rundown-utils/rundownUtils.js'; diff --git a/packages/utils/src/rundown-utils/rundownUtils.test.ts b/packages/utils/src/rundown-utils/rundownUtils.test.ts index 225df243f..02d9bfb9b 100644 --- a/packages/utils/src/rundown-utils/rundownUtils.test.ts +++ b/packages/utils/src/rundown-utils/rundownUtils.test.ts @@ -1,4 +1,4 @@ -import type { OntimeDelay, OntimeEntry, OntimeEvent, OntimeGroup } from 'ontime-types'; +import type { OntimeDelay, OntimeEntry, OntimeEvent, OntimeGroup, Rundown } from 'ontime-types'; import { SupportedEntry } from 'ontime-types'; import { @@ -6,66 +6,15 @@ import { getLastEvent, getLastGroupNormal, getLastNormal, - getNext, getNextEvent, getNextGroupNormal, getNextNormal, - getPrevious, - getPreviousEvent, - getPreviousGroup, getPreviousGroupNormal, getPreviousNormal, swapEventData, } from './rundownUtils'; import { demoDb } from './rundownUtils.mock'; -describe('getNext()', () => { - it('returns the next event of type event', () => { - const testRundown = { - entries: { - '1': { id: '1', type: SupportedEntry.Event } as OntimeEvent, - '2': { id: '2', type: SupportedEntry.Event } as OntimeEvent, - '3': { id: '3', type: SupportedEntry.Event } as OntimeEvent, - }, - order: ['1', '2', '3'], - }; - - const { nextEvent, nextIndex } = getNext(testRundown, '1'); - expect(nextEvent?.id).toBe('2'); - expect(nextIndex).toBe(1); - }); - - it('returns any type of OntimeEntry ', () => { - const testRundown = { - entries: { - '1': { id: '1', type: SupportedEntry.Event } as OntimeEvent, - '2': { id: '2', type: SupportedEntry.Delay } as OntimeDelay, - '3': { id: '3', type: SupportedEntry.Group } as OntimeGroup, - '4': { id: '4', type: SupportedEntry.Event } as OntimeEvent, - }, - order: ['1', '2', '3', '4'], - }; - - const { nextEvent, nextIndex } = getNext(testRundown, '1'); - expect(nextEvent?.id).toBe('2'); - expect(nextIndex).toBe(1); - }); - - it('returns null if none found', () => { - const testRundown = { - entries: { - '1': { id: '1', type: SupportedEntry.Event } as OntimeEvent, - '2': { id: '2', type: SupportedEntry.Event } as OntimeEvent, - '3': { id: '3', type: SupportedEntry.Event } as OntimeEvent, - }, - order: ['1', '2', '3'], - }; - const { nextEvent, nextIndex } = getNext(testRundown, '3'); - expect(nextEvent).toBe(null); - expect(nextIndex).toBe(null); - }); -}); - describe('getNextEvent()', () => { it('returns the next event of type event', () => { const testRundown = [ @@ -105,101 +54,6 @@ describe('getNextEvent()', () => { }); }); -describe('getPrevious()', () => { - it('returns the previous event of type event', () => { - const testRundown = { - entries: { - '1': { id: '1', type: SupportedEntry.Event } as OntimeEvent, - '2': { id: '2', type: SupportedEntry.Event } as OntimeEvent, - '3': { id: '3', type: SupportedEntry.Event } as OntimeEvent, - }, - order: ['1', '2', '3'], - }; - - const { entry, index } = getPrevious(testRundown, '3'); - expect(entry?.id).toBe('2'); - expect(index).toBe(1); - }); - - it('allow other event types', () => { - const testRundown = { - entries: { - '1': { id: '1', type: SupportedEntry.Event } as OntimeEvent, - '2': { id: '2', type: SupportedEntry.Delay } as OntimeDelay, - '3': { id: '3', type: SupportedEntry.Group } as OntimeGroup, - '4': { id: '4', type: SupportedEntry.Event } as OntimeEvent, - }, - order: ['1', '2', '3', '4'], - }; - - const { entry, index } = getPrevious(testRundown, '3'); - expect(entry?.id).toBe('2'); - expect(index).toBe(1); - }); - - it('returns null if none found', () => { - const testRundown = { - entries: { - '2': { id: '2', type: SupportedEntry.Event } as OntimeEvent, - '3': { id: '3', type: SupportedEntry.Event } as OntimeEvent, - }, - order: ['1', '2', '3'], - }; - - const { entry, index } = getPrevious(testRundown, '2'); - expect(entry).toBe(null); - expect(index).toBe(null); - }); -}); - -describe('getPreviousEvent()', () => { - it('returns the previous event of type event', () => { - const testRundown = { - entries: { - '1': { id: '1', type: SupportedEntry.Event } as OntimeEvent, - '2': { id: '2', type: SupportedEntry.Event } as OntimeEvent, - '3': { id: '3', type: SupportedEntry.Event } as OntimeEvent, - }, - order: ['1', '2', '3'], - }; - - const { previousEvent, previousIndex } = getPreviousEvent(testRundown, '3'); - expect(previousEvent?.id).toBe('2'); - expect(previousIndex).toBe(1); - }); - - it('ignores other event types', () => { - const testRundown = { - entries: { - '1': { id: '1', type: SupportedEntry.Event } as OntimeEvent, - '2': { id: '2', type: SupportedEntry.Delay } as OntimeDelay, - '3': { id: '3', type: SupportedEntry.Group } as OntimeGroup, - '4': { id: '4', type: SupportedEntry.Event } as OntimeEvent, - }, - order: ['1', '2', '3', '4'], - }; - - const { previousEvent, previousIndex } = getPreviousEvent(testRundown, '4'); - expect(previousEvent?.id).toBe('1'); - expect(previousIndex).toBe(0); - }); - - it('returns null if none found', () => { - const testRundown = { - entries: { - '2': { id: '2', type: SupportedEntry.Delay } as OntimeDelay, - '3': { id: '3', type: SupportedEntry.Group } as OntimeGroup, - '4': { id: '4', type: SupportedEntry.Event } as OntimeEvent, - }, - order: ['2', '3', '4'], - }; - - const { previousEvent, previousIndex } = getPreviousEvent(testRundown, '2'); - expect(previousEvent).toBe(null); - expect(previousIndex).toBe(null); - }); -}); - describe('swapEventData', () => { it('swaps some data between two events', () => { const eventA = { @@ -373,55 +227,11 @@ describe('getLastEvent', () => { }); }); - describe('getPreviousGroup()', () => { - const testRundown = { entries: { - a: { id: 'a', type: SupportedEntry.Event } as OntimeEvent, - b: { id: 'b', type: SupportedEntry.Event } as OntimeEvent, - c: { id: 'c', type: SupportedEntry.Event } as OntimeEvent, - d: { id: 'd', type: SupportedEntry.Delay } as OntimeDelay, - e: { id: 'e', type: SupportedEntry.Group } as OntimeGroup, - f: { id: 'f', type: SupportedEntry.Event } as OntimeEvent, - g: { id: 'g', type: SupportedEntry.Group } as OntimeGroup, - h: { id: 'h', type: SupportedEntry.Event } as OntimeEvent, }, - order: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], - }; - test.each([ - ['h', 'g'], - ['f', 'e'], - ])('returns the relevant group', (id, expected) => { - const group = getPreviousGroup(testRundown, id); - expect(group?.id).toBe(expected); - }); - it('returns null if there is no parent group relevant group', () => { - const group = getPreviousGroup(testRundown, 'a'); - expect(group).toBe(null); - }); - it('also works on index 0', () => { - testRundown.order.unshift('0'); - // @ts-expect-error -- we are adding an event to the rundown - testRundown.entries['0'] = { id: '0', type: SupportedEntry.Group } as OntimeGroup; - const group = getPreviousGroup(testRundown, 'a'); - expect(group?.id).toBe('0'); - }); - it('returns the parent group if nested event', () => { - const testRundown = { - entries: { - 1: { id: '1', type: SupportedEntry.Event } as OntimeEvent, - group: { id: 'group', type: SupportedEntry.Group, entries: ['21', '22', '23'] } as OntimeGroup, - 21: { id: '21', type: SupportedEntry.Event, parent: 'group' } as OntimeEvent, - 22: { id: '22', type: SupportedEntry.Event, parent: 'group' } as OntimeEvent, - 23: { id: '23', type: SupportedEntry.Event, parent: 'group' } as OntimeEvent, - }, - order: ['1', 'group'], - }; - const group = getPreviousGroup(testRundown, '21'); - expect(group?.id).toBe('group'); - }); }); }); diff --git a/packages/utils/src/rundown-utils/rundownUtils.ts b/packages/utils/src/rundown-utils/rundownUtils.ts index e2d15454f..9f70beadb 100644 --- a/packages/utils/src/rundown-utils/rundownUtils.ts +++ b/packages/utils/src/rundown-utils/rundownUtils.ts @@ -114,24 +114,6 @@ export function getLastEventNormal( return { lastEvent: null, lastIndex: null }; } -/** - * Gets next entry in rundown, if it exists - */ -export function getNext( - rundown: Pick, - currentId: EntryId, -): { nextEvent: OntimeEntry | null; nextIndex: number | null } { - const index = rundown.order.findIndex((entryId) => entryId === currentId); - if (index !== -1 && index + 1 < rundown.order.length) { - const nextIndex = index + 1; - const nextId = rundown.order[nextIndex]; - const nextEvent = rundown.entries[nextId]; - return { nextEvent, nextIndex }; - } else { - return { nextEvent: null, nextIndex: null }; - } -} - /** * Gets next entry in rundown, if it exists */ @@ -195,22 +177,6 @@ export function getNextEventNormal( return { nextEvent: null, nextIndex: null }; } -/** - * Gets previous entry in rundown, if it exists - */ -export function getPrevious(rundown: Pick, currentId: EntryId): IndexAndEntry { - const currentIndex = rundown.order.findIndex((entryId) => entryId === currentId); - - if (currentIndex > 1) { - const index = currentIndex - 1; - const previousId = rundown.order[index]; - const entry = rundown.entries[previousId]; - return { entry, index }; - } else { - return { entry: null, index: null }; - } -} - /** * Gets previous entry in a normalised rundown, if it exists */ @@ -262,27 +228,6 @@ export function getLastGroupNormal(entries: RundownEntries, flatOrder: EntryId[] return { entry: null, index: null }; } -/** - * Gets previous scheduled event in rundown, if it exists - */ -export function getPreviousEvent( - rundown: Pick, - currentId: EntryId, -): { previousEvent: OntimeEvent | null; previousIndex: number | null } { - const index = rundown.order.findIndex((entryId) => entryId === currentId); - if (index < 0) { - return { previousEvent: null, previousIndex: null }; - } - for (let i = index - 1; i >= 0; i--) { - const previousId = rundown.order[i]; - const previousEvent = rundown.entries[previousId]; - if (isOntimeEvent(previousEvent)) { - return { previousEvent, previousIndex: i }; - } - } - return { previousEvent: null, previousIndex: null }; -} - /** * Gets previous scheduled event in a normalised rundown, if it exists */ @@ -417,29 +362,9 @@ export function getNextGroupNormal( /** * Gets relevant group element for a given ID */ -export function getPreviousGroup(rundown: Pick, currentId: EntryId): OntimeGroup | null { - const currentEvent = rundown.entries[currentId]; - // check if entry is inside a group - if ('parent' in currentEvent && currentEvent.parent) { - return rundown.entries[currentEvent.parent] as OntimeGroup; - } - let foundCurrentEvent = false; - // Iterate backwards through the rundown to find the current event - for (let i = rundown.order.length - 1; i >= 0; i--) { - const entryId = rundown.order[i]; - const entry = rundown.entries[entryId]; - if (!foundCurrentEvent && entry.id === currentId) { - // set the flag when the current event is found - foundCurrentEvent = true; - continue; } - // the first group before the current event is the relevant one - if (foundCurrentEvent && isOntimeGroup(entry)) { - return entry; } } - // no groups exist before null event - return null; }