From e4b4359f674a629b32f74d3a8e39e88b8fada656 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 16 Feb 2024 18:01:31 -0400 Subject: sugar: cutStart --- src/util/sugar.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src/util') diff --git a/src/util/sugar.js b/src/util/sugar.js index 344ddfd4..58a2c3e9 100644 --- a/src/util/sugar.js +++ b/src/util/sugar.js @@ -282,7 +282,19 @@ export function typeAppearance(value) { // if it cuts any text off. export function cut(text, length = 40) { if (text.length >= length) { - return text.slice(0, Math.max(1, length - 3)) + '...'; + const index = Math.max(1, length - 3); + return text.slice(0, index) + '...'; + } else { + return text; + } +} + +// Limits a string to the desired length, filling in an ellipsis at the start +// if it cuts any text off. +export function cutStart(text, length = 40) { + if (text.length >= length) { + const index = Math.min(text.length - 1, text.length - length + 3); + return '...' + text.slice(index); } else { return text; } -- cgit 1.3.0-6-gf8a5