Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions String/Truncate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @function truncate
* @description Truncates a string to a specified length and appends
* an ellipsis (...) if the string exceeds that length.
* @param {string} str - The input string
* @param {number} maxLength - The maximum length of the output string
* @returns {string} - The truncated string
* @see https://en.wikipedia.org/wiki/Ellipsis
* @example truncate("Hello World", 5) => "Hello..."
* @example truncate("Hi", 5) => "Hi"
* @example truncate("Hello World", 11) => "Hello World"
*/
const truncate = (str, maxLength) => {
if (typeof str !== 'string') {
throw new TypeError('First argument must be a string')
}
if (!Number.isInteger(maxLength) || maxLength < 1) {
throw new TypeError('Second argument must be a positive integer')
}
return str.length > maxLength ? str.slice(0, maxLength) + '...' : str
}

export { truncate }
47 changes: 47 additions & 0 deletions String/test/Truncate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { truncate } from '../Truncate.js'

describe('truncate', () => {
test("truncate('Hello World', 5) returns 'Hello...'", () => {
expect(truncate('Hello World', 5)).toBe('Hello...')
})

test("truncate('Hi', 5) returns 'Hi'", () => {
expect(truncate('Hi', 5)).toBe('Hi')
})

test('truncate at exact length returns original', () => {
expect(truncate('Hello', 5)).toBe('Hello')
})

test("truncate('Hello World', 11) returns 'Hello World'", () => {
expect(truncate('Hello World', 11)).toBe('Hello World')
})

test('truncate empty string returns empty string', () => {
expect(truncate('', 5)).toBe('')
})

test('truncate with maxLength 1', () => {
expect(truncate('Hello', 1)).toBe('H...')
})

test('truncate long string', () => {
expect(truncate('The quick brown fox', 9)).toBe('The quick...')
})

test('throws TypeError for non-string input', () => {
expect(() => truncate(123, 5)).toThrow(TypeError)
})

test('throws TypeError for zero maxLength', () => {
expect(() => truncate('Hello', 0)).toThrow(TypeError)
})

test('throws TypeError for negative maxLength', () => {
expect(() => truncate('Hello', -1)).toThrow(TypeError)
})

test('throws TypeError for float maxLength', () => {
expect(() => truncate('Hello', 2.5)).toThrow(TypeError)
})
})
Loading