@visulima/string 3.0.0-alpha.10 → 3.0.0-alpha.12

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.
Files changed (114) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/LICENSE.md +68 -3
  3. package/dist/align-text.d.ts +35 -0
  4. package/dist/align-text.js +2 -34
  5. package/dist/case/camel-case.d.ts +17 -2
  6. package/dist/case/camel-case.js +1 -55
  7. package/dist/case/capital-case.d.ts +14 -2
  8. package/dist/case/capital-case.js +1 -47
  9. package/dist/case/constant-case.d.ts +14 -2
  10. package/dist/case/constant-case.js +1 -7
  11. package/dist/case/dot-case.d.ts +14 -2
  12. package/dist/case/dot-case.js +1 -7
  13. package/dist/case/flat-case.d.ts +12 -2
  14. package/dist/case/flat-case.js +1 -7
  15. package/dist/case/flip-case.d.ts +16 -2
  16. package/dist/case/flip-case.js +1 -82
  17. package/dist/case/identify-case.d.ts +15 -2
  18. package/dist/case/identify-case.js +1 -66
  19. package/dist/case/index.d.ts +8 -2
  20. package/dist/case/index.js +1 -20
  21. package/dist/case/kebab-case.d.ts +25 -2
  22. package/dist/case/kebab-case.js +1 -55
  23. package/dist/case/lower-first.d.ts +13 -2
  24. package/dist/case/lower-first.js +1 -9
  25. package/dist/case/no-case.d.ts +14 -2
  26. package/dist/case/no-case.js +1 -7
  27. package/dist/case/pascal-case.d.ts +17 -2
  28. package/dist/case/pascal-case.js +1 -48
  29. package/dist/case/pascal-snake-case.d.ts +14 -2
  30. package/dist/case/pascal-snake-case.js +1 -28
  31. package/dist/case/path-case.d.ts +14 -2
  32. package/dist/case/path-case.js +1 -7
  33. package/dist/case/sentence-case.d.ts +14 -2
  34. package/dist/case/sentence-case.js +1 -54
  35. package/dist/case/snake-case.d.ts +14 -2
  36. package/dist/case/snake-case.js +1 -7
  37. package/dist/case/split-by-case.d.ts +29 -2
  38. package/dist/case/split-by-case.js +1 -763
  39. package/dist/case/title-case.d.ts +12 -2
  40. package/dist/case/title-case.js +1 -40
  41. package/dist/case/train-case.d.ts +14 -2
  42. package/dist/case/train-case.js +1 -40
  43. package/dist/case/upper-first.d.ts +13 -2
  44. package/dist/case/upper-first.js +1 -9
  45. package/dist/closest-string.d.ts +32 -0
  46. package/dist/closest-string.js +1 -22
  47. package/dist/compare-similarity.d.ts +38 -1
  48. package/dist/compare-similarity.js +1 -12
  49. package/dist/get-string-truncated-width.d.ts +151 -0
  50. package/dist/get-string-truncated-width.js +1 -332
  51. package/dist/get-string-width.d.ts +58 -0
  52. package/dist/get-string-width.js +1 -5
  53. package/dist/index.d.ts +194 -9
  54. package/dist/index.js +1 -44
  55. package/dist/native-string-types.d.ts +239 -0
  56. package/dist/outdent.d.ts +74 -0
  57. package/dist/outdent.js +1 -109
  58. package/dist/packem_shared/LRUCache-Bjo6gsbC.js +1 -0
  59. package/dist/packem_shared/closest-uarbVSbs.js +1 -0
  60. package/dist/packem_shared/closestN-Vd5oNJy_.js +1 -0
  61. package/dist/packem_shared/constants-DmzZF6_u.js +1 -0
  62. package/dist/packem_shared/countOccurrences-DIHSCdcG.js +1 -0
  63. package/dist/packem_shared/direction-dtczwUOE.js +1 -0
  64. package/dist/packem_shared/eastAsianWidthType-lcBsj7vg.js +1 -0
  65. package/dist/packem_shared/excerpt-DhV2yoHG.js +54 -0
  66. package/dist/packem_shared/generate-cache-key-DPByFaMH.js +1 -0
  67. package/dist/packem_shared/indent-CDlPQmpe.js +1 -0
  68. package/dist/packem_shared/isFullwidthCodePoint-Dmsf2-5L.js +1 -0
  69. package/dist/packem_shared/joinSegments-D27SxoCV.js +1 -0
  70. package/dist/packem_shared/lookup-ChWuUpyb.js +1 -0
  71. package/dist/packem_shared/normalize-german-eszett-AKfdw2MZ.js +1 -0
  72. package/dist/packem_shared/redent-BECVOaZp.js +1 -0
  73. package/dist/packem_shared/replaceString-aq-C7Wor.js +1 -0
  74. package/dist/packem_shared/stripIndent-Dt8hSxVM.js +1 -0
  75. package/dist/packem_shared/transliterate-D1p1yYE7.js +5 -0
  76. package/dist/packem_shared/types.d-FOLSY2Tc.d.ts +392 -0
  77. package/dist/packem_shared/types.d-NJ4vcG-A.d.ts +346 -0
  78. package/dist/slice.d.ts +42 -0
  79. package/dist/slice.js +1 -415
  80. package/dist/slugify.d.ts +10 -1
  81. package/dist/slugify.js +1 -46
  82. package/dist/test/utils.d.ts +17 -0
  83. package/dist/test/utils.js +13 -88
  84. package/dist/test/vitest.d.ts +3 -0
  85. package/dist/test/vitest.js +1 -5
  86. package/dist/truncate.d.ts +43 -0
  87. package/dist/truncate.js +1 -112
  88. package/dist/utilities.d.ts +28 -1
  89. package/dist/utilities.js +1 -52
  90. package/dist/word-similarity-sort.d.ts +34 -0
  91. package/dist/word-similarity-sort.js +1 -22
  92. package/dist/word-wrap.d.ts +44 -0
  93. package/dist/word-wrap.js +8 -639
  94. package/package.json +46 -46
  95. package/dist/packem_shared/LRUCache-3r3SovvR.js +0 -46
  96. package/dist/packem_shared/closest-IHuSvnSn.js +0 -139
  97. package/dist/packem_shared/closestN-DUsjgiXb.js +0 -25
  98. package/dist/packem_shared/constants-BsZS5f0z.js +0 -95
  99. package/dist/packem_shared/countOccurrences-CS9XprQN.js +0 -22
  100. package/dist/packem_shared/direction-C9gfWWTK.js +0 -16
  101. package/dist/packem_shared/eastAsianWidthType-SD3YniiT.js +0 -20
  102. package/dist/packem_shared/excerpt-COsVNttN.js +0 -1502
  103. package/dist/packem_shared/generate-cache-key-DdayDfjV.js +0 -6
  104. package/dist/packem_shared/indent-D3DSCa6s.js +0 -33
  105. package/dist/packem_shared/isFullwidthCodePoint-IFXZxNjq.js +0 -10
  106. package/dist/packem_shared/joinSegments-CvxSgKz0.js +0 -44
  107. package/dist/packem_shared/lookup-BE8lEqy4.js +0 -104
  108. package/dist/packem_shared/normalize-german-eszett-4p3yKEzn.js +0 -8
  109. package/dist/packem_shared/redent-DSb1IJWn.js +0 -8
  110. package/dist/packem_shared/replaceString-Ce8X2ZQ-.js +0 -192
  111. package/dist/packem_shared/stripIndent-D_TRjxZ_.js +0 -20
  112. package/dist/packem_shared/transliterate-CKt9VK7D.js +0 -47756
  113. package/dist/packem_shared/types.d-BVNv675q.d.ts +0 -61
  114. package/dist/packem_shared/types.d-DfgbcNKh.d.ts +0 -57
