« get me outta code hell

sugar: cutStart - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-02-16 18:01:31 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-02-17 17:09:18 -0400
commite4b4359f674a629b32f74d3a8e39e88b8fada656 (patch)
tree8bc1f8a7870b474b8c266ba3af9dd6ba7e499e36
parentafb1e2e771b8de59346516c4a5d7ef7ef5f808f9 (diff)
sugar: cutStart
-rw-r--r--src/util/sugar.js14
1 files changed, 13 insertions, 1 deletions
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;
   }