« get me outta code hell

sugar: cut (string to length) - 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>2023-11-20 13:53:13 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-11-20 13:53:13 -0400
commit1de52e0937132eca0349460fdcc7b0383d2b3312 (patch)
tree134a2811cd0216d5021f3c8ed229eafd1cc4c327
parent2d58b70d0bd5bbc7cdd8789332a31a220c78da01 (diff)
sugar: cut (string to length)
-rw-r--r--src/util/sugar.js10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/util/sugar.js b/src/util/sugar.js
index 9646be37..ad676914 100644
--- a/src/util/sugar.js
+++ b/src/util/sugar.js
@@ -250,6 +250,16 @@ export function typeAppearance(value) {
   return typeof value;
 }
 
+// Limits a string to the desired length, filling in an ellipsis at the end
+// 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)) + '...';
+  } else {
+    return text;
+  }
+}
+
 // Binds default values for arguments in a {key: value} type function argument
 // (typically the second argument, but may be overridden by providing a
 // [bindOpts.bindIndex] argument). Typically useful for preparing a function for