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 1 commit
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
Next Next commit
added String.truncate util
  • Loading branch information
jessekelly881 committed Dec 10, 2024
commit c764f25011e64a3ee56c8e1af5669f08005b2ab5
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!", 5) // "Hello..."
```
90 changes: 90 additions & 0 deletions packages/effect/src/String.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,93 @@ 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 (e.g. "...") to the end of the string.
* @since 3.12.0
*
* @example
* import { truncate } from "effect/String";
* truncate("Hello World!", 5); // "Hello..."
*/
export const truncate: {
(
str: string,
lengthOrOptions:
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should just have an options object instead of a union. Also the member should be readonly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

| number
| {
/**
* The length to truncate the string to.
*/
length: number

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

(
lengthOrOptions:
| number
| {
/**
* The length to truncate the string to.
*/
length: number

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

/**
* The omission string to add to the end of the truncated string. Defaults to "...".
*/
omission?: string
}
): (str: string) => string
} = dual(
2,
(
str: string,
lengthOrOptions:
| number
| {
length: number
separator?: string
omission?: string
}
) => {
const len = typeof lengthOrOptions === "number"
? lengthOrOptions
: lengthOrOptions.length

if (str.length <= len) {
return str
}

let subString = str.slice(0, len)

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

const omission = typeof lengthOrOptions === "object"
? (lengthOrOptions.omission ?? "...")
: "..."

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

it("truncate", () => {
expect(S.truncate("Hello World!", 5)).toBe("Hello...")
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