Skip to content

Latest commit

 

History

History
45 lines (36 loc) · 1.39 KB

ellipsis.md

File metadata and controls

45 lines (36 loc) · 1.39 KB
title tags author_title author_url author_image_url description image
ellipsis
string,beginner
Deepak Vishwakarma
Implementation of "ellipsis" in typescript, javascript and deno.

JS TODO

Truncates a string up to a specified length.

Determine if the string's length is greater than num. Return the string truncated to the desired length, with '...' appended to the end or the original string.

const truncateString = (
  str: string,
  num: number = str.length,
  ellipsisStr = "..."
) =>
  str.length >= num
    ? str.slice(0, num >= ellipsisStr.length ? num - ellipsisStr.length : num) +
      ellipsisStr
    : str;

const ellipsis = (str: string, num: number = str.length, ellipsisStr = "...") =>
  str.length >= num
    ? str.slice(0, num >= ellipsisStr.length ? num - ellipsisStr.length : num) +
      ellipsisStr
    : str;
truncateString("boomerang", 7); // 'boom...'

ellipsis("boomerang", 5, ".."); // "boo.."

ellipsis("boomerang"); // "boomer..."

ellipsis("boomerang", undefined, "♦♦♦"); // "boomer♦♦♦"