Skip to content

Commit

Permalink
feat: truncate long statuses
Browse files Browse the repository at this point in the history
Closes #114
  • Loading branch information
JackCuthbert committed Jan 5, 2021
1 parent ffecd07 commit 173d0c7
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -106,7 +107,10 @@ async function main(): Promise<void> {
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
)
}
}

Expand Down
46 changes: 46 additions & 0 deletions src/lib/__tests__/truncateStatus.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './truncateStatus'
40 changes: 40 additions & 0 deletions src/lib/truncateStatus.ts
Original file line number Diff line number Diff line change
@@ -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}`
}

0 comments on commit 173d0c7

Please sign in to comment.