@@ -1,5 +1,15 @@
1
- import { C as CaseOptions, T as TitleCase } from "../packem_shared/types.d-DfgbcNKh.js";
2
- import "../packem_shared/types.d-BVNv675q.js";
1
+ import { C as CaseOptions, T as TitleCase } from "../packem_shared/types.d-FOLSY2Tc.js";
2
+ import "../packem_shared/types.d-NJ4vcG-A.js";
3
3
  import "../packem_shared/lru-cache.d-BsP6kbVJ.js";
4
+ /**
5
+ * With Title Case all words are capitalized, except for minor words.
6
+ * @example
7
+ * ```typescript
8
+ * titleCase("this-IS-aTitle") // => "This is a Title"
9
+ * titleCase("XMLHttpRequest") // => "XML Http Request"
10
+ * titleCase("AJAXRequest") // => "AJAX Request"
11
+ * titleCase("QueryXML123String") // => "Query XML 123 String"
12
+ * ```
13
+ */
4
14
  declare function titleCase<T extends string = string>(value?: T, options?: CaseOptions): TitleCase<T>;
5
15
  export { titleCase as default };
@@ -1,40 +1 @@
1
- import LRUCache from '../packem_shared/LRUCache-3r3SovvR.js';
2
- import { splitByCase } from './split-by-case.js';
3
- import upperFirst from './upper-first.js';
4
- import { g as generateCacheKey } from '../packem_shared/generate-cache-key-DdayDfjV.js';
5
- import { n as normalizeGermanEszett } from '../packem_shared/normalize-german-eszett-4p3yKEzn.js';
6
-
7
- const defaultCacheStore = new LRUCache(1e3);
8
- function titleCase(value, options) {
9
- if (typeof value !== "string") {
10
- return "";
11
- }
12
- const shouldCache = options?.cache ?? false;
13
- const cacheStore = options?.cacheStore ?? defaultCacheStore;
14
- let cacheKey;
15
- if (shouldCache) {
16
- cacheKey = generateCacheKey(value, options);
17
- }
18
- if (shouldCache && cacheKey && cacheStore.has(cacheKey)) {
19
- return cacheStore.get(cacheKey);
20
- }
21
- const result = splitByCase(value, {
22
- handleAnsi: options?.handleAnsi,
23
- handleEmoji: options?.handleEmoji,
24
- knownAcronyms: options?.knownAcronyms,
25
- locale: options?.locale,
26
- normalize: options?.normalize,
27
- separators: void 0,
28
- stripAnsi: options?.stripAnsi,
29
- stripEmoji: options?.stripEmoji
30
- }).map((word) => {
31
- const normalized = options?.locale?.startsWith("de") ? normalizeGermanEszett(word) : word;
32
- return upperFirst(options?.locale ? normalized.toLocaleLowerCase(options.locale) : normalized.toLowerCase(), { locale: options?.locale });
33
- }).join(" ");
34
- if (shouldCache && cacheKey) {
35
- cacheStore.set(cacheKey, result);
36
- }
37
- return result;
38
- }
39
-
40
- export { titleCase as default };
1
+ var c=Object.defineProperty;var i=(r,o)=>c(r,"name",{value:o,configurable:!0});import m from"../packem_shared/LRUCache-Bjo6gsbC.js";import{splitByCase as p}from"./split-by-case.js";import f from"./upper-first.js";import{a as u}from"../packem_shared/generate-cache-key-DPByFaMH.js";import{l as d}from"../packem_shared/normalize-german-eszett-AKfdw2MZ.js";var h=Object.defineProperty,j=i((r,o)=>h(r,"name",{value:o,configurable:!0}),"c");const A=new m(1e3);function w(r,o){if(typeof r!="string")return"";const a=o?.cache??!1,e=o?.cacheStore??A;let t;if(a&&(t=u(r,o)),a&&t&&e.has(t))return e.get(t);const n=p(r,{handleAnsi:o?.handleAnsi,handleEmoji:o?.handleEmoji,knownAcronyms:o?.knownAcronyms,locale:o?.locale,normalize:o?.normalize,separators:void 0,stripAnsi:o?.stripAnsi,stripEmoji:o?.stripEmoji}).map(l=>{const s=o?.locale?.startsWith("de")?d(l):l;return f(o?.locale?s.toLocaleLowerCase(o.locale):s.toLowerCase(),{locale:o?.locale})}).join(" ");return a&&t&&e.set(t,n),n}i(w,"g");j(w,"titleCase");export{w as default};
@@ -1,5 +1,17 @@
1
- import { C as CaseOptions, j as TrainCase } from "../packem_shared/types.d-DfgbcNKh.js";
2
- import "../packem_shared/types.d-BVNv675q.js";
1
+ import { C as CaseOptions, j as TrainCase } from "../packem_shared/types.d-FOLSY2Tc.js";
2
+ import "../packem_shared/types.d-NJ4vcG-A.js";
3
3
  import "../packem_shared/lru-cache.d-BsP6kbVJ.js";
