@scalar/helpers 0.1.0 → 0.1.1
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/CHANGELOG.md +8 -0
- package/dist/general/debounce.d.ts +33 -0
- package/dist/general/debounce.d.ts.map +1 -0
- package/dist/general/debounce.js +55 -0
- package/dist/general/debounce.js.map +7 -0
- package/dist/regex/find-variables.d.ts +8 -1
- package/dist/regex/find-variables.d.ts.map +1 -1
- package/dist/regex/find-variables.js +3 -1
- package/dist/regex/find-variables.js.map +2 -2
- package/dist/regex/regex-helpers.d.ts +7 -0
- package/dist/regex/regex-helpers.d.ts.map +1 -1
- package/dist/regex/regex-helpers.js +3 -0
- package/dist/regex/regex-helpers.js.map +2 -2
- package/dist/string/add-word-breaks.d.ts +32 -0
- package/dist/string/add-word-breaks.d.ts.map +1 -0
- package/dist/string/add-word-breaks.js +19 -0
- package/dist/string/add-word-breaks.js.map +7 -0
- package/dist/string/truncate.d.ts +13 -0
- package/dist/string/truncate.d.ts.map +1 -0
- package/dist/string/truncate.js +10 -0
- package/dist/string/truncate.js.map +7 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @scalar/helpers
|
|
2
2
|
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#7289](https://github.com/scalar/scalar/pull/7289) [`9c9dbba`](https://github.com/scalar/scalar/commit/9c9dbbaa940667303f0ace59469fd78c2a741937) Thanks [@amritk](https://github.com/amritk)! - feat: move debounce to helpers and add max wait
|
|
8
|
+
|
|
9
|
+
- [#7252](https://github.com/scalar/scalar/pull/7252) [`4bec1ba`](https://github.com/scalar/scalar/commit/4bec1ba332e919c4ee32dcfbfb07bd8ee42c4d74) Thanks [@hwkr](https://github.com/hwkr)! - fix(api-reference): improve wrapping of long strings
|
|
10
|
+
|
|
3
11
|
## 0.1.0
|
|
4
12
|
|
|
5
13
|
### Minor Changes
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for configuring the debounce behavior.
|
|
3
|
+
*/
|
|
4
|
+
export type DebounceOptions = {
|
|
5
|
+
/** The delay in milliseconds before executing the function. Defaults to 100ms. */
|
|
6
|
+
delay?: number;
|
|
7
|
+
/** Maximum time in milliseconds to wait before forcing execution, even with continuous calls. */
|
|
8
|
+
maxWait?: number;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Creates a debounced function executor that delays execution until after a specified time.
|
|
12
|
+
* Multiple calls with the same key will cancel previous pending executions.
|
|
13
|
+
*
|
|
14
|
+
* This is useful for batching rapid updates (like auto-save or API calls) to avoid
|
|
15
|
+
* unnecessary processing or network requests.
|
|
16
|
+
*
|
|
17
|
+
* @param options - Configuration options for delay, maxWait, and key separator
|
|
18
|
+
* @returns A function that accepts a key array and callback to execute
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const debouncedSave = debounce({ delay: 328 })
|
|
22
|
+
* debouncedSave.execute(['user', '123'], () => saveUser(user))
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // With maxWait to guarantee execution even with continuous calls
|
|
26
|
+
* const debouncedSave = debounce({ delay: 328, maxWait: 2000 })
|
|
27
|
+
* debouncedSave.execute(['user', '123'], () => saveUser(user))
|
|
28
|
+
*/
|
|
29
|
+
export declare const debounce: (options?: DebounceOptions) => {
|
|
30
|
+
execute: (key: string, fn: () => unknown | Promise<unknown>) => void;
|
|
31
|
+
cleanup: () => void;
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=debounce.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debounce.d.ts","sourceRoot":"","sources":["../../src/general/debounce.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,kFAAkF;IAClF,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,iGAAiG;IACjG,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB,CAAA;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,QAAQ,GAAI,UAAS,eAAoB;mBA6C9B,MAAM,MAAM,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,KAAG,IAAI;mBAvCrD,IAAI;CAiEzB,CAAA"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const debounce = (options = {}) => {
|
|
2
|
+
const { delay = 328, maxWait } = options;
|
|
3
|
+
const timeouts = /* @__PURE__ */ new Map();
|
|
4
|
+
const maxWaitTimeouts = /* @__PURE__ */ new Map();
|
|
5
|
+
const latestFunctions = /* @__PURE__ */ new Map();
|
|
6
|
+
const cleanup = () => {
|
|
7
|
+
timeouts.forEach(clearTimeout);
|
|
8
|
+
maxWaitTimeouts.forEach(clearTimeout);
|
|
9
|
+
timeouts.clear();
|
|
10
|
+
maxWaitTimeouts.clear();
|
|
11
|
+
latestFunctions.clear();
|
|
12
|
+
};
|
|
13
|
+
const executeAndCleanup = (key) => {
|
|
14
|
+
const fn = latestFunctions.get(key);
|
|
15
|
+
const timeout = timeouts.get(key);
|
|
16
|
+
if (timeout !== void 0) {
|
|
17
|
+
clearTimeout(timeout);
|
|
18
|
+
timeouts.delete(key);
|
|
19
|
+
}
|
|
20
|
+
const maxWaitTimeout = maxWaitTimeouts.get(key);
|
|
21
|
+
if (maxWaitTimeout !== void 0) {
|
|
22
|
+
clearTimeout(maxWaitTimeout);
|
|
23
|
+
maxWaitTimeouts.delete(key);
|
|
24
|
+
}
|
|
25
|
+
latestFunctions.delete(key);
|
|
26
|
+
if (fn !== void 0) {
|
|
27
|
+
try {
|
|
28
|
+
fn();
|
|
29
|
+
} catch {
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const execute = (key, fn) => {
|
|
34
|
+
latestFunctions.set(key, fn);
|
|
35
|
+
const existingTimeout = timeouts.get(key);
|
|
36
|
+
if (existingTimeout !== void 0) {
|
|
37
|
+
clearTimeout(existingTimeout);
|
|
38
|
+
}
|
|
39
|
+
timeouts.set(
|
|
40
|
+
key,
|
|
41
|
+
setTimeout(() => executeAndCleanup(key), delay)
|
|
42
|
+
);
|
|
43
|
+
if (maxWait !== void 0 && !maxWaitTimeouts.has(key)) {
|
|
44
|
+
maxWaitTimeouts.set(
|
|
45
|
+
key,
|
|
46
|
+
setTimeout(() => executeAndCleanup(key), maxWait)
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
return { execute, cleanup };
|
|
51
|
+
};
|
|
52
|
+
export {
|
|
53
|
+
debounce
|
|
54
|
+
};
|
|
55
|
+
//# sourceMappingURL=debounce.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/general/debounce.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Options for configuring the debounce behavior.\n */\nexport type DebounceOptions = {\n /** The delay in milliseconds before executing the function. Defaults to 100ms. */\n delay?: number\n /** Maximum time in milliseconds to wait before forcing execution, even with continuous calls. */\n maxWait?: number\n}\n\n/**\n * Creates a debounced function executor that delays execution until after a specified time.\n * Multiple calls with the same key will cancel previous pending executions.\n *\n * This is useful for batching rapid updates (like auto-save or API calls) to avoid\n * unnecessary processing or network requests.\n *\n * @param options - Configuration options for delay, maxWait, and key separator\n * @returns A function that accepts a key array and callback to execute\n *\n * @example\n * const debouncedSave = debounce({ delay: 328 })\n * debouncedSave.execute(['user', '123'], () => saveUser(user))\n *\n * @example\n * // With maxWait to guarantee execution even with continuous calls\n * const debouncedSave = debounce({ delay: 328, maxWait: 2000 })\n * debouncedSave.execute(['user', '123'], () => saveUser(user))\n */\nexport const debounce = (options: DebounceOptions = {}) => {\n const { delay = 328, maxWait } = options\n const timeouts = new Map<string, ReturnType<typeof setTimeout>>()\n const maxWaitTimeouts = new Map<string, ReturnType<typeof setTimeout>>()\n const latestFunctions = new Map<string, () => unknown | Promise<unknown>>()\n\n const cleanup = (): void => {\n timeouts.forEach(clearTimeout)\n maxWaitTimeouts.forEach(clearTimeout)\n timeouts.clear()\n maxWaitTimeouts.clear()\n latestFunctions.clear()\n }\n\n /** Executes the function and cleans up all associated timeouts */\n const executeAndCleanup = (key: string): void => {\n // Get the latest function for this key\n const fn = latestFunctions.get(key)\n\n // Clear both timeout types\n const timeout = timeouts.get(key)\n if (timeout !== undefined) {\n clearTimeout(timeout)\n timeouts.delete(key)\n }\n\n const maxWaitTimeout = maxWaitTimeouts.get(key)\n if (maxWaitTimeout !== undefined) {\n clearTimeout(maxWaitTimeout)\n maxWaitTimeouts.delete(key)\n }\n\n // Clear the latest function reference\n latestFunctions.delete(key)\n\n // Execute the function if it exists\n if (fn !== undefined) {\n try {\n fn()\n } catch {\n // Errors are silently caught to prevent the debounce mechanism from breaking\n }\n }\n }\n\n const execute = (key: string, fn: () => unknown | Promise<unknown>): void => {\n // Store the latest function for this key\n latestFunctions.set(key, fn)\n\n // Clear existing debounce timeout\n const existingTimeout = timeouts.get(key)\n if (existingTimeout !== undefined) {\n clearTimeout(existingTimeout)\n }\n\n // Set debounce timeout\n timeouts.set(\n key,\n setTimeout(() => executeAndCleanup(key), delay),\n )\n\n // Set maxWait timeout only if configured and this is a new sequence\n if (maxWait !== undefined && !maxWaitTimeouts.has(key)) {\n maxWaitTimeouts.set(\n key,\n setTimeout(() => executeAndCleanup(key), maxWait),\n )\n }\n }\n\n return { execute, cleanup }\n}\n"],
|
|
5
|
+
"mappings": "AA6BO,MAAM,WAAW,CAAC,UAA2B,CAAC,MAAM;AACzD,QAAM,EAAE,QAAQ,KAAK,QAAQ,IAAI;AACjC,QAAM,WAAW,oBAAI,IAA2C;AAChE,QAAM,kBAAkB,oBAAI,IAA2C;AACvE,QAAM,kBAAkB,oBAAI,IAA8C;AAE1E,QAAM,UAAU,MAAY;AAC1B,aAAS,QAAQ,YAAY;AAC7B,oBAAgB,QAAQ,YAAY;AACpC,aAAS,MAAM;AACf,oBAAgB,MAAM;AACtB,oBAAgB,MAAM;AAAA,EACxB;AAGA,QAAM,oBAAoB,CAAC,QAAsB;AAE/C,UAAM,KAAK,gBAAgB,IAAI,GAAG;AAGlC,UAAM,UAAU,SAAS,IAAI,GAAG;AAChC,QAAI,YAAY,QAAW;AACzB,mBAAa,OAAO;AACpB,eAAS,OAAO,GAAG;AAAA,IACrB;AAEA,UAAM,iBAAiB,gBAAgB,IAAI,GAAG;AAC9C,QAAI,mBAAmB,QAAW;AAChC,mBAAa,cAAc;AAC3B,sBAAgB,OAAO,GAAG;AAAA,IAC5B;AAGA,oBAAgB,OAAO,GAAG;AAG1B,QAAI,OAAO,QAAW;AACpB,UAAI;AACF,WAAG;AAAA,MACL,QAAQ;AAAA,MAER;AAAA,IACF;AAAA,EACF;AAEA,QAAM,UAAU,CAAC,KAAa,OAA+C;AAE3E,oBAAgB,IAAI,KAAK,EAAE;AAG3B,UAAM,kBAAkB,SAAS,IAAI,GAAG;AACxC,QAAI,oBAAoB,QAAW;AACjC,mBAAa,eAAe;AAAA,IAC9B;AAGA,aAAS;AAAA,MACP;AAAA,MACA,WAAW,MAAM,kBAAkB,GAAG,GAAG,KAAK;AAAA,IAChD;AAGA,QAAI,YAAY,UAAa,CAAC,gBAAgB,IAAI,GAAG,GAAG;AACtD,sBAAgB;AAAA,QACd;AAAA,QACA,WAAW,MAAM,kBAAkB,GAAG,GAAG,OAAO;AAAA,MAClD;AAAA,IACF;AAAA,EACF;AAEA,SAAO,EAAE,SAAS,QAAQ;AAC5B;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Find all strings wrapped in {} or {{}} in value.
|
|
3
|
+
*
|
|
4
|
+
* @param value - The string to find variables in
|
|
5
|
+
* @param includePath - Whether to include path variables {single}
|
|
6
|
+
* @param includeEnv - Whether to include environment variables {{double}}
|
|
3
7
|
*/
|
|
4
|
-
export declare const findVariables: (value: string
|
|
8
|
+
export declare const findVariables: (value: string, { includePath, includeEnv }?: {
|
|
9
|
+
includePath?: boolean;
|
|
10
|
+
includeEnv?: boolean;
|
|
11
|
+
}) => string[];
|
|
5
12
|
//# sourceMappingURL=find-variables.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"find-variables.d.ts","sourceRoot":"","sources":["../../src/regex/find-variables.ts"],"names":[],"mappings":"AAEA
|
|
1
|
+
{"version":3,"file":"find-variables.d.ts","sourceRoot":"","sources":["../../src/regex/find-variables.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,eAAO,MAAM,aAAa,GACxB,OAAO,MAAM,EACb,8BAA2C;IAAE,WAAW,CAAC,EAAE,OAAO,CAAC;IAAC,UAAU,CAAC,EAAE,OAAO,CAAA;CAAO,KAC9F,MAAM,EAON,CAAA"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { REGEX } from "./regex-helpers.js";
|
|
2
|
-
const findVariables = (value) => [
|
|
2
|
+
const findVariables = (value, { includePath = true, includeEnv = true } = {}) => [includePath && REGEX.PATH, includeEnv && REGEX.VARIABLES].flatMap(
|
|
3
|
+
(regex) => regex ? [...value.matchAll(regex)].map((match) => match[1]?.trim()).filter((variable) => variable !== void 0) : []
|
|
4
|
+
);
|
|
3
5
|
export {
|
|
4
6
|
findVariables
|
|
5
7
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/regex/find-variables.ts"],
|
|
4
|
-
"sourcesContent": ["import { REGEX } from './regex-helpers'\n\n/**\n * Find all strings wrapped in {} or {{}} in value.\n */\nexport const findVariables = (value: string) =>\n [
|
|
5
|
-
"mappings": "AAAA,SAAS,aAAa;
|
|
4
|
+
"sourcesContent": ["import { REGEX } from './regex-helpers'\n\n/**\n * Find all strings wrapped in {} or {{}} in value.\n *\n * @param value - The string to find variables in\n * @param includePath - Whether to include path variables {single}\n * @param includeEnv - Whether to include environment variables {{double}}\n */\nexport const findVariables = (\n value: string,\n { includePath = true, includeEnv = true }: { includePath?: boolean; includeEnv?: boolean } = {},\n): string[] =>\n [includePath && REGEX.PATH, includeEnv && REGEX.VARIABLES].flatMap((regex) =>\n regex\n ? [...value.matchAll(regex)]\n .map((match) => match[1]?.trim())\n .filter((variable): variable is string => variable !== undefined)\n : [],\n )\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,aAAa;AASf,MAAM,gBAAgB,CAC3B,OACA,EAAE,cAAc,MAAM,aAAa,KAAK,IAAqD,CAAC,MAE9F,CAAC,eAAe,MAAM,MAAM,cAAc,MAAM,SAAS,EAAE;AAAA,EAAQ,CAAC,UAClE,QACI,CAAC,GAAG,MAAM,SAAS,KAAK,CAAC,EACtB,IAAI,CAAC,UAAU,MAAM,CAAC,GAAG,KAAK,CAAC,EAC/B,OAAO,CAAC,aAAiC,aAAa,MAAS,IAClE,CAAC;AACP;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,12 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Collection of regular expressions used throughout the application.
|
|
3
|
+
* These patterns handle URL parsing, variable detection, and reference path extraction.
|
|
4
|
+
*/
|
|
1
5
|
export declare const REGEX: {
|
|
2
6
|
/** Checks for a valid scheme */
|
|
3
7
|
readonly PROTOCOL: RegExp;
|
|
4
8
|
/** Finds multiple slashes after the scheme to replace with a single slash */
|
|
5
9
|
readonly MULTIPLE_SLASHES: RegExp;
|
|
10
|
+
/** Finds all variables wrapped in {{double}} */
|
|
6
11
|
readonly VARIABLES: RegExp;
|
|
12
|
+
/** Finds all variables wrapped in {single} */
|
|
7
13
|
readonly PATH: RegExp;
|
|
8
14
|
/** Finds the name of the schema from the ref path */
|
|
9
15
|
readonly REF_NAME: RegExp;
|
|
16
|
+
/** Finds template variables in multiple formats: {{var}}, {var}, or :var */
|
|
10
17
|
readonly TEMPLATE_VARIABLE: RegExp;
|
|
11
18
|
};
|
|
12
19
|
//# sourceMappingURL=regex-helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"regex-helpers.d.ts","sourceRoot":"","sources":["../../src/regex/regex-helpers.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,KAAK;IAChB,gCAAgC;;IAEhC,6EAA6E
|
|
1
|
+
{"version":3,"file":"regex-helpers.d.ts","sourceRoot":"","sources":["../../src/regex/regex-helpers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,KAAK;IAChB,gCAAgC;;IAEhC,6EAA6E;;IAE7E,gDAAgD;;IAEhD,8CAA8C;;IAE9C,qDAAqD;;IAErD,4EAA4E;;CAEpE,CAAA"}
|
|
@@ -3,10 +3,13 @@ const REGEX = {
|
|
|
3
3
|
PROTOCOL: /^(?:https?|ftp|file|mailto|tel|data|wss?)*:\/\//,
|
|
4
4
|
/** Finds multiple slashes after the scheme to replace with a single slash */
|
|
5
5
|
MULTIPLE_SLASHES: /(?<!:)\/{2,}/g,
|
|
6
|
+
/** Finds all variables wrapped in {{double}} */
|
|
6
7
|
VARIABLES: /{{((?:[^{}]|{[^{}]*})*)}}/g,
|
|
8
|
+
/** Finds all variables wrapped in {single} */
|
|
7
9
|
PATH: /(?:{)([^{}]+)}(?!})/g,
|
|
8
10
|
/** Finds the name of the schema from the ref path */
|
|
9
11
|
REF_NAME: /\/([^\/]+)$/,
|
|
12
|
+
/** Finds template variables in multiple formats: {{var}}, {var}, or :var */
|
|
10
13
|
TEMPLATE_VARIABLE: /{{\s*([^}\s]+?)\s*}}|{\s*([^}\s]+?)\s*}|:\b[\w.]+\b/g
|
|
11
14
|
};
|
|
12
15
|
export {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/regex/regex-helpers.ts"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["/**\n * Collection of regular expressions used throughout the application.\n * These patterns handle URL parsing, variable detection, and reference path extraction.\n */\nexport const REGEX = {\n /** Checks for a valid scheme */\n PROTOCOL: /^(?:https?|ftp|file|mailto|tel|data|wss?)*:\\/\\//,\n /** Finds multiple slashes after the scheme to replace with a single slash */\n MULTIPLE_SLASHES: /(?<!:)\\/{2,}/g,\n /** Finds all variables wrapped in {{double}} */\n VARIABLES: /{{((?:[^{}]|{[^{}]*})*)}}/g,\n /** Finds all variables wrapped in {single} */\n PATH: /(?:{)([^{}]+)}(?!})/g,\n /** Finds the name of the schema from the ref path */\n REF_NAME: /\\/([^\\/]+)$/,\n /** Finds template variables in multiple formats: {{var}}, {var}, or :var */\n TEMPLATE_VARIABLE: /{{\\s*([^}\\s]+?)\\s*}}|{\\s*([^}\\s]+?)\\s*}|:\\b[\\w.]+\\b/g,\n} as const\n"],
|
|
5
|
+
"mappings": "AAIO,MAAM,QAAQ;AAAA;AAAA,EAEnB,UAAU;AAAA;AAAA,EAEV,kBAAkB;AAAA;AAAA,EAElB,WAAW;AAAA;AAAA,EAEX,MAAM;AAAA;AAAA,EAEN,UAAU;AAAA;AAAA,EAEV,mBAAmB;AACrB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A list of preset styles for word breaks
|
|
3
|
+
*/
|
|
4
|
+
declare const PRESETS: {
|
|
5
|
+
/** Breaks on `/` and `-` for urls or file paths */
|
|
6
|
+
readonly path: RegExp;
|
|
7
|
+
/**
|
|
8
|
+
* Breaks on capitals, `_` and `.` for properties written in
|
|
9
|
+
* camel, pascal or snake case
|
|
10
|
+
*/
|
|
11
|
+
readonly property: RegExp;
|
|
12
|
+
};
|
|
13
|
+
/** Word break options */
|
|
14
|
+
type WordBreakOptions = {
|
|
15
|
+
/**
|
|
16
|
+
* Presets for word wrapping
|
|
17
|
+
*/
|
|
18
|
+
preset?: keyof typeof PRESETS;
|
|
19
|
+
/**
|
|
20
|
+
* Explicit regex to allow wrapping on, overrides any `preset`
|
|
21
|
+
*/
|
|
22
|
+
regex?: RegExp;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* String utility to add word break opportunities
|
|
26
|
+
*
|
|
27
|
+
* Adds a zero-width space before certain characters to allow improved
|
|
28
|
+
* line wrapping in the. Allows wrapping on "/" and * "-" by default.
|
|
29
|
+
*/
|
|
30
|
+
export declare const addWordBreaks: (label: string, opts?: WordBreakOptions) => string;
|
|
31
|
+
export {};
|
|
32
|
+
//# sourceMappingURL=add-word-breaks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"add-word-breaks.d.ts","sourceRoot":"","sources":["../../src/string/add-word-breaks.ts"],"names":[],"mappings":"AAOA;;GAEG;AACH,QAAA,MAAM,OAAO;IACX,mDAAmD;;IAEnD;;;OAGG;;CAEsC,CAAA;AAE3C,yBAAyB;AACzB,KAAK,gBAAgB,GAAG;IACtB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,OAAO,OAAO,CAAA;IAC7B;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;CACf,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,aAAa,GAAI,OAAO,MAAM,EAAE,OAAM,gBAAqB,KAAG,MAI1E,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const ZWSP = "\u200B";
|
|
2
|
+
const PRESETS = {
|
|
3
|
+
/** Breaks on `/` and `-` for urls or file paths */
|
|
4
|
+
"path": /[\/-]/,
|
|
5
|
+
/**
|
|
6
|
+
* Breaks on capitals, `_` and `.` for properties written in
|
|
7
|
+
* camel, pascal or snake case
|
|
8
|
+
*/
|
|
9
|
+
"property": /[A-Z\_\.-]/
|
|
10
|
+
};
|
|
11
|
+
const addWordBreaks = (label, opts = {}) => {
|
|
12
|
+
const { preset = "path", regex } = opts;
|
|
13
|
+
const wrapRegex = new RegExp(regex ?? PRESETS[preset], "g");
|
|
14
|
+
return label.replace(wrapRegex, `${ZWSP}$&`);
|
|
15
|
+
};
|
|
16
|
+
export {
|
|
17
|
+
addWordBreaks
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=add-word-breaks.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/string/add-word-breaks.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Unicode character for zero-width space\n *\n * @see https://en.wikipedia.org/wiki/Zero-width_space\n */\nconst ZWSP = '\\u200B'\n\n/**\n * A list of preset styles for word breaks\n */\nconst PRESETS = {\n /** Breaks on `/` and `-` for urls or file paths */\n 'path': /[\\/-]/,\n /**\n * Breaks on capitals, `_` and `.` for properties written in\n * camel, pascal or snake case\n */\n 'property': /[A-Z\\_\\.-]/,\n} as const satisfies Record<string, RegExp>\n\n/** Word break options */\ntype WordBreakOptions = {\n /**\n * Presets for word wrapping\n */\n preset?: keyof typeof PRESETS\n /**\n * Explicit regex to allow wrapping on, overrides any `preset`\n */\n regex?: RegExp\n}\n\n/**\n * String utility to add word break opportunities\n *\n * Adds a zero-width space before certain characters to allow improved\n * line wrapping in the. Allows wrapping on \"/\" and * \"-\" by default.\n */\nexport const addWordBreaks = (label: string, opts: WordBreakOptions = {}): string => {\n const { preset = 'path', regex } = opts\n const wrapRegex = new RegExp(regex ?? PRESETS[preset], 'g')\n return label.replace(wrapRegex, `${ZWSP}$&`)\n}\n"],
|
|
5
|
+
"mappings": "AAKA,MAAM,OAAO;AAKb,MAAM,UAAU;AAAA;AAAA,EAEd,QAAQ;AAAA;AAAA;AAAA;AAAA;AAAA,EAKR,YAAY;AACd;AAoBO,MAAM,gBAAgB,CAAC,OAAe,OAAyB,CAAC,MAAc;AACnF,QAAM,EAAE,SAAS,QAAQ,MAAM,IAAI;AACnC,QAAM,YAAY,IAAI,OAAO,SAAS,QAAQ,MAAM,GAAG,GAAG;AAC1D,SAAO,MAAM,QAAQ,WAAW,GAAG,IAAI,IAAI;AAC7C;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Truncates a string to a specified length and adds an ellipsis if it's longer in JS
|
|
3
|
+
*
|
|
4
|
+
* @param str - The string to truncate
|
|
5
|
+
* @param maxLength - The maximum length before truncation (default: 18)
|
|
6
|
+
* @returns The truncated string with ellipsis if needed
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* truncate('Very long name that needs truncation') // 'Very long name th…'
|
|
10
|
+
* truncate('Short') // 'Short'
|
|
11
|
+
*/
|
|
12
|
+
export declare const truncate: (str: string, maxLength?: number) => string;
|
|
13
|
+
//# sourceMappingURL=truncate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"truncate.d.ts","sourceRoot":"","sources":["../../src/string/truncate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,QAAQ,GAAI,KAAK,MAAM,EAAE,kBAAc,KAAG,MAKtD,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/string/truncate.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Truncates a string to a specified length and adds an ellipsis if it's longer in JS\n *\n * @param str - The string to truncate\n * @param maxLength - The maximum length before truncation (default: 18)\n * @returns The truncated string with ellipsis if needed\n *\n * @example\n * truncate('Very long name that needs truncation') // 'Very long name th\u2026'\n * truncate('Short') // 'Short'\n */\nexport const truncate = (str: string, maxLength = 18): string => {\n if (str.length <= maxLength) {\n return str\n }\n return str.slice(0, maxLength) + '\u2026'\n}\n"],
|
|
5
|
+
"mappings": "AAWO,MAAM,WAAW,CAAC,KAAa,YAAY,OAAe;AAC/D,MAAI,IAAI,UAAU,WAAW;AAC3B,WAAO;AAAA,EACT;AACA,SAAO,IAAI,MAAM,GAAG,SAAS,IAAI;AACnC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|