shelving 1.60.0 → 1.61.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.60.0",
14
+ "version": "1.61.0",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
package/util/string.d.ts CHANGED
@@ -81,3 +81,9 @@ export declare function yieldWords(value: string): Generator<string, void, void>
81
81
  export declare const isUppercaseLetter: (str: string) => boolean;
82
82
  /** Is the first character of a string a lowercase letter? */
83
83
  export declare const isLowercaseLetter: (str: string) => boolean;
84
+ /**
85
+ * Limit a string to a given length.
86
+ * - Stops at the last space inside `maxLength`
87
+ * - Appends an `…` ellipses after the string (but only if a limit is applied).
88
+ */
89
+ export declare function limitString(str: string, maxLength: number, append?: string): string;
package/util/string.js CHANGED
@@ -139,3 +139,14 @@ const MATCH_WORD = /[^\s"]+|"([^"]*)"/g; // Runs of characters without spaces, o
139
139
  export const isUppercaseLetter = (str) => isBetween(str.charCodeAt(0), 65, 90);
140
140
  /** Is the first character of a string a lowercase letter? */
141
141
  export const isLowercaseLetter = (str) => isBetween(str.charCodeAt(0), 97, 122);
142
+ /**
143
+ * Limit a string to a given length.
144
+ * - Stops at the last space inside `maxLength`
145
+ * - Appends an `…` ellipses after the string (but only if a limit is applied).
146
+ */
147
+ export function limitString(str, maxLength, append = "…") {
148
+ if (str.length < maxLength)
149
+ return str;
150
+ const lastSpace = str.lastIndexOf(" ", maxLength);
151
+ return `${str.slice(0, lastSpace > 0 ? lastSpace : maxLength).trimEnd()}${append}`;
152
+ }