Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: split files #56

Merged
merged 8 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
47 changes: 47 additions & 0 deletions src/additional/truncate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { type Truncate, truncate } from './truncate.js'

namespace TruncateTests {
type test1 = Expect<Equal<Truncate<'Hello, world', 9>, 'Hello,...'>>
type test2 = Expect<Equal<Truncate<'Hello, world', 12>, 'Hello, world'>>
type test3 = Expect<Equal<Truncate<'Hello, world', 2>, '...'>>
type test4 = Expect<Equal<Truncate<'Hello, world', 9, '[...]'>, 'Hell[...]'>>
type test5 = Expect<Equal<Truncate<'Hello, world', -1>, '...'>>
type test6 = Expect<Equal<Truncate<'Hello, world', 0, '[...]'>, '[...]'>>
}

describe('truncate', () => {
test('truncate small sentence does nothing', () => {
const expected = 'Hello' as const
const result = truncate('Hello', 9)
expect(result).toEqual(expected)
type test = Expect<Equal<typeof result, typeof expected>>
})

test('truncate big sentence truncate', () => {
const expected = 'Hello ...' as const
const result = truncate('Hello world', 9)
expect(result).toEqual(expected)
type test = Expect<Equal<typeof result, typeof expected>>
})

test('truncate with negative integer does truncate', () => {
const expected = '...' as const
const result = truncate('Hello world', -1)
expect(result).toEqual(expected)
type test = Expect<Equal<typeof result, typeof expected>>
})

test('truncate big sentence with specified omission', () => {
const expected = 'Hello[...]' as const
const result = truncate('Hello world', 10, '[...]')
expect(result).toEqual(expected)
type test = Expect<Equal<typeof result, typeof expected>>
})

test('truncate small sentence with specified omission', () => {
const expected = 'Hello' as const
const result = truncate('Hello', 10, '[...]')
expect(result).toEqual(expected)
type test = Expect<Equal<typeof result, typeof expected>>
})
})
39 changes: 39 additions & 0 deletions src/additional/truncate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { Math } from '../internal/math.js'
import { join, type Join } from '../native/join.js'
import { type Length } from '../native/length.js'
import { type Slice } from '../native/slice.js'

// STRING FUNCTIONS

/**
* Truncate a string if it's longer than the given maximum length.
* The last characters of the truncated string are replaced with the omission string which defaults to "...".
*/
export type Truncate<
T extends string,
Size extends number,
Omission extends string = '...',
> = Math.IsNegative<Size> extends true
? Omission
: Math.Subtract<Length<T>, Size> extends 0
? T
: Join<[Slice<T, 0, Math.Subtract<Size, Length<Omission>>>, Omission]>

/**
* A strongly typed function to truncate a string if it's longer than the given maximum string length.
* The last characters of the truncated string are replaced with the omission string which defaults to "...".
* @param sentence the sentence to extract the words from.
* @param length the maximum length of the string.
* @param omission the string to append to the end of the truncated string.
* @returns the truncated string
* @example truncate('Hello, World', 8) // 'Hello...'
*/
export function truncate<
T extends string,
S extends number,
P extends string = '...',
>(sentence: T, length: S, omission = '...' as P): Truncate<T, S, P> {
if (length < 0) return omission as Truncate<T, S, P>
if (sentence.length <= length) return sentence as Truncate<T, S, P>
return join([sentence.slice(0, length - omission.length), omission])
}
198 changes: 0 additions & 198 deletions src/casing.test.ts

This file was deleted.

Loading
Loading