A JavaScript module to truncate the string with the specify length; Safe with the emojis, URLs, and words.
- Bun ^ v1.0.0
- Cloudflare Workers
- Deno >= v1.34.0
π‘οΈ Require Permission
N/A
- NodeJS >= v20.9.0
π― Supported Target
- Cloudflare Workers
- NodeJS
- Install via console/shell/terminal:
- Via NPM
npm install @hugoalh/string-overflow[@<Tag>]
- Via PNPM
pnpm add @hugoalh/string-overflow[@<Tag>]
- Via Yarn
yarn add @hugoalh/string-overflow[@<Tag>]
- Via NPM
- Import at the script (
<ScriptName>.js
):import ... from "@hugoalh/string-overflow";
βΉοΈ Note
Although it is recommended to import the entire module, it is also able to import part of the module with sub path if available, please visit file
package.json
propertyexports
for available sub paths.
π― Supported Target
- Bun
- Deno
- Import at the script (
<ScriptName>.js
):import ... from "npm:@hugoalh/string-overflow[@<Tag>]";
βΉοΈ Note
Although it is recommended to import the entire module, it is also able to import part of the module with sub path if available, please visit file
package.json
propertyexports
for available sub paths.
-
class StringTruncator { constructor(maximumLength: number, options: StringTruncatorOptions = {}): StringTruncator; truncate(item: string, maximumLengthOverride?: number): string; static truncate(item: string, maximumLength: number, options: StringTruncatorOptions = {}): string; }
-
function truncateString(item: string, maximumLength: number, options: StringTruncatorOptions = {}): string;
-
enum StringTruncateEllipsisPosition { end = "end", End = "end", middle = "middle", Middle = "middle", start = "start", Start = "start" }
-
interface StringTruncatorOptions extends StringDissectorOptions { /** * Ellipsis mark of the target string. * @default "..." */ ellipsisMark?: string; /** * Ellipsis position at the target string. * @default "end" */ ellipsisPosition?: StringTruncateEllipsisPosition | keyof typeof StringTruncateEllipsisPosition; }
-
const text = "Vel ex sit est sit est tempor enim et voluptua consetetur gubergren gubergren ut. Amet dolores sit. Duo iriure vel dolore illum diam. Ea vero diam diam tincidunt molestie elitr te sed nisl ut vulputate tincidunt accusam sit sed. Amet sea dolore rebum amet accusam labore dolor no sadipscing labore. Sit erat sit sed voluptua tempor sit ea dolor et."; /* Either */ new StringTruncator(100).truncate(text); truncateString(text, 100); //=> "Vel ex sit est sit est tempor enim et voluptua consetetur gubergren gubergren ut. Amet dolores ..." /* Either */ new StringTruncator(100, { safeWords: false }).truncate(text); truncateString(text, 100, { safeWords: false }); //=> "Vel ex sit est sit est tempor enim et voluptua consetetur gubergren gubergren ut. Amet dolores si..."