4
+ /**
5
+ * Converts a string to Train-Case.
6
+ * @example
7
+ * ```typescript
8
+ * trainCase("foo bar") // => "Foo-Bar"
9
+ * trainCase("foo-bar") // => "Foo-Bar"
10
+ * trainCase("foo_bar") // => "Foo-Bar"
11
+ * trainCase("XMLHttpRequest") // => "XML-Http-Request"
12
+ * trainCase("AJAXRequest") // => "AJAX-Request"
13
+ * trainCase("QueryXML123String") // => "Query-XML-123-String"
14
+ * ```
15
+ */
4
16
  declare const trainCase: <T extends string = string>(value?: T, options?: CaseOptions) => TrainCase<T>;
5
17
  export { trainCase as default };
@@ -1,40 +1 @@
1
- import LRUCache from '../packem_shared/LRUCache-3r3SovvR.js';
2
- import { splitByCase } from './split-by-case.js';
3
- import upperFirst from './upper-first.js';
4
- import { g as generateCacheKey } from '../packem_shared/generate-cache-key-DdayDfjV.js';
5
- import joinSegments from '../packem_shared/joinSegments-CvxSgKz0.js';
6
-
7
- const defaultCacheStore = new LRUCache(1e3);
8
- const trainCase = (value, options) => {
9
- if (typeof value !== "string") {
10
- return "";
11
- }
12
- const shouldCache = options?.cache ?? false;
13
- const cacheStore = options?.cacheStore ?? defaultCacheStore;
14
- let cacheKey;
15
- if (shouldCache) {
16
- cacheKey = generateCacheKey(value, options);
17
- }
18
- if (shouldCache && cacheKey && cacheStore.has(cacheKey)) {
19
- return cacheStore.get(cacheKey);
20
- }
21
- const result = joinSegments(
22
- splitByCase(value, {
23
- handleAnsi: options?.handleAnsi,
24
- handleEmoji: options?.handleEmoji,
25
- knownAcronyms: options?.knownAcronyms,
26
- locale: options?.locale,
27
- normalize: options?.normalize,
28
- separators: void 0,
29
- stripAnsi: options?.stripAnsi,
30
- stripEmoji: options?.stripEmoji
31
- }).map((p) => upperFirst(p, { locale: options?.locale })),
32
- "-"
33
- );
34
- if (shouldCache && cacheKey) {
35
- cacheStore.set(cacheKey, result);
36
- }
37
- return result;
38
- };
39
-
40
- export { trainCase as default };
1
+ var m=Object.defineProperty;var e=(o,r)=>m(o,"name",{value:r,configurable:!0});import l from"../packem_shared/LRUCache-Bjo6gsbC.js";import{splitByCase as p}from"./split-by-case.js";import c from"./upper-first.js";import{a as f}from"../packem_shared/generate-cache-key-DPByFaMH.js";import h from"../packem_shared/joinSegments-D27SxoCV.js";var d=Object.defineProperty,u=e((o,r)=>d(o,"name",{value:r,configurable:!0}),"i");const A=new l(1e3),C=u((o,r)=>{if(typeof o!="string")return"";const n=r?.cache??!1,i=r?.cacheStore??A;let t;if(n&&(t=f(o,r)),n&&t&&i.has(t))return i.get(t);const a=h(p(o,{handleAnsi:r?.handleAnsi,handleEmoji:r?.handleEmoji,knownAcronyms:r?.knownAcronyms,locale:r?.locale,normalize:r?.normalize,separators:void 0,stripAnsi:r?.stripAnsi,stripEmoji:r?.stripEmoji}).map(s=>c(s,{locale:r?.locale})),"-");return n&&t&&i.set(t,a),a},"trainCase");export{C as default};
@@ -1,5 +1,16 @@
1
- import { L as LocaleOptions, U as UpperFirst } from "../packem_shared/types.d-DfgbcNKh.js";
2
- import "../packem_shared/types.d-BVNv675q.js";
1
+ import { L as LocaleOptions, U as UpperFirst } from "../packem_shared/types.d-FOLSY2Tc.js";
2
+ import "../packem_shared/types.d-NJ4vcG-A.js";
3
3
  import "../packem_shared/lru-cache.d-BsP6kbVJ.js";
