hugoalh-studio / string-overflow-js

[MIGRATED TO https://github.com/hugoalh-studio/string-overflow-es] A JavaScript module to truncate the string with the specify length; Safe with the emojis, URLs, and words.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

String Overflow (JavaScript)

βš–οΈ MIT

πŸ—‚οΈ GitHub: hugoalh-studio/string-overflow-js NPM: @hugoalh/string-overflow

πŸ†™ Latest Release Version (Latest Release Date)

A JavaScript module to truncate the string with the specify length; Safe with the emojis, URLs, and words.

🎯 Target

  • Bun ^ v1.0.0
  • Cloudflare Workers
  • Deno >= v1.34.0

    πŸ›‘οΈ Require Permission

    N/A

  • NodeJS >= v20.9.0

πŸ”— Other Edition

πŸ”° Usage

Via Installation

🎯 Supported Target

  • Cloudflare Workers
  • NodeJS
  1. 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>]
  2. 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 property exports for available sub paths.

Via NPM Specifier

🎯 Supported Target

  • Bun
  • Deno
  1. 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 property exports for available sub paths.

🧩 API

  • 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;
    }

✍️ Example

  • 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..."

About

[MIGRATED TO https://github.com/hugoalh-studio/string-overflow-es] A JavaScript module to truncate the string with the specify length; Safe with the emojis, URLs, and words.

License:Other


Languages

Language:TypeScript 100.0%