@atom8n/utils 1.21.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.
Files changed (106) hide show
  1. package/README.md +25 -0
  2. package/dist/assert.cjs +3 -0
  3. package/dist/assert.d.cts +2 -0
  4. package/dist/assert.d.mts +2 -0
  5. package/dist/assert.mjs +3 -0
  6. package/dist/assert2.cjs +17 -0
  7. package/dist/assert2.cjs.map +1 -0
  8. package/dist/assert2.d.cts +5 -0
  9. package/dist/assert2.d.mts +5 -0
  10. package/dist/assert2.mjs +11 -0
  11. package/dist/assert2.mjs.map +1 -0
  12. package/dist/event-bus.cjs +3 -0
  13. package/dist/event-bus.d.cts +2 -0
  14. package/dist/event-bus.d.mts +2 -0
  15. package/dist/event-bus.mjs +3 -0
  16. package/dist/event-bus2.cjs +49 -0
  17. package/dist/event-bus2.cjs.map +1 -0
  18. package/dist/event-bus2.d.cts +14 -0
  19. package/dist/event-bus2.d.mts +14 -0
  20. package/dist/event-bus2.mjs +43 -0
  21. package/dist/event-bus2.mjs.map +1 -0
  22. package/dist/event-queue.cjs +3 -0
  23. package/dist/event-queue.d.cts +2 -0
  24. package/dist/event-queue.d.mts +2 -0
  25. package/dist/event-queue.mjs +3 -0
  26. package/dist/event-queue2.cjs +46 -0
  27. package/dist/event-queue2.cjs.map +1 -0
  28. package/dist/event-queue2.d.cts +7 -0
  29. package/dist/event-queue2.d.mts +7 -0
  30. package/dist/event-queue2.mjs +40 -0
  31. package/dist/event-queue2.mjs.map +1 -0
  32. package/dist/files/sanitize.cjs +3 -0
  33. package/dist/files/sanitize.d.cts +2 -0
  34. package/dist/files/sanitize.d.mts +2 -0
  35. package/dist/files/sanitize.mjs +3 -0
  36. package/dist/index.cjs +23 -0
  37. package/dist/index.d.cts +11 -0
  38. package/dist/index.d.mts +11 -0
  39. package/dist/index.mjs +12 -0
  40. package/dist/number/smartDecimal.cjs +3 -0
  41. package/dist/number/smartDecimal.d.cts +2 -0
  42. package/dist/number/smartDecimal.d.mts +2 -0
  43. package/dist/number/smartDecimal.mjs +3 -0
  44. package/dist/reRankSearchResults.cjs +25 -0
  45. package/dist/reRankSearchResults.cjs.map +1 -0
  46. package/dist/reRankSearchResults.d.cts +13 -0
  47. package/dist/reRankSearchResults.d.mts +13 -0
  48. package/dist/reRankSearchResults.mjs +19 -0
  49. package/dist/reRankSearchResults.mjs.map +1 -0
  50. package/dist/retry.cjs +3 -0
  51. package/dist/retry.d.cts +2 -0
  52. package/dist/retry.d.mts +2 -0
  53. package/dist/retry.mjs +3 -0
  54. package/dist/retry2.cjs +45 -0
  55. package/dist/retry2.cjs.map +1 -0
  56. package/dist/retry2.d.cts +6 -0
  57. package/dist/retry2.d.mts +6 -0
  58. package/dist/retry2.mjs +39 -0
  59. package/dist/retry2.mjs.map +1 -0
  60. package/dist/sanitize.cjs +67 -0
  61. package/dist/sanitize.cjs.map +1 -0
  62. package/dist/sanitize.d.cts +5 -0
  63. package/dist/sanitize.d.mts +5 -0
  64. package/dist/sanitize.mjs +61 -0
  65. package/dist/sanitize.mjs.map +1 -0
  66. package/dist/search/reRankSearchResults.cjs +3 -0
  67. package/dist/search/reRankSearchResults.d.cts +2 -0
  68. package/dist/search/reRankSearchResults.d.mts +2 -0
  69. package/dist/search/reRankSearchResults.mjs +3 -0
  70. package/dist/search/sublimeSearch.cjs +4 -0
  71. package/dist/search/sublimeSearch.d.cts +2 -0
  72. package/dist/search/sublimeSearch.d.mts +2 -0
  73. package/dist/search/sublimeSearch.mjs +3 -0
  74. package/dist/smartDecimal.cjs +16 -0
  75. package/dist/smartDecimal.cjs.map +1 -0
  76. package/dist/smartDecimal.d.cts +5 -0
  77. package/dist/smartDecimal.d.mts +5 -0
  78. package/dist/smartDecimal.mjs +10 -0
  79. package/dist/smartDecimal.mjs.map +1 -0
  80. package/dist/sort/sortByProperty.cjs +3 -0
  81. package/dist/sort/sortByProperty.d.cts +2 -0
  82. package/dist/sort/sortByProperty.d.mts +2 -0
  83. package/dist/sort/sortByProperty.mjs +3 -0
  84. package/dist/sortByProperty.cjs +18 -0
  85. package/dist/sortByProperty.cjs.map +1 -0
  86. package/dist/sortByProperty.d.cts +5 -0
  87. package/dist/sortByProperty.d.mts +5 -0
  88. package/dist/sortByProperty.mjs +12 -0
  89. package/dist/sortByProperty.mjs.map +1 -0
  90. package/dist/string/truncate.cjs +4 -0
  91. package/dist/string/truncate.d.cts +2 -0
  92. package/dist/string/truncate.d.mts +2 -0
  93. package/dist/string/truncate.mjs +3 -0
  94. package/dist/sublimeSearch.cjs +183 -0
  95. package/dist/sublimeSearch.cjs.map +1 -0
  96. package/dist/sublimeSearch.d.cts +15 -0
  97. package/dist/sublimeSearch.d.mts +15 -0
  98. package/dist/sublimeSearch.mjs +171 -0
  99. package/dist/sublimeSearch.mjs.map +1 -0
  100. package/dist/truncate.cjs +42 -0
  101. package/dist/truncate.cjs.map +1 -0
  102. package/dist/truncate.d.cts +6 -0
  103. package/dist/truncate.d.mts +6 -0
  104. package/dist/truncate.mjs +30 -0
  105. package/dist/truncate.mjs.map +1 -0
  106. package/package.json +53 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sublimeSearch.cjs","names":["bestRecursiveMatches: number[]","recursiveMatches: number[]","values: Array<{ value: string; weight: number }>"],"sources":["../src/search/sublimeSearch.ts"],"sourcesContent":["/*\n * Constants and utility functions used for searching for node types in node creator component\n * based on https://github.com/forrestthewoods/lib_fts/blob/master/code/fts_fuzzy_match.js\n */\n\nconst SEQUENTIAL_BONUS = 60; // bonus for adjacent matches\nconst SEPARATOR_BONUS = 38; // bonus if match occurs after a separator\nconst CAMEL_BONUS = 30; // bonus if match is uppercase and prev is lower\nconst FIRST_LETTER_BONUS = 15; // bonus if the first letter is matched\n\nconst LEADING_LETTER_PENALTY = -20; // penalty applied for every letter in str before the first match\nconst MAX_LEADING_LETTER_PENALTY = -200; // maximum penalty for leading letters\nconst UNMATCHED_LETTER_PENALTY = -5;\n\nexport const DEFAULT_KEYS = [\n\t{ key: 'properties.displayName', weight: 1.3 },\n\t{ key: 'properties.codex.alias', weight: 1 },\n];\n\n/**\n * Returns true if each character in pattern is found sequentially within target\n * @param {*} pattern string\n * @param {*} target string\n */\nfunction fuzzyMatchSimple(pattern: string, target: string): boolean {\n\tlet patternIdx = 0;\n\tlet strIdx = 0;\n\n\twhile (patternIdx < pattern.length && strIdx < target.length) {\n\t\tconst patternChar = pattern.charAt(patternIdx).toLowerCase();\n\t\tconst targetChar = target.charAt(strIdx).toLowerCase();\n\t\tif (patternChar === targetChar) {\n\t\t\tpatternIdx++;\n\t\t}\n\t\t++strIdx;\n\t}\n\n\treturn pattern.length !== 0 && target.length !== 0 && patternIdx === pattern.length;\n}\n\nfunction fuzzyMatchRecursive(\n\tpattern: string,\n\ttarget: string,\n\tpatternCurIndex: number,\n\ttargetCurrIndex: number,\n\ttargetMatches: null | number[],\n\tmatches: number[],\n\tmaxMatches: number,\n\tnextMatch: number,\n\trecursionCount: number,\n\trecursionLimit: number,\n): { matched: boolean; outScore: number } {\n\tlet outScore = 0;\n\n\t// Return if recursion limit is reached.\n\tif (++recursionCount >= recursionLimit) {\n\t\treturn { matched: false, outScore };\n\t}\n\n\t// Return if we reached ends of strings.\n\tif (patternCurIndex === pattern.length || targetCurrIndex === target.length) {\n\t\treturn { matched: false, outScore };\n\t}\n\n\t// Recursion params\n\tlet recursiveMatch = false;\n\tlet bestRecursiveMatches: number[] = [];\n\tlet bestRecursiveScore = 0;\n\n\t// Loop through pattern and str looking for a match.\n\tlet firstMatch = true;\n\twhile (patternCurIndex < pattern.length && targetCurrIndex < target.length) {\n\t\t// Match found.\n\t\tif (pattern[patternCurIndex].toLowerCase() === target[targetCurrIndex].toLowerCase()) {\n\t\t\tif (nextMatch >= maxMatches) {\n\t\t\t\treturn { matched: false, outScore };\n\t\t\t}\n\n\t\t\tif (firstMatch && targetMatches) {\n\t\t\t\tmatches = [...targetMatches];\n\t\t\t\tfirstMatch = false;\n\t\t\t}\n\n\t\t\tconst recursiveMatches: number[] = [];\n\t\t\tconst recursiveResult = fuzzyMatchRecursive(\n\t\t\t\tpattern,\n\t\t\t\ttarget,\n\t\t\t\tpatternCurIndex,\n\t\t\t\ttargetCurrIndex + 1,\n\t\t\t\tmatches,\n\t\t\t\trecursiveMatches,\n\t\t\t\tmaxMatches,\n\t\t\t\tnextMatch,\n\t\t\t\trecursionCount,\n\t\t\t\trecursionLimit,\n\t\t\t);\n\n\t\t\tconst recursiveScore = recursiveResult.outScore;\n\t\t\tif (recursiveResult.matched) {\n\t\t\t\t// Pick best recursive score.\n\t\t\t\tif (!recursiveMatch || recursiveScore > bestRecursiveScore) {\n\t\t\t\t\tbestRecursiveMatches = [...recursiveMatches];\n\t\t\t\t\tbestRecursiveScore = recursiveScore;\n\t\t\t\t}\n\t\t\t\trecursiveMatch = true;\n\t\t\t}\n\n\t\t\tmatches[nextMatch++] = targetCurrIndex;\n\t\t\t++patternCurIndex;\n\t\t}\n\t\t++targetCurrIndex;\n\t}\n\n\tconst matched = patternCurIndex === pattern.length;\n\n\tif (matched) {\n\t\toutScore = 100;\n\n\t\t// Apply leading letter penalty (if not n8n-prefixed)\n\t\tif (!target.toLowerCase().startsWith('n8n')) {\n\t\t\tlet penalty = LEADING_LETTER_PENALTY * matches[0];\n\t\t\tpenalty = penalty < MAX_LEADING_LETTER_PENALTY ? MAX_LEADING_LETTER_PENALTY : penalty;\n\t\t\toutScore += penalty;\n\t\t}\n\n\t\t//Apply unmatched penalty\n\t\tconst unmatched = target.length - nextMatch;\n\t\toutScore += UNMATCHED_LETTER_PENALTY * unmatched;\n\n\t\t// Apply ordering bonuses\n\t\tfor (let i = 0; i < nextMatch; i++) {\n\t\t\tconst currIdx = matches[i];\n\n\t\t\tif (i > 0) {\n\t\t\t\tconst prevIdx = matches[i - 1];\n\t\t\t\tif (currIdx === prevIdx + 1) {\n\t\t\t\t\toutScore += SEQUENTIAL_BONUS;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Check for bonuses based on neighbor character value.\n\t\t\tif (currIdx > 0) {\n\t\t\t\t// Camel case\n\t\t\t\tconst neighbor = target[currIdx - 1];\n\t\t\t\tconst curr = target[currIdx];\n\t\t\t\tif (neighbor !== neighbor.toUpperCase() && curr !== curr.toLowerCase()) {\n\t\t\t\t\toutScore += CAMEL_BONUS;\n\t\t\t\t}\n\t\t\t\tconst isNeighbourSeparator = neighbor === '_' || neighbor === ' ';\n\t\t\t\tif (isNeighbourSeparator) {\n\t\t\t\t\toutScore += SEPARATOR_BONUS;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// First letter\n\t\t\t\toutScore += FIRST_LETTER_BONUS;\n\t\t\t}\n\t\t}\n\n\t\t// Return best result\n\t\tif (recursiveMatch && (!matched || bestRecursiveScore > outScore)) {\n\t\t\t// Recursive score is better than \"this\"\n\t\t\tmatches = [...bestRecursiveMatches];\n\t\t\toutScore = bestRecursiveScore;\n\t\t\treturn { matched: true, outScore };\n\t\t} else if (matched) {\n\t\t\t// \"this\" score is better than recursive\n\t\t\treturn { matched: true, outScore };\n\t\t} else {\n\t\t\treturn { matched: false, outScore };\n\t\t}\n\t}\n\treturn { matched: false, outScore };\n}\n\n/**\n * Does a fuzzy search to find pattern inside a string.\n * @param {*} pattern string pattern to search for\n * @param {*} target string string which is being searched\n * @returns [boolean, number] a boolean which tells if pattern was\n * found or not and a search score\n */\nfunction fuzzyMatch(pattern: string, target: string): { matched: boolean; outScore: number } {\n\tconst recursionCount = 0;\n\tconst recursionLimit = 5;\n\tconst matches: number[] = [];\n\tconst maxMatches = 256;\n\n\treturn fuzzyMatchRecursive(\n\t\tpattern,\n\t\ttarget,\n\t\t0 /* patternCurIndex */,\n\t\t0 /* strCurrIndex */,\n\t\tnull /* srcMatces */,\n\t\tmatches,\n\t\tmaxMatches,\n\t\t0 /* nextMatch */,\n\t\trecursionCount,\n\t\trecursionLimit,\n\t);\n}\n\n// prop = 'key'\n// prop = 'key1.key2'\n// prop = ['key1', 'key2']\nfunction getValue<T extends object>(obj: T, prop: string): unknown {\n\tif (obj.hasOwnProperty(prop)) {\n\t\treturn obj[prop as keyof T];\n\t}\n\n\tconst segments = prop.split('.');\n\n\tlet result = obj;\n\tlet i = 0;\n\twhile (result && i < segments.length) {\n\t\tconst key = segments[i] as keyof T;\n\t\tresult = result[key] as T;\n\t\ti++;\n\t}\n\treturn result;\n}\n\nexport function sublimeSearch<T extends object>(\n\tfilter: string,\n\tdata: readonly T[],\n\tkeys: Array<{ key: string; weight: number }> = DEFAULT_KEYS,\n): Array<{ score: number; item: T }> {\n\tconst results = data.reduce((accu: Array<{ score: number; item: T }>, item: T) => {\n\t\tlet values: Array<{ value: string; weight: number }> = [];\n\t\tkeys.forEach(({ key, weight }) => {\n\t\t\tconst value = getValue(item, key);\n\t\t\tif (Array.isArray(value)) {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n\t\t\t\tvalues = values.concat(value.map((v) => ({ value: v, weight })));\n\t\t\t} else if (typeof value === 'string') {\n\t\t\t\tvalues.push({\n\t\t\t\t\tvalue,\n\t\t\t\t\tweight,\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\n\t\t// for each item, check every key and get maximum score\n\t\tconst itemMatch = values.reduce(\n\t\t\t(\n\t\t\t\tresult: null | { matched: boolean; outScore: number },\n\t\t\t\t{ value, weight }: { value: string; weight: number },\n\t\t\t) => {\n\t\t\t\tif (!fuzzyMatchSimple(filter, value)) {\n\t\t\t\t\treturn result;\n\t\t\t\t}\n\n\t\t\t\tconst match = fuzzyMatch(filter, value);\n\t\t\t\tmatch.outScore *= weight;\n\n\t\t\t\tconst { matched, outScore } = match;\n\t\t\t\tif (!result && matched) {\n\t\t\t\t\treturn match;\n\t\t\t\t}\n\t\t\t\tif (matched && result && outScore > result.outScore) {\n\t\t\t\t\treturn match;\n\t\t\t\t}\n\t\t\t\treturn result;\n\t\t\t},\n\t\t\tnull,\n\t\t);\n\n\t\tif (itemMatch) {\n\t\t\taccu.push({\n\t\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.d.ts
