From 173d0c79e6894562390b5e175741f42ea3f6e6b4 Mon Sep 17 00:00:00 2001 From: Jack Cuthbert Date: Tue, 5 Jan 2021 17:16:08 +1100 Subject: [PATCH] feat: truncate long statuses Closes #114 --- src/index.ts | 6 +++- src/lib/__tests__/truncateStatus.test.ts | 46 ++++++++++++++++++++++++ src/lib/index.ts | 1 + src/lib/truncateStatus.ts | 40 +++++++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/lib/__tests__/truncateStatus.test.ts create mode 100644 src/lib/index.ts create mode 100644 src/lib/truncateStatus.ts diff --git a/src/index.ts b/src/index.ts index 8ca13c2..ea59f6a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import { getHours, getTime, isWeekend } from 'date-fns' +import { truncateStatus } from './lib' import { Cache, Config, LastFM, Schedule, SlackAPI, log } from './services' import type { Track } from './services/LastFM' @@ -106,7 +107,10 @@ async function main(): Promise { const { status_text: currStatus } = await client.getProfile() if (currStatus !== '' && !currStatus.includes(config.app.separator)) return - await client.setStatus(status, trackDuration) + await client.setStatus( + truncateStatus(status, config.app.separator), + trackDuration + ) } } diff --git a/src/lib/__tests__/truncateStatus.test.ts b/src/lib/__tests__/truncateStatus.test.ts new file mode 100644 index 0000000..1aec150 --- /dev/null +++ b/src/lib/__tests__/truncateStatus.test.ts @@ -0,0 +1,46 @@ +import { truncateStatus } from '../truncateStatus' + +const sep = '·' +const hellip = '…' + +describe('truncateStatus', () => { + it('returns a short status unaltered', () => { + expect(truncateStatus(`Hello ${sep} Yes`, sep)).toBe(`Hello ${sep} Yes`) + }) + + it('returns a long track name truncated', () => { + const longBoi = + 'One Day the Only Butterflies Left Will Be in Your Chest as You March Towards Your Death · Bring me the Horizon' + const result = truncateStatus(longBoi, sep) + + const truncName = `One Day the Only Butterflies Left Will Be in Your Chest as You March Towards${hellip}` + const truncArtist = 'Bring me the Horizon' + + expect(result).toEqual(`${truncName} ${sep} ${truncArtist}`) + expect(result.length).toBeLessThanOrEqual(100) + }) + + it('returns a long artist name truncated', () => { + const longBoi = + 'Bring me the Horizon · One Day the Only Butterflies Left Will Be in Your Chest as You March Towards Your Death' + const result = truncateStatus(longBoi, sep) + + const truncName = 'Bring me the Horizon' + const truncArtist = `One Day the Only Butterflies Left Will Be in Your Chest as You March Towards${hellip}` + + expect(result).toEqual(`${truncName} ${sep} ${truncArtist}`) + expect(result.length).toEqual(100) + }) + + it('returns long track and artist names truncated', () => { + const longBoi = + 'One Day the Only Butterflies Left Will Be in Your Chest as You March Towards Your Death · One Day the Only Butterflies Left Will Be in Your Chest as You March Towards Your Death' + const result = truncateStatus(longBoi, sep) + + const truncName = `One Day the Only Butterflies Left Will Be in Yo${hellip}` + const truncArtist = `One Day the Only Butterflies Left Will Be in Yo${hellip}` + + expect(result).toEqual(`${truncName} ${sep} ${truncArtist}`) + expect(result.length).toBeLessThanOrEqual(100) + }) +}) diff --git a/src/lib/index.ts b/src/lib/index.ts new file mode 100644 index 0000000..be04bee --- /dev/null +++ b/src/lib/index.ts @@ -0,0 +1 @@ +export * from './truncateStatus' diff --git a/src/lib/truncateStatus.ts b/src/lib/truncateStatus.ts new file mode 100644 index 0000000..f0f180a --- /dev/null +++ b/src/lib/truncateStatus.ts @@ -0,0 +1,40 @@ +const MAX_LENGTH = 100 + +export function truncateStatus(status: string, sepChar: string): string { + if (status.length <= MAX_LENGTH) return status + + const hellip = '…' + const separator = ` ${sepChar} ` + + const [name, artist] = status.split(separator) + + let newTrackName = name + let newArtistName = artist + + // truncate track name + if (name.length > 48 && artist.length <= 48) { + const availNumChars = + MAX_LENGTH - artist.length - separator.length - hellip.length + + newTrackName = name.slice(0, availNumChars).trim() + hellip + } + + // truncate artist name + else if (name.length <= 48 && artist.length > 48) { + const availNumChars = + MAX_LENGTH - name.length - separator.length - hellip.length + + newArtistName = artist.slice(0, availNumChars).trim() + hellip + } + + // truncate both + else if (artist.length > 48 && artist.length > 48) { + const availNumChars = + (MAX_LENGTH - separator.length - hellip.length * 2) / 2 + + newTrackName = name.slice(0, availNumChars).trim() + hellip + newArtistName = artist.slice(0, availNumChars).trim() + hellip + } + + return `${newTrackName} ${sepChar} ${newArtistName}` +}