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

added String.truncate #4110

Open
wants to merge 5 commits into
base: next-minor
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions .changeset/four-socks-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"effect": minor
---

added String.truncate

```ts
import { truncate } from "effect/String"
truncate("Hello World!", { length: 5 }) // "Hello..."
```
75 changes: 75 additions & 0 deletions packages/effect/src/String.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,78 @@ const isLineBreak = (char: string): boolean => {
const isLineBreak2 = (char0: string, char1: string): boolean => char0.charCodeAt(0) === CR && char1.charCodeAt(0) === LF

const linesSeparated = (self: string, stripped: boolean): LinesIterator => new LinesIterator(self, stripped)

/**
* Truncates a string to a specified length and by default adds the omission string "..." to the end of the string.
* @since 3.12.0
*
* @example
* import { truncate } from "effect/String";
* truncate("Hello World!", { length: 5 }); // "Hello..."
*/
export const truncate: {
(
str: string,
options: {
/**
* The length to truncate the string to.
*/
readonly length: number

/**
* The separator to use to truncate the string. Use " " to truncate evenly on a word boundary.
*/
readonly separator?: string

/**
* The omission string to add to the end of the truncated string. Defaults to "...".
*/
readonly omission?: string
}
): string

(
options: {
/**
* The length to truncate the string to.
*/
readonly length: number

/**
* The separator to use to truncate the string. Use " " to truncate evenly on a word boundary.
*/
readonly separator?: string

/**
* The omission string to add to the end of the truncated string. Defaults to "...".
*/
readonly omission?: string
}
): (str: string) => string
} = dual(
2,
(
str: string,
options: {
readonly length: number
readonly separator?: string
readonly omission?: string
}
) => {
if (str.length <= options.length) {
return str
}

let subString = str.slice(0, options.length)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The omission length should be considered too.


if (typeof options.separator === "string") {
subString = subString.slice(
0,
subString.lastIndexOf(options.separator)
)
}

const omission = options.omission ?? "..."
return subString + omission
}
)
9 changes: 9 additions & 0 deletions packages/effect/test/String.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,13 @@ describe("String", () => {
deepStrictEqual(Array.from(result), ["", "$", " $Hello,", " $World!", " $"])
})
})

it("truncate", () => {
expect(S.truncate("Hello World!", { length: 5 })).toBe("Hello...")
expect(S.truncate("Hello World!", { length: 5, omission: "" })).toBe("Hello")
expect(S.truncate("Hello World!", { length: 7 })).toBe("Hello W...")
expect(S.truncate("Hello World!", { length: 7, separator: " " })).toBe(
"Hello..."
)
})
})
Loading