-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
37 additions
and
54 deletions.
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 |
---|---|---|
@@ -1,61 +1,44 @@ | ||
type Options = { | ||
/** | ||
* The maximum string length. | ||
* | ||
* Default: 30 | ||
*/ | ||
length?: number; | ||
|
||
/** | ||
* The string to indicate text is omitted. | ||
* | ||
* Also named [ellipsis](https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow) | ||
* | ||
* Default: "...", you might want to use "…" (… U+02026) instead | ||
*/ | ||
omission?: string; | ||
|
||
/** | ||
* The separator pattern to truncate to. | ||
* | ||
* Default: none | ||
*/ | ||
separator?: string; | ||
}; | ||
|
||
/** | ||
* Truncates a string if it's longer than the given maximum length. | ||
* The last characters of the truncated string are replaced with the omission | ||
* The last characters of the truncated string are replaced with the ellipsis | ||
* string which defaults to "...". | ||
* | ||
* @example | ||
* truncate("Hello, world!", { length: 5 }) | ||
* // => "Hello..." | ||
* | ||
* truncate("Hello, world!", { length: 5, ellipsis: " [...]" }) | ||
* // => "Hello [...]" | ||
* | ||
* truncate("Hello, world!", { length: 5, separator: " " }) | ||
* // => "Hello, ..." | ||
* | ||
* @param str The string to truncate | ||
* @param options The options object | ||
* @param options.length The maximum string length (default: 30) | ||
* @param options.ellipsis The string to indicate text is omitted (default: "...") | ||
* @param options.separator The separator pattern to truncate to (default: none) | ||
* @returns The truncated string | ||
*/ | ||
export function truncate(str: string, options?: Options) { | ||
// https://stackoverflow.com/q/1199352 | ||
// https://github.com/Maggi64/moderndash/issues/155 | ||
// https://lodash.com/docs/4.17.15#truncate | ||
|
||
const { length = 30, omission = "...", separator } = options ?? {}; | ||
export function truncate(str: string, options?: { length?: number; ellipsis?: string; separator?: string }): string { | ||
const { length = 30, ellipsis = "...", separator } = options ?? {}; | ||
if (str.length <= length) return str; | ||
|
||
if (str.length <= length) { | ||
return str; | ||
} | ||
const end = length - ellipsis.length; | ||
|
||
if (end < 1) | ||
return ellipsis; | ||
|
||
// Actually long enough to truncate the string | ||
let truncated = str.slice(0, end); | ||
|
||
let maxLength = length - omission.length; | ||
if (maxLength < 0) { | ||
maxLength = 0; | ||
if (separator) { | ||
const sepIndex = truncated.lastIndexOf(separator); | ||
if (sepIndex > -1) { | ||
truncated = truncated.slice(0, sepIndex); | ||
} | ||
} | ||
const subString = str.slice( | ||
0, | ||
// FYI .slice() is OK if maxLength > text.length | ||
maxLength | ||
); | ||
|
||
return ( | ||
(separator | ||
? subString.slice(0, subString.lastIndexOf(separator)) | ||
: subString) + omission | ||
); | ||
return truncated + ellipsis; | ||
} |
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