2
+ declare const DEFAULT_KEYS: {
3
+ key: string;
4
+ weight: number;
5
+ }[];
6
+ declare function sublimeSearch<T extends object>(filter: string, data: readonly T[], keys?: Array<{
7
+ key: string;
8
+ weight: number;
9
+ }>): Array<{
10
+ score: number;
11
+ item: T;
12
+ }>;
13
+ //#endregion
14
+ export { sublimeSearch as n, DEFAULT_KEYS as t };
15
+ //# sourceMappingURL=sublimeSearch.d.cts.map
@@ -0,0 +1,15 @@
1
+ //#region src/search/sublimeSearch.d.ts
2
+ declare const DEFAULT_KEYS: {
3
+ key: string;
4
+ weight: number;
5
+ }[];
6
+ declare function sublimeSearch<T extends object>(filter: string, data: readonly T[], keys?: Array<{
7
+ key: string;
8
+ weight: number;
9
+ }>): Array<{
10
+ score: number;
11
+ item: T;
12
+ }>;
13
+ //#endregion
14
+ export { sublimeSearch as n, DEFAULT_KEYS as t };
15
+ //# sourceMappingURL=sublimeSearch.d.mts.map
@@ -0,0 +1,171 @@
1
+ //#region src/search/sublimeSearch.ts
2
+ const SEQUENTIAL_BONUS = 60;
3
+ const SEPARATOR_BONUS = 38;
4
+ const CAMEL_BONUS = 30;
5
+ const FIRST_LETTER_BONUS = 15;
6
+ const LEADING_LETTER_PENALTY = -20;
7
+ const MAX_LEADING_LETTER_PENALTY = -200;
8
+ const UNMATCHED_LETTER_PENALTY = -5;
9
+ const DEFAULT_KEYS = [{
10
+ key: "properties.displayName",
11
+ weight: 1.3
12
+ }, {
13
+ key: "properties.codex.alias",
14
+ weight: 1
15
+ }];
16
+ /**
17
+ * Returns true if each character in pattern is found sequentially within target
18
+ * @param {*} pattern string
19
+ * @param {*} target string
20
+ */
21
+ function fuzzyMatchSimple(pattern, target) {
22
+ let patternIdx = 0;
23
+ let strIdx = 0;
24
+ while (patternIdx < pattern.length && strIdx < target.length) {
25
+ if (pattern.charAt(patternIdx).toLowerCase() === target.charAt(strIdx).toLowerCase()) patternIdx++;
26
+ ++strIdx;
27
+ }
28
+ return pattern.length !== 0 && target.length !== 0 && patternIdx === pattern.length;
29
+ }
30
+ function fuzzyMatchRecursive(pattern, target, patternCurIndex, targetCurrIndex, targetMatches, matches, maxMatches, nextMatch, recursionCount, recursionLimit) {
31
+ let outScore = 0;
32
+ if (++recursionCount >= recursionLimit) return {
33
+ matched: false,
34
+ outScore
35
+ };
36
+ if (patternCurIndex === pattern.length || targetCurrIndex === target.length) return {
37
+ matched: false,
38
+ outScore
39
+ };
40
+ let recursiveMatch = false;
41
+ let bestRecursiveMatches = [];
42
+ let bestRecursiveScore = 0;
43
+ let firstMatch = true;
44
+ while (patternCurIndex < pattern.length && targetCurrIndex < target.length) {
45
+ if (pattern[patternCurIndex].toLowerCase() === target[targetCurrIndex].toLowerCase()) {
46
+ if (nextMatch >= maxMatches) return {
47
+ matched: false,
48
+ outScore
49
+ };
50
+ if (firstMatch && targetMatches) {
51
+ matches = [...targetMatches];
52
+ firstMatch = false;
53
+ }
54
+ const recursiveMatches = [];
55
+ const recursiveResult = fuzzyMatchRecursive(pattern, target, patternCurIndex, targetCurrIndex + 1, matches, recursiveMatches, maxMatches, nextMatch, recursionCount, recursionLimit);
56
+ const recursiveScore = recursiveResult.outScore;
57
+ if (recursiveResult.matched) {
58
+ if (!recursiveMatch || recursiveScore > bestRecursiveScore) {
59
+ bestRecursiveMatches = [...recursiveMatches];
60
+ bestRecursiveScore = recursiveScore;
61
+ }
62
+ recursiveMatch = true;
63
+ }
64
+ matches[nextMatch++] = targetCurrIndex;
65
+ ++patternCurIndex;
66
+ }
67
+ ++targetCurrIndex;
68
+ }
69
+ const matched = patternCurIndex === pattern.length;
70
+ if (matched) {
71
+ outScore = 100;
72
+ if (!target.toLowerCase().startsWith("n8n")) {
73
+ let penalty = LEADING_LETTER_PENALTY * matches[0];
74
+ penalty = penalty < MAX_LEADING_LETTER_PENALTY ? MAX_LEADING_LETTER_PENALTY : penalty;
75
+ outScore += penalty;
76
+ }
77
+ const unmatched = target.length - nextMatch;
78
+ outScore += UNMATCHED_LETTER_PENALTY * unmatched;
79
+ for (let i = 0; i < nextMatch; i++) {
80
+ const currIdx = matches[i];
81
+ if (i > 0) {
82
+ if (currIdx === matches[i - 1] + 1) outScore += SEQUENTIAL_BONUS;
83
+ }
84
+ if (currIdx > 0) {
85
+ const neighbor = target[currIdx - 1];
86
+ const curr = target[currIdx];
87
+ if (neighbor !== neighbor.toUpperCase() && curr !== curr.toLowerCase()) outScore += CAMEL_BONUS;
88
+ if (neighbor === "_" || neighbor === " ") outScore += SEPARATOR_BONUS;
89
+ } else outScore += FIRST_LETTER_BONUS;
90
+ }
91
+ if (recursiveMatch && (!matched || bestRecursiveScore > outScore)) {
92
+ matches = [...bestRecursiveMatches];
93
+ outScore = bestRecursiveScore;
94
+ return {
95
+ matched: true,
96
+ outScore
97
+ };
98
+ } else if (matched) return {
99
+ matched: true,
100
+ outScore
101
+ };
102
+ else return {
103
+ matched: false,
104
+ outScore
105
+ };
106
+ }
107
+ return {
108
+ matched: false,
109
+ outScore
110
+ };
111
+ }
112
+ /**
113
+ * Does a fuzzy search to find pattern inside a string.
114
+ * @param {*} pattern string pattern to search for
115
+ * @param {*} target string string which is being searched
116
+ * @returns [boolean, number] a boolean which tells if pattern was
117
+ * found or not and a search score
118
+ */
119
+ function fuzzyMatch(pattern, target) {
120
+ return fuzzyMatchRecursive(pattern, target, 0, 0, null, [], 256, 0, 0, 5);
121
+ }
122
+ function getValue(obj, prop) {
123
+ if (obj.hasOwnProperty(prop)) return obj[prop];
124
+ const segments = prop.split(".");
125
+ let result = obj;
126
+ let i = 0;
127
+ while (result && i < segments.length) {
128
+ const key = segments[i];
129
+ result = result[key];
130
+ i++;
131
+ }
132
+ return result;
133
+ }
134
+ function sublimeSearch(filter, data, keys = DEFAULT_KEYS) {
135
+ const results = data.reduce((accu, item) => {
136
+ let values = [];
137
+ keys.forEach(({ key, weight }) => {
138
+ const value = getValue(item, key);
139
+ if (Array.isArray(value)) values = values.concat(value.map((v) => ({
140
+ value: v,
141
+ weight
142
+ })));
143
+ else if (typeof value === "string") values.push({
144
+ value,
145
+ weight
146
+ });
147
+ });
148
+ const itemMatch = values.reduce((result, { value, weight }) => {
149
+ if (!fuzzyMatchSimple(filter, value)) return result;
150
+ const match = fuzzyMatch(filter, value);
151
+ match.outScore *= weight;
152
+ const { matched, outScore } = match;
153
+ if (!result && matched) return match;
154
+ if (matched && result && outScore > result.outScore) return match;
155
+ return result;
156
+ }, null);
157
+ if (itemMatch) accu.push({
158
+ score: itemMatch.outScore,
159
+ item
160
+ });
161
+ return accu;
162
+ }, []);
163
+ results.sort((a, b) => {
164
+ return b.score - a.score;
165
+ });
166
+ return results;
167
+ }
168
+
169
+ //#endregion
170
+ export { sublimeSearch as n, DEFAULT_KEYS as t };
171
+ //# sourceMappingURL=sublimeSearch.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sublimeSearch.mjs","names":["bestRecursiveMatches: number[]","recursiveMatches: number[]","values: Array<{ value: string; weight: number }>"],"sources":["../src/search/sublimeSearch.ts"],"sourcesContent":["/*\n * Constants and utility functions used for searching for node types in node creator component\n * based on https://github.com/forrestthewoods/lib_fts/blob/master/code/fts_fuzzy_match.js\n */\n\nconst SEQUENTIAL_BONUS = 60; // bonus for adjacent matches\nconst SEPARATOR_BONUS = 38; // bonus if match occurs after a separator\nconst CAMEL_BONUS = 30; // bonus if match is uppercase and prev is lower\nconst FIRST_LETTER_BONUS = 15; // bonus if the first letter is matched\n\nconst LEADING_LETTER_PENALTY = -20; // penalty applied for every letter in str before the first match\nconst MAX_LEADING_LETTER_PENALTY = -200; // maximum penalty for leading letters\nconst UNMATCHED_LETTER_PENALTY = -5;\n\nexport const DEFAULT_KEYS = [\n\t{ key: 'properties.displayName', weight: 1.3 },\n\t{ key: 'properties.codex.alias', weight: 1 },\n];\n\n/**\n * Returns true if each character in pattern is found sequentially within target\n * @param {*} pattern string\n * @param {*} target string\n */\nfunction fuzzyMatchSimple(pattern: string, target: string): boolean {\n\tlet patternIdx = 0;\n\tlet strIdx = 0;\n\n\twhile (patternIdx < pattern.length && strIdx < target.length) {\n\t\tconst patternChar = pattern.charAt(patternIdx).toLowerCase();\n\t\tconst targetChar = target.charAt(strIdx).toLowerCase();\n\t\tif (patternChar === targetChar) {\n\t\t\tpatternIdx++;\n\t\t}\n\t\t++strIdx;\n\t}\n\n\treturn pattern.length !== 0 && target.length !== 0 && patternIdx === pattern.length;\n}\n\nfunction fuzzyMatchRecursive(\n\tpattern: string,\n\ttarget: string,\n\tpatternCurIndex: number,\n\ttargetCurrIndex: number,\n\ttargetMatches: null | number[],\n\tmatches: number[],\n\tmaxMatches: number,\n\tnextMatch: number,\n\trecursionCount: number,\n\trecursionLimit: number,\n): { matched: boolean; outScore: number } {\n\tlet outScore = 0;\n\n\t// Return if recursion limit is reached.\n\tif (++recursionCount >= recursionLimit) {\n\t\treturn { matched: false, outScore };\n\t}\n\n\t// Return if we reached ends of strings.\n\tif (patternCurIndex === pattern.length || targetCurrIndex === target.length) {\n\t\treturn { matched: false, outScore };\n\t}\n\n\t// Recursion params\n\tlet recursiveMatch = false;\n\tlet bestRecursiveMatches: number[] = [];\n\tlet bestRecursiveScore = 0;\n\n\t// Loop through pattern and str looking for a match.\n\tlet firstMatch = true;\n\twhile (patternCurIndex < pattern.length && targetCurrIndex < target.length) {\n\t\t// Match found.\n\t\tif (pattern[patternCurIndex].toLowerCase() === target[targetCurrIndex].toLowerCase()) {\n\t\t\tif (nextMatch >= maxMatches) {\n\t\t\t\treturn { matched: false, outScore };\n\t\t\t}\n\n\t\t\tif (firstMatch && targetMatches) {\n\t\t\t\tmatches = [...targetMatches];\n\t\t\t\tfirstMatch = false;\n\t\t\t}\n\n\t\t\tconst recursiveMatches: number[] = [];\n\t\t\tconst recursiveResult = fuzzyMatchRecursive(\n\t\t\t\tpattern,\n\t\t\t\ttarget,\n\t\t\t\tpatternCurIndex,\n\t\t\t\ttargetCurrIndex + 1,\n\t\t\t\tmatches,\n\t\t\t\trecursiveMatches,\n\t\t\t\tmaxMatches,\n\t\t\t\tnextMatch,\n\t\t\t\trecursionCount,\n\t\t\t\trecursionLimit,\n\t\t\t);\n\n\t\t\tconst recursiveScore = recursiveResult.outScore;\n\t\t\tif (recursiveResult.matched) {\n\t\t\t\t// Pick best recursive score.\n\t\t\t\tif (!recursiveMatch || recursiveScore > bestRecursiveScore) {\n\t\t\t\t\tbestRecursiveMatches = [...recursiveMatches];\n\t\t\t\t\tbestRecursiveScore = recursiveScore;\n\t\t\t\t}\n\t\t\t\trecursiveMatch = true;\n\t\t\t}\n\n\t\t\tmatches[nextMatch++] = targetCurrIndex;\n\t\t\t++patternCurIndex;\n\t\t}\n\t\t++targetCurrIndex;\n\t}\n\n\tconst matched = patternCurIndex === pattern.length;\n\n\tif (matched) {\n\t\toutScore = 100;\n\n\t\t// Apply leading letter penalty (if not n8n-prefixed)\n\t\tif (!target.toLowerCase().startsWith('n8n')) {\n\t\t\tlet penalty = LEADING_LETTER_PENALTY * matches[0];\n\t\t\tpenalty = penalty < MAX_LEADING_LETTER_PENALTY ? MAX_LEADING_LETTER_PENALTY : penalty;\n\t\t\toutScore += penalty;\n\t\t}\n\n\t\t//Apply unmatched penalty\n\t\tconst unmatched = target.length - nextMatch;\n\t\toutScore += UNMATCHED_LETTER_PENALTY * unmatched;\n\n\t\t// Apply ordering bonuses\n\t\tfor (let i = 0; i < nextMatch; i++) {\n\t\t\tconst currIdx = matches[i];\n\n\t\t\tif (i > 0) {\n\t\t\t\tconst prevIdx = matches[i - 1];\n\t\t\t\tif (currIdx === prevIdx + 1) {\n\t\t\t\t\toutScore += SEQUENTIAL_BONUS;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Check for bonuses based on neighbor character value.\n\t\t\tif (currIdx > 0) {\n\t\t\t\t// Camel case\n\t\t\t\tconst neighbor = target[currIdx - 1];\n\t\t\t\tconst curr = target[currIdx];\n\t\t\t\tif (neighbor !== neighbor.toUpperCase() && curr !== curr.toLowerCase()) {\n\t\t\t\t\toutScore += CAMEL_BONUS;\n\t\t\t\t}\n\t\t\t\tconst isNeighbourSeparator = neighbor === '_' || neighbor === ' ';\n\t\t\t\tif (isNeighbourSeparator) {\n\t\t\t\t\toutScore += SEPARATOR_BONUS;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// First letter\n\t\t\t\toutScore += FIRST_LETTER_BONUS;\n\t\t\t}\n\t\t}\n\n\t\t// Return best result\n\t\tif (recursiveMatch && (!matched || bestRecursiveScore > outScore)) {\n\t\t\t// Recursive score is better than \"this\"\n\t\t\tmatches = [...bestRecursiveMatches];\n\t\t\toutScore = bestRecursiveScore;\n\t\t\treturn { matched: true, outScore };\n\t\t} else if (matched) {\n\t\t\t// \"this\" score is better than recursive\n\t\t\treturn { matched: true, outScore };\n\t\t} else {\n\t\t\treturn { matched: false, outScore };\n\t\t}\n\t}\n\treturn { matched: false, outScore };\n}\n\n/**\n * Does a fuzzy search to find pattern inside a string.\n * @param {*} pattern string pattern to search for\n * @param {*} target string string which is being searched\n * @returns [boolean, number] a boolean which tells if pattern was\n * found or not and a search score\n */\nfunction fuzzyMatch(pattern: string, target: string): { matched: boolean; outScore: number } {\n\tconst recursionCount = 0;\n\tconst recursionLimit = 5;\n\tconst matches: number[] = [];\n\tconst maxMatches = 256;\n\n\treturn fuzzyMatchRecursive(\n\t\tpattern,\n\t\ttarget,\n\t\t0 /* patternCurIndex */,\n\t\t0 /* strCurrIndex */,\n\t\tnull /* srcMatces */,\n\t\tmatches,\n\t\tmaxMatches,\n\t\t0 /* nextMatch */,\n\t\trecursionCount,\n\t\trecursionLimit,\n\t);\n}\n\n// prop = 'key'\n// prop = 'key1.key2'\n// prop = ['key1', 'key2']\nfunction getValue<T extends object>(obj: T, prop: string): unknown {\n\tif (obj.hasOwnProperty(prop)) {\n\t\treturn obj[prop as keyof T];\n\t}\n\n\tconst segments = prop.split('.');\n\n\tlet result = obj;\n\tlet i = 0;\n\twhile (result && i < segments.length) {\n\t\tconst key = segments[i] as keyof T;\n\t\tresult = result[key] as T;\n\t\ti++;\n\t}\n\treturn result;\n}\n\nexport function sublimeSearch<T extends object>(\n\tfilter: string,\n\tdata: readonly T[],\n\tkeys: Array<{ key: string; weight: number }> = DEFAULT_KEYS,\n): Array<{ score: number; item: T }> {\n\tconst results = data.reduce((accu: Array<{ score: number; item: T }>, item: T) => {\n\t\tlet values: Array<{ value: string; weight: number }> = [];\n\t\tkeys.forEach(({ key, weight }) => {\n\t\t\tconst value = getValue(item, key);\n\t\t\tif (Array.isArray(value)) {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n\t\t\t\tvalues = values.concat(value.map((v) => ({ value: v, weight })));\n\t\t\t} else if (typeof value === 'string') {\n\t\t\t\tvalues.push({\n\t\t\t\t\tvalue,\n\t\t\t\t\tweight,\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\n\t\t// for each item, check every key and get maximum score\n\t\tconst itemMatch = values.reduce(\n\t\t\t(\n\t\t\t\tresult: null | { matched: boolean; outScore: number },\n\t\t\t\t{ value, weight }: { value: string; weight: number },\n\t\t\t) => {\n\t\t\t\tif (!fuzzyMatchSimple(filter, value)) {\n\t\t\t\t\treturn result;\n\t\t\t\t}\n\n\t\t\t\tconst match = fuzzyMatch(filter, value);\n\t\t\t\tmatch.outScore *= weight;\n\n\t\t\t\tconst { matched, outScore } = match;\n\t\t\t\tif (!result && matched) {\n\t\t\t\t\treturn match;\n\t\t\t\t}\n\t\t\t\tif (matched && result && outScore > result.outScore) {\n\t\t\t\t\treturn match;\n\t\t\t\t}\n\t\t\t\treturn result;\n\t\t\t},\n\t\t\tnull,\n\t\t);\n\n\t\tif (itemMatch) {\n\t\t\taccu.push({\n\t\t\t\tscore: itemMatch.outScore,\n\t\t\t\titem,\n\t\t\t});\n\t\t}\n\n\t\treturn accu;\n\t}, []);\n\n\tresults.sort((a, b) => {\n\t\treturn b.score - a.score;\n\t});\n\n\treturn results;\n}\n"],"mappings":";AAKA,MAAM,mBAAmB;AACzB,MAAM,kBAAkB;AACx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AM,WAAW,KAAK,MAAM,IAAI;CAEhC,IAAI,SAAS;CACb,IAAI,IAAI;AACR,QAAO,UAAU,IAAI,SAAS,QAAQ;EACrC,MAAM,MAAM,SAAS;AACrB,WAAS,OAAO;AAChB;;AAED,QAAO;;AAGR,SAAgB,cACf,QACA,MACA,OAA+C,cACX;CACpC,MAAM,UAAU,KAAK,QAAQ,MAAyC,SAAY;EACjF,IAAIC,SAAmD,EAAE;AACzD,OAAK,SAAS,EAAE,KAAK,aAAa;GACjC,MAAM,QAAQ,SAAS,MAAM,IAAI;AACjC,OAAI,MAAM,QAAQ,MAAM,CAEvB,UAAS,OAAO,OAAO,MAAM,KAAK,OAAO;IAAE,OAAO;IAAG;IAAQ,EAAE,CAAC;YACtD,OAAO,UAAU,SAC3B,QAAO,KAAK;IACX;IACA;IACA,CAAC;IAEF;EAGF,MAAM,YAAY,OAAO,QAEvB,QACA,EAAE,OAAO,aACL;AACJ,OAAI,CAAC,iBAAiB,QAAQ,MAAM,CACnC,QAAO;GAGR,MAAM,QAAQ,WAAW,QAAQ,MAAM;AACvC,SAAM,YAAY;GAElB,MAAM,EAAE,SAAS,aAAa;AAC9B,OAAI,CAAC,UAAU,QACd,QAAO;AAER,OAAI,WAAW,UAAU,WAAW,OAAO,SAC1C,QAAO;AAER,UAAO;KAER,KACA;AAED,MAAI,UACH,MAAK,KAAK;GACT,OAAO,UAAU;GACjB;GACA,CAAC;AAGH,SAAO;IACL,EAAE,CAAC;AAEN,SAAQ,MAAM,GAAG,MAAM;AACtB,SAAO,EAAE,QAAQ,EAAE;GAClB;AAEF,QAAO"}
@@ -0,0 +1,42 @@
1
+
2
+ //#region src/string/truncate.ts
3
+ const truncate = (text, length = 30) => text.length > length ? text.slice(0, length) + "..." : text;
4
+ /**
5
+ * Replace part of given text with ellipsis following the rules below:
6
+ *
7
+ * - Remove chars just before the last word, as long as the last word is under 15 chars
8
+ * - Otherwise preserve the last 5 chars of the name and remove chars before that
9
+ */
10
+ function truncateBeforeLast(text, maxLength, lastCharsLength = 5) {
11
+ const chars = [];
12
+ const segmenter = new Intl.Segmenter(void 0, { granularity: "grapheme" });
13
+ for (const { segment } of segmenter.segment(text)) chars.push(segment);
14
+ if (chars.length <= maxLength) return text;
15
+ const lastWordIndex = chars.findLastIndex((ch) => ch.match(/^\s+$/)) + 1;
16
+ const lastWord = chars.slice(lastWordIndex);
17
+ const ellipsis = "…";
18
+ const ellipsisLength = 1;
19
+ if (lastWord.length < 15) {
20
+ const charsToRemove = chars.length - maxLength + ellipsisLength;
21
+ const indexBeforeLastWord = lastWordIndex;
22
+ const keepLength = indexBeforeLastWord - charsToRemove;
23
+ if (keepLength > 0) return chars.slice(0, keepLength).join("") + ellipsis + chars.slice(indexBeforeLastWord).join("");
24
+ }
25
+ if (lastCharsLength < 1) return chars.slice(0, maxLength).join("") + ellipsis;
26
+ return chars.slice(0, maxLength - lastCharsLength - ellipsisLength).join("") + ellipsis + chars.slice(-lastCharsLength).join("");
27
+ }
28
+
29
+ //#endregion
30
+ Object.defineProperty(exports, 'truncate', {
31
+ enumerable: true,
32
+ get: function () {
33
+ return truncate;
34
+ }
35
+ });
36
+ Object.defineProperty(exports, 'truncateBeforeLast', {
37
+ enumerable: true,
38
+ get: function () {
39
+ return truncateBeforeLast;
40
+ }
41
+ });
42
+ //# sourceMappingURL=truncate.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"truncate.cjs","names":["chars: string[]"],"sources":["../src/string/truncate.ts"],"sourcesContent":["export const truncate = (text: string, length = 30): string =>\n\ttext.length > length ? text.slice(0, length) + '...' : text;\n\n/**\n * Replace part of given text with ellipsis following the rules below:\n *\n * - Remove chars just before the last word, as long as the last word is under 15 chars\n * - Otherwise preserve the last 5 chars of the name and remove chars before that\n */\nexport function truncateBeforeLast(\n\ttext: string,\n\tmaxLength: number,\n\tlastCharsLength: number = 5,\n): string {\n\tconst chars: string[] = [];\n\n\tconst segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });\n\n\tfor (const { segment } of segmenter.segment(text)) {\n\t\tchars.push(segment);\n\t}\n\n\tif (chars.length <= maxLength) {\n\t\treturn text;\n\t}\n\n\tconst lastWhitespaceIndex = chars.findLastIndex((ch) => ch.match(/^\\s+$/));\n\tconst lastWordIndex = lastWhitespaceIndex + 1;\n\tconst lastWord = chars.slice(lastWordIndex);\n\tconst ellipsis = '…';\n\tconst ellipsisLength = ellipsis.length;\n\n\tif (lastWord.length < 15) {\n\t\tconst charsToRemove = chars.length - maxLength + ellipsisLength;\n\t\tconst indexBeforeLastWord = lastWordIndex;\n\t\tconst keepLength = indexBeforeLastWord - charsToRemove;\n\n\t\tif (keepLength > 0) {\n\t\t\treturn (\n\t\t\t\tchars.slice(0, keepLength).join('') + ellipsis + chars.slice(indexBeforeLastWord).join('')\n\t\t\t);\n\t\t}\n\t}\n\n\tif (lastCharsLength < 1) {\n\t\treturn chars.slice(0, maxLength).join('') + ellipsis;\n\t}\n\n\treturn (\n\t\tchars.slice(0, maxLength - lastCharsLength - ellipsisLength).join('') +\n\t\tellipsis +\n\t\tchars.slice(-lastCharsLength).join('')\n\t);\n}\n"],"mappings":";;AAAA,MAAa,YAAY,MAAc,SAAS,OAC/C,KAAK,SAAS,SAAS,KAAK,MAAM,GAAG,OAAO,GAAG,QAAQ;;;;;;;AAQxD,SAAgB,mBACf,MACA,WACA,kBAA0B,GACjB;CACT,MAAMA,QAAkB,EAAE;CAE1B,MAAM,YAAY,IAAI,KAAK,UAAU,QAAW,EAAE,aAAa,YAAY,CAAC;AAE5E,MAAK,MAAM,EAAE,aAAa,UAAU,QAAQ,KAAK,CAChD,OAAM,KAAK,QAAQ;AAGpB,KAAI,MAAM,UAAU,UACnB,QAAO;CAIR,MAAM,gBADsB,MAAM,eAAe,OAAO,GAAG,MAAM,QAAQ,CAAC,GAC9B;CAC5C,MAAM,WAAW,MAAM,MAAM,cAAc;CAC3C,MAAM,WAAW;CACjB,MAAM,iBAAiB;AAEvB,KAAI,SAAS,SAAS,IAAI;EACzB,MAAM,gBAAgB,MAAM,SAAS,YAAY;EACjD,MAAM,sBAAsB;EAC5B,MAAM,aAAa,sBAAsB;AAEzC,MAAI,aAAa,EAChB,QACC,MAAM,MAAM,GAAG,WAAW,CAAC,KAAK,GAAG,GAAG,WAAW,MAAM,MAAM,oBAAoB,CAAC,KAAK,GAAG;;AAK7F,KAAI,kBAAkB,EACrB,QAAO,MAAM,MAAM,GAAG,UAAU,CAAC,KAAK,GAAG,GAAG;AAG7C,QACC,MAAM,MAAM,GAAG,YAAY,kBAAkB,eAAe,CAAC,KAAK,GAAG,GACrE,WACA,MAAM,MAAM,CAAC,gBAAgB,CAAC,KAAK,GAAG"}
@@ -0,0 +1,6 @@
1
+ //#region src/string/truncate.d.ts
2
+ declare const truncate: (text: string, length?: number) => string;
3
+ declare function truncateBeforeLast(text: string, maxLength: number, lastCharsLength?: number): string;
4
+ //#endregion
5
+ export { truncateBeforeLast as n, truncate as t };
6
+ //# sourceMappingURL=truncate.d.cts.map
@@ -0,0 +1,6 @@
1
+ //#region src/string/truncate.d.ts
2
+ declare const truncate: (text: string, length?: number) => string;
3
+ declare function truncateBeforeLast(text: string, maxLength: number, lastCharsLength?: number): string;
4
+ //#endregion
5
+ export { truncateBeforeLast as n, truncate as t };
6
+ //# sourceMappingURL=truncate.d.mts.map
@@ -0,0 +1,30 @@
1
+ //#region src/string/truncate.ts
2
+ const truncate = (text, length = 30) => text.length > length ? text.slice(0, length) + "..." : text;
3
+ /**
4
+ * Replace part of given text with ellipsis following the rules below:
5
+ *
6
+ * - Remove chars just before the last word, as long as the last word is under 15 chars
7
+ * - Otherwise preserve the last 5 chars of the name and remove chars before that
8
+ */
9
+ function truncateBeforeLast(text, maxLength, lastCharsLength = 5) {
10
+ const chars = [];
11
+ const segmenter = new Intl.Segmenter(void 0, { granularity: "grapheme" });
12
+ for (const { segment } of segmenter.segment(text)) chars.push(segment);
13
+ if (chars.length <= maxLength) return text;
14
+ const lastWordIndex = chars.findLastIndex((ch) => ch.match(/^\s+$/)) + 1;
15
+ const lastWord = chars.slice(lastWordIndex);
16
+ const ellipsis = "…";
17
+ const ellipsisLength = 1;
18
+ if (lastWord.length < 15) {
19
+ const charsToRemove = chars.length - maxLength + ellipsisLength;
20
+ const indexBeforeLastWord = lastWordIndex;
21
+ const keepLength = indexBeforeLastWord - charsToRemove;
22
+ if (keepLength > 0) return chars.slice(0, keepLength).join("") + ellipsis + chars.slice(indexBeforeLastWord).join("");
23
+ }
24
+ if (lastCharsLength < 1) return chars.slice(0, maxLength).join("") + ellipsis;
25
+ return chars.slice(0, maxLength - lastCharsLength - ellipsisLength).join("") + ellipsis + chars.slice(-lastCharsLength).join("");
26
+ }
27
+
28
+ //#endregion
29
+ export { truncateBeforeLast as n, truncate as t };
30
+ //# sourceMappingURL=truncate.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"truncate.mjs","names":["chars: string[]"],"sources":["../src/string/truncate.ts"],"sourcesContent":["export const truncate = (text: string, length = 30): string =>\n\ttext.length > length ? text.slice(0, length) + '...' : text;\n\n/**\n * Replace part of given text with ellipsis following the rules below:\n *\n * - Remove chars just before the last word, as long as the last word is under 15 chars\n * - Otherwise preserve the last 5 chars of the name and remove chars before that\n */\nexport function truncateBeforeLast(\n\ttext: string,\n\tmaxLength: number,\n\tlastCharsLength: number = 5,\n): string {\n\tconst chars: string[] = [];\n\n\tconst segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });\n\n\tfor (const { segment } of segmenter.segment(text)) {\n\t\tchars.push(segment);\n\t}\n\n\tif (chars.length <= maxLength) {\n\t\treturn text;\n\t}\n\n\tconst lastWhitespaceIndex = chars.findLastIndex((ch) => ch.match(/^\\s+$/));\n\tconst lastWordIndex = lastWhitespaceIndex + 1;\n\tconst lastWord = chars.slice(lastWordIndex);\n\tconst ellipsis = '…';\n\tconst ellipsisLength = ellipsis.length;\n\n\tif (lastWord.length < 15) {\n\t\tconst charsToRemove = chars.length - maxLength + ellipsisLength;\n\t\tconst indexBeforeLastWord = lastWordIndex;\n\t\tconst keepLength = indexBeforeLastWord - charsToRemove;\n\n\t\tif (keepLength > 0) {\n\t\t\treturn (\n\t\t\t\tchars.slice(0, keepLength).join('') + ellipsis + chars.slice(indexBeforeLastWord).join('')\n\t\t\t);\n\t\t}\n\t}\n\n\tif (lastCharsLength < 1) {\n\t\treturn chars.slice(0, maxLength).join('') + ellipsis;\n\t}\n\n\treturn (\n\t\tchars.slice(0, maxLength - lastCharsLength - ellipsisLength).join('') +\n\t\tellipsis +\n\t\tchars.slice(-lastCharsLength).join('')\n\t);\n}\n"],"mappings":";AAAA,MAAa,YAAY,MAAc,SAAS,OAC/C,KAAK,SAAS,SAAS,KAAK,MAAM,GAAG,OAAO,GAAG,QAAQ;;;;;;;AAQxD,SAAgB,mBACf,MACA,WACA,kBAA0B,GACjB;CACT,MAAMA,QAAkB,EAAE;CAE1B,MAAM,YAAY,IAAI,KAAK,UAAU,QAAW,EAAE,aAAa,YAAY,CAAC;AAE5E,MAAK,MAAM,EAAE,aAAa,UAAU,QAAQ,KAAK,CAChD,OAAM,KAAK,QAAQ;AAGpB,KAAI,MAAM,UAAU,UACnB,QAAO;CAIR,MAAM,gBADsB,MAAM,eAAe,OAAO,GAAG,MAAM,QAAQ,CAAC,GAC9B;CAC5C,MAAM,WAAW,MAAM,MAAM,cAAc;CAC3C,MAAM,WAAW;CACjB,MAAM,iBAAiB;AAEvB,KAAI,SAAS,SAAS,IAAI;EACzB,MAAM,gBAAgB,MAAM,SAAS,YAAY;EACjD,MAAM,sBAAsB;EAC5B,MAAM,aAAa,sBAAsB;AAEzC,MAAI,aAAa,EAChB,QACC,MAAM,MAAM,GAAG,WAAW,CAAC,KAAK,GAAG,GAAG,WAAW,MAAM,MAAM,oBAAoB,CAAC,KAAK,GAAG;;AAK7F,KAAI,kBAAkB,EACrB,QAAO,MAAM,MAAM,GAAG,UAAU,CAAC,KAAK,GAAG,GAAG;AAG7C,QACC,MAAM,MAAM,GAAG,YAAY,kBAAkB,eAAe,CAAC,KAAK,GAAG,GACrE,WACA,MAAM,MAAM,CAAC,gBAAgB,CAAC,KAAK,GAAG"}
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@atom8n/utils",
3
+ "type": "module",
4
+ "version": "1.21.0",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "main": "./dist/index.cjs",
9
+ "module": "./dist/index.mjs",
10
+ "types": "./dist/index.d.mts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.mts",
14
+ "import": "./dist/index.mjs",
15
+ "require": "./dist/index.cjs"
16
+ },
17
+ "./dist/*": {
18
+ "types": "./dist/*.d.mts",
19
+ "import": "./dist/*.mjs",
20
+ "require": "./dist/*.cjs"
21
+ },
22
+ "./*": {
23
+ "types": "./dist/*.d.mts",
24
+ "import": "./dist/*.mjs",
25
+ "require": "./dist/*.cjs"
26
+ }
27
+ },
28
+ "scripts": {
29
+ "dev": "tsdown --watch",
30
+ "build": "tsdown",
31
+ "preview": "vite preview",
32
+ "typecheck": "tsc --noEmit",
33
+ "test": "vitest run",
34
+ "test:unit": "vitest run",
35
+ "test:dev": "vitest --silent=false",
36
+ "lint": "eslint src --quiet",
37
+ "lint:fix": "eslint src --fix",
38
+ "format": "biome format --write . && prettier --write . --ignore-path ../../../.prettierignore",
39
+ "format:check": "biome ci . && prettier --check . --ignore-path ../../../.prettierignore"
40
+ },
41
+ "devDependencies": {
42
+ "@n8n/eslint-config": "*",
43
+ "@atom8n/typescript-config": "1.3.0",
44
+ "@atom8n/vitest-config": "1.5.0",
45
+ "@testing-library/jest-dom": "catalog:frontend",
46
+ "@testing-library/user-event": "catalog:frontend",
47
+ "tsdown": "catalog:",
48
+ "typescript": "catalog:",
49
+ "vite": "catalog:",
50
+ "vitest": "catalog:"
51
+ },
52
+ "license": "See LICENSE.md file in the root of the repository"
53
+ }