4
+ /**
5
+ * Converts first character to upper case.
6
+ * @param value The string to convert.
7
+ * @param options Options for case conversion.
8
+ * @returns The string with its first character converted to uppercase.
9
+ * @example
10
+ * ```typescript
11
+ * upperFirst("hello world!") // => "Hello world!"
12
+ * upperFirst("istanbul", { locale: "tr" }) // => "İstanbul"
13
+ * ```
14
+ */
4
15
  declare const upperFirst: <T extends string = string>(value?: T, options?: LocaleOptions) => UpperFirst<T>;
5
16
  export { upperFirst as default };
@@ -1,9 +1 @@
1
- const upperFirst = (value, options) => {
2
- if (typeof value !== "string" || value === "") {
3
- return "";
4
- }
5
- const firstChar = options?.locale ? value[0].toLocaleUpperCase(options.locale) : value[0].toUpperCase();
6
- return firstChar + value.slice(1);
7
- };
8
-
9
- export { upperFirst as default };
1
+ var p=Object.defineProperty;var o=(e,a)=>p(e,"name",{value:a,configurable:!0});var t=Object.defineProperty,l=o((e,a)=>t(e,"name",{value:a,configurable:!0}),"s");const s=l((e,a)=>typeof e!="string"||e===""?"":(a?.locale?e[0].toLocaleUpperCase(a.locale):e[0].toUpperCase())+e.slice(1),"upperFirst");export{s as default};
@@ -1,6 +1,38 @@
1
+ /** Options for {@linkcode closestString}. */
1
2
  interface ClosestStringOptions {
3
+ /**
4
+ * Whether the distance should include case.
5
+ * @default {false}
6
+ */
2
7
  caseSensitive?: boolean;
8
+ /**
9
+ * A custom comparison function to use for comparing strings.
10
+ * @param a The first string for comparison.
11
+ * @param b The second string for comparison.
12
+ * @returns The distance between the two strings.
13
+ * @default {levenshteinDistance}
14
+ */
3
15
  compareFn?: (a: string, b: string) => number;
4
16
  }
