@playpilot/tpi 5.28.0 → 5.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@playpilot/tpi",
3
- "version": "5.28.0",
3
+ "version": "5.29.0",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "vite dev",
@@ -1,7 +1,9 @@
1
1
  <script lang="ts">
2
2
  import { fetchParticipantsForTitle } from '$lib/api/participants'
3
3
  import { TrackingEvent } from '$lib/enums/TrackingEvent'
4
+ import { getLinkInjectionElements, getLinkInjectionsParentElement, getPageText } from '$lib/injection'
4
5
  import { openModal } from '$lib/modal'
6
+ import { cleanPhrase, findNumberOfMatchesInString } from '$lib/text'
5
7
  import { track } from '$lib/tracking'
6
8
  import type { ParticipantData } from '$lib/types/participant'
7
9
  import type { TitleData } from '$lib/types/title'
@@ -13,6 +15,31 @@
13
15
 
14
16
  const { title }: Props = $props()
15
17
 
18
+ /**
19
+ * Order participants by how often their name appears in an the page text. This only takes into account
20
+ * their full name. If an actor is mentioned by first name only, which is often the case, they won't get
21
+ * boosted. Often times if an actor is mentioned at least once, their full name will be somewhere in the
22
+ * page text, so the goal is still the same; to list participants that are mentioned in the page text before
23
+ * other participants.
24
+ */
25
+ function orderParticipantsByPageTextOccurrence(participants: ParticipantData[]): ParticipantData[] {
26
+ const pageText = cleanPhrase(getPageText(getLinkInjectionElements(getLinkInjectionsParentElement())))
27
+
28
+ // Create a new object with all participants by their sid with the number of times their names appear in the page text
29
+ const participantsByOccurrence: Record<string, { participant: ParticipantData, count: number }> = {}
30
+ for(const participant of participants) {
31
+ const count = findNumberOfMatchesInString(pageText, cleanPhrase(participant.name))
32
+
33
+ participantsByOccurrence[participant.sid] = { participant, count }
34
+ }
35
+
36
+ // Sort all participants by their occurrences. If a participant did not occur their positioned will be retained
37
+ // relative to where it was before
38
+ const participantsSortedByCount = Object.values(participantsByOccurrence).sort((a, b) => b.count - a.count)
39
+
40
+ return participantsSortedByCount.map(({ participant }) => participant)
41
+ }
42
+
16
43
  function onclick(event: MouseEvent, participant: ParticipantData): void {
17
44
  openModal({ event, type: 'participant', data: participant })
18
45
  track(TrackingEvent.ParticipantClick, null, { title_source: title.original_title, participant: participant.name })
@@ -28,7 +55,7 @@
28
55
  {:then participants}
29
56
  {#if participants?.length}
30
57
  <Rail heading="Cast">
31
- {#each participants.slice(0, 15) as participant}
58
+ {#each orderParticipantsByPageTextOccurrence(participants).slice(0, 15) as participant}
32
59
  <button class="participant" data-testid="participant" onclick={event => onclick(event, participant)}>
33
60
  <span class="truncate">{participant.name}</span>
34
61
 
@@ -21,10 +21,13 @@ vi.mock('$lib/api/participants', () => ({
21
21
  }))
22
22
 
23
23
  describe('ParticipantsRail.svelte', () => {
24
+ beforeEach(() => {
25
+ document.body.innerHTML = ''
26
+ })
27
+
24
28
  it('Should render each given participant', async () => {
25
29
  vi.mocked(fetchParticipantsForTitle).mockResolvedValueOnce(participants)
26
30
 
27
- // @ts-ignore
28
31
  const { getByText } = render(ParticipantsRail, { title })
29
32
 
30
33
  await waitFor(() => {
@@ -33,6 +36,19 @@ describe('ParticipantsRail.svelte', () => {
33
36
  })
34
37
  })
35
38
 
39
+ it('Should order participants by their number of occurrences on the page', async () => {
40
+ document.body.innerHTML = `<main>${participants[1].name}. ${participants[2].name}, ${participants[2].name}</main>`
41
+ vi.mocked(fetchParticipantsForTitle).mockResolvedValueOnce(participants)
42
+
43
+ const { getAllByTestId } = render(ParticipantsRail, { title })
44
+
45
+ await waitFor(() => {
46
+ expect(getAllByTestId('participant')[0].innerText).toContain(participants[2].name)
47
+ expect(getAllByTestId('participant')[1].innerText).toContain(participants[1].name)
48
+ expect(getAllByTestId('participant')[2].innerText).toContain(participants[0].name)
49
+ })
50
+ })
51
+
36
52
  it('Should not render when no participants are returned', async () => {
37
53
  vi.mocked(fetchParticipantsForTitle).mockResolvedValueOnce([])
38
54