-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './truncateStatus' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` | ||
} |