17
+ /**
18
+ * Finds the most similar string from an array of strings.
19
+ *
20
+ * By default, calculates the distance between words using the
21
+ * {@link https://en.wikipedia.org/wiki/Levenshtein_distance | Levenshtein distance}.
22
+ * @example Usage
23
+ * ```ts
24
+ * import { closestString } from "@visulima/string";
25
+ * import assert from "node:assert";
26
+ *
27
+ * const possibleWords = ["length", "size", "blah", "help"];
28
+ * const suggestion = closestString("hep", possibleWords);
29
+ *
30
+ * assert.deepStrictEqual(suggestion, "help");
31
+ * ```
32
+ * @param givenWord The string to measure distance against
33
+ * @param possibleWords The string-array to pick the closest string from
34
+ * @param options The options for the comparison.
35
+ * @returns The closest string from `possibleWords`, or `undefined` if `possibleWords` is empty (though the function throws in this case).
36
+ */
5
37
  declare const closestString: (givenWord: string, possibleWords: ReadonlyArray<string>, options?: ClosestStringOptions) => string | undefined;
6
38
  export { ClosestStringOptions, closestString };
@@ -1,22 +1 @@
1
- import { distance } from './packem_shared/closest-IHuSvnSn.js';
2
-
3
- const closestString = (givenWord, possibleWords, options) => {
4
- if (possibleWords.length === 0) {
5
- throw new TypeError("When using closestString(), the possibleWords array must contain at least one word");
6
- }
7
- const { caseSensitive, compareFn: compareFunction = distance } = { ...options };
8
- const normalizedGivenWord = caseSensitive ? givenWord : givenWord.toLowerCase();
9
- let nearestWord = possibleWords[0] ?? void 0;
10
- let closestStringDistance = Infinity;
11
- for (const possibleWord of possibleWords) {
12
- const each = possibleWord;
13
- const distance = caseSensitive ? compareFunction(normalizedGivenWord, each) : compareFunction(normalizedGivenWord, each.toLowerCase());
14
- if (distance < closestStringDistance) {
15
- nearestWord = each;
16
- closestStringDistance = distance;
17
- }
18
- }
19
- return nearestWord;
20
- };
21
-
22
- export { closestString };
1
+ var g=Object.defineProperty;var l=(t,e)=>g(t,"name",{value:e,configurable:!0});import{distance as p}from"./packem_shared/closest-uarbVSbs.js";var u=Object.defineProperty,m=l((t,e)=>u(t,"name",{value:e,configurable:!0}),"l");const h=m((t,e,f)=>{if(e.length===0)throw new TypeError("When using closestString(), the possibleWords array must contain at least one word");const{caseSensitive:r,compareFn:s=p}={...f},n=r?t:t.toLowerCase();let a=e[0]??void 0,i=1/0;for(const d of e){const o=d,c=r?s(n,o):s(n,o.toLowerCase());c<i&&(a=o,i=c)}return a},"closestString");export{h as closestString};
@@ -1,6 +1,43 @@
1
+ /** Options for {@linkcode compareSimilarity}. */
1
2
  interface CompareSimilarityOptions {
3
+ /**
4
+ * Whether the distance should include case.
5
+ * @default {false}
6
+ */
2
7
  caseSensitive?: boolean;
8
+ /**
9
+ * A custom comparison function to use for comparing strings.
10
+ * @param a The first string for comparison.
11
+ * @param b The second string for comparison.
12
+ * @returns The distance between the two strings.
13
+ * @default {levenshteinDistance}
14
+ */
3
15
  compareFn?: (a: string, b: string) => number;
4
16
  }
