TwitchClone / src / utils / helpers / shortenString / index.tsx
index.tsx
Raw
export const textTruncate = function (
  str: string,
  length?: number,
  ending?: string
) {
  if (length == null) {
    length = 30;
  }
  if (ending == null) {
    ending = "...";
  }
  if (str.length > length) {
    return str.substring(0, length - ending.length) + ending;
  } else {
    return str;
  }
};

export const textTruncateM = function (
  str: string,
  length: number,
  ending: string
) {
  if (length == null) {
    length = 30;
  }
  if (ending == null) {
    ending = "...";
  }
  if (str.length > length) {
    return str.substring(0, length - ending.length) + ending;
  } else {
    return str;
  }
};