diff options
author | Minseo Lee <itoupluk427@gmail.com> | 2024-02-28 13:03:55 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-28 13:03:55 +0900 |
commit | 3767e763906088d410da9ca99532c093368ca196 (patch) | |
tree | 533df6df19320459b69c54c7b26fac5b59279e5e /src/lib/strings/helpers.ts | |
parent | 41e69651f97cc95d42ff4ae247706604faca51ae (diff) | |
parent | c4d30a0b7fb3e73b208a01d0f62130535d549392 (diff) | |
download | voidsky-3767e763906088d410da9ca99532c093368ca196.tar.zst |
Merge branch 'bluesky-social:main' into patch-3
Diffstat (limited to 'src/lib/strings/helpers.ts')
-rw-r--r-- | src/lib/strings/helpers.ts | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/lib/strings/helpers.ts b/src/lib/strings/helpers.ts index e2abe9019..de4562d2c 100644 --- a/src/lib/strings/helpers.ts +++ b/src/lib/strings/helpers.ts @@ -8,10 +8,27 @@ export function pluralize(n: number, base: string, plural?: string): string { return base + 's' } -export function enforceLen(str: string, len: number, ellipsis = false): string { +export function enforceLen( + str: string, + len: number, + ellipsis = false, + mode: 'end' | 'middle' = 'end', +): string { str = str || '' if (str.length > len) { - return str.slice(0, len) + (ellipsis ? '...' : '') + if (ellipsis) { + if (mode === 'end') { + return str.slice(0, len) + '…' + } else if (mode === 'middle') { + const half = Math.floor(len / 2) + return str.slice(0, half) + '…' + str.slice(-half) + } else { + // fallback + return str.slice(0, len) + } + } else { + return str.slice(0, len) + } } return str } |