5
- declare const compareSimilarity: (givenWord: string, options?: CompareSimilarityOptions) => (a: string, b: string) => number;
17
+ /**
18
+ * Takes a string and generates a comparator function to determine which of two
19
+ * strings is more similar to the given one.
20
+ *
21
+ * By default, calculates the distance between words using the
22
+ * {@link https://en.wikipedia.org/wiki/Levenshtein_distance | Levenshtein distance}.
23
+ * @param givenWord The string to measure distance against.
24
+ * @param options Options for the sort.
25
+ * @returns The difference in distance. This will be a negative number if `a`
26
+ * is more similar to `givenWord` than `b`, a positive number if `b` is more
27
+ * similar, or `0` if they are equally similar.
28
+ * @example Usage
29
+ *
30
+ * Most-similar words will be at the start of the array.
31
+ *
32
+ * ```ts
33
+ * import { compareSimilarity } from "@visulima/string";
34
+ * import assert from "node:assert";
35
+ *
36
+ * const words = ["hi", "hello", "help"];
37
+ * const sortedWords = words.toSorted(compareSimilarity("hep"));
38
+ *
39
+ * assert.deepStrictEqual(sortedWords, ["help", "hi", "hello"]);
40
+ * ```
41
+ */
42
+ declare const compareSimilarity: (givenWord: string, options?: CompareSimilarityOptions) => ((a: string, b: string) => number);
6
43
  export { CompareSimilarityOptions, compareSimilarity };
@@ -1,12 +1 @@
1
- import { distance } from './packem_shared/closest-IHuSvnSn.js';
2
-
3
- const compareSimilarity = (givenWord, options) => {
4
- const { compareFn: compareFunction = distance } = { ...options };
5
- if (options?.caseSensitive) {
6
- return (a, b) => compareFunction(givenWord, a) - compareFunction(givenWord, b);
7
- }
8
- const normalizedGivenWord = givenWord.toLowerCase();
9
- return (a, b) => compareFunction(normalizedGivenWord, a.toLowerCase()) - compareFunction(normalizedGivenWord, b.toLowerCase());
10
- };
11
-
12
- export { compareSimilarity };
1
+ var s=Object.defineProperty;var n=(e,r)=>s(e,"name",{value:r,configurable:!0});import{distance as c}from"./packem_shared/closest-uarbVSbs.js";var m=Object.defineProperty,p=n((e,r)=>m(e,"name",{value:r,configurable:!0}),"a");const l=p((e,r)=>{const{compareFn:o=c}={...r};if(r?.caseSensitive)return(t,a)=>o(e,t)-o(e,a);const i=e.toLowerCase();return(t,a)=>o(i,t.toLowerCase())-o(i,a.toLowerCase())},"compareSimilarity");export{l as compareSimilarity};
@@ -1,23 +1,174 @@
1
+ /**
2
+ * Configuration options for string width calculation and truncation.
3
+ * @example
4
+ * ```typescript
5
+ * // Basic usage with defaults
6
+ * const options: StringTruncatedWidthOptions = {};
7
+ *
8
+ * // Custom character widths
9
+ * const options: StringTruncatedWidthOptions = {
10
+ * regularWidth: 2,
11
+ * emojiWidth: 3,
12
+ * tabWidth: 4
13
+ * };
14
+ *
15
+ * // Truncation settings
16
+ * const options: StringTruncatedWidthOptions = {
17
+ * limit: 10,
18
+ * ellipsis: '...',
19
+ * ellipsisWidth: 3
20
+ * };
21
+ * ```
22
+ */
1
23
  interface StringTruncatedWidthOptions {
24
+ /**
25
+ * Count [ambiguous width characters](https://www.unicode.org/reports/tr11/#Ambiguous) as having narrow width (count of 1 (regular)) instead of wide width (count of 2 (wide)).
26
+ * @default false
27
+ */
2
28
  ambiguousIsNarrow?: boolean;
29
+ /**
30
+ * Width of ANSI escape sequences
31
+ * @default 0
32
+ */
3
33
  ansiWidth?: number;
34
+ /**
35
+ * Width of control characters
36
+ * @default 0
37
+ */
4
38
  controlWidth?: number;
39
+ /**
40
+ * Whether to count ANSI escape codes in width calculation
41
+ * @default false
42
+ */
5
43
  countAnsiEscapeCodes?: boolean;
44
+ /**
45
+ * Text appended to the string when it is truncated
46
+ * @default ''
47
+ */
6
48
  ellipsis?: string;
49
+ /**
50
+ * Width of the ellipsis string
51
+ * If not provided, it will be calculated using getStringTruncatedWidth
52
+ */
7
53
  ellipsisWidth?: number;
54
+ /**
55
+ * Width of emoji characters
56
+ * @default 2
57
+ */
8
58
  emojiWidth?: number;
59
+ /**
60
+ * Width of full-width characters
61
+ * @default 2
62
+ */
9
63
  fullWidth?: number;
64
+ /**
65
+ * Width of half-width characters
66
+ * @default 1
67
+ */
10
68
  halfWidth?: number;
69
+ /**
70
+ * Maximum width limit for the string
71
+ * @default Infinity
72
+ */
11
73
  limit?: number;
74
+ /**
75
+ * Width of regular characters
76
+ * @default 1
77
+ */
12
78
  regularWidth?: number;
79
+ /**
80
+ * Width of tab characters
81
+ * @default 8
82
+ */
13
83
  tabWidth?: number;
84
+ /**
85
+ * Width of wide characters
86
+ * @default 2
87
+ */
14
88
  wideWidth?: number;
15
89
  }
90
+ /**
91
+ * Result object returned by getStringTruncatedWidth containing width calculation and the truncation details.
92
+ * @example
93
+ * ```typescript
94
+ * // No truncation
95
+ * const result: StringTruncatedWidthResult = {
96
+ * width: 5, // Total visual width
97
+ * truncated: false, // String was not truncated
98
+ * ellipsed: false, // No ellipsis added
99
+ * index: 5, // Full string length
100
+ * };
101
+ *
102
+ * // With truncation
103
+ * const result: StringTruncatedWidthResult = {
104
+ * width: 8, // Width including ellipsis
105
+ * truncated: true, // String was truncated
106
+ * ellipsed: true, // Ellipsis was added
107
+ * index: 5, // Truncation point
108
+ * };
109
+ * ```
110
+ */
16
111
  interface StringTruncatedWidthResult {
112
+ /**
113
+ * Whether an ellipsis was added
114
+ */
17
115
  ellipsed: boolean;
116
+ /**
117
+ * The index at which truncation occurred (if any)
118
+ */
18
119
  index: number;
120
+ /**
121
+ * Whether the string was truncated
122
+ */
19
123
  truncated: boolean;
124
+ /**
125
+ * The calculated width of the string
126
+ */
20
127
  width: number;
21
128
  }
129
+ /**
130
+ * Calculate the visual width of a string with optional truncation, handling various Unicode character types.
131
+ * @example
132
+ * ```typescript
133
+ * // Basic width calculation
134
+ * getStringTruncatedWidth('hello');
135
+ * // => { width: 5, truncated: false, ellipsed: false, index: 5 }
136
+ *
137
+ * // With truncation
138
+ * getStringTruncatedWidth('hello world', {
139
+ * limit: 8,
140
+ * ellipsis: '...'
141
+ * });
142
+ * // => { width: 8, truncated: true, ellipsed: true, index: 5 }
143
+ *
144
+ * // With custom character widths
145
+ * getStringTruncatedWidth('あいう', {
146
+ * fullWidth: 2,
147
+ * ambiguousIsNarrow: true
148
+ * });
149
+ * // => { width: 6, truncated: false, ellipsed: false, index: 3 }
150
+ *
151
+ * // With combining characters
152
+ * getStringTruncatedWidth('e\u0301'); // Latin e with acute
153
+ * // => { width: 1, truncated: false, ellipsed: false, index: 2 }
154
+ *
155
+ * getStringTruncatedWidth('ก\u0e31'); // Thai character with vowel mark
156
+ * // => { width: 1, truncated: false, ellipsed: false, index: 2 }
157
+ * ```
158
+ *
159
+ * Features:
160
+ * - Full Unicode support including combining marks from various scripts
161
+ * - Accurate width calculation for emoji sequences
162
+ * - ANSI escape code handling
163
+ * - Customizable character widths
164
+ * - Optional string truncation with ellipsis
165
+ * @param input The string to calculate the width for
166
+ * @param options Configuration options for width calculation and truncation
167
+ * @returns Result object containing:
168
+ * - width: The calculated visual width of the string
169
+ * - truncated: Whether the string was truncated
170
+ * - ellipsed: Whether an ellipsis was added
171
+ * - index: The index at which truncation occurred (if any)
172
+ */
22
173
  declare const getStringTruncatedWidth: (input: string, options?: StringTruncatedWidthOptions) => StringTruncatedWidthResult;
23
174
  export { StringTruncatedWidthOptions, StringTruncatedWidthResult, getStringTruncatedWidth };