@gesslar/toolkit 0.1.5 → 0.1.6
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 +1 -1
- package/src/lib/Util.js +62 -0
- package/src/types/Util.d.ts +40 -0
package/package.json
CHANGED
package/src/lib/Util.js
CHANGED
|
@@ -230,4 +230,66 @@ export default class Util {
|
|
|
230
230
|
)
|
|
231
231
|
}
|
|
232
232
|
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Determine the Levenshtein distance between two string values
|
|
236
|
+
*
|
|
237
|
+
* @param {string} a The first value for comparison.
|
|
238
|
+
* @param {string} b The second value for comparison.
|
|
239
|
+
* @returns {number} The Levenshtein distance
|
|
240
|
+
*/
|
|
241
|
+
static levenshteinDistance(a, b) {
|
|
242
|
+
const matrix = Array.from({length: a.length + 1}, (_, i) =>
|
|
243
|
+
Array.from({length: b.length + 1}, (_, j) =>
|
|
244
|
+
(i === 0 ? j : j === 0 ? i : 0)
|
|
245
|
+
)
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
for(let i = 1; i <= a.length; i++) {
|
|
249
|
+
for(let j = 1; j <= b.length; j++) {
|
|
250
|
+
matrix[i][j] =
|
|
251
|
+
a[i - 1] === b[j - 1]
|
|
252
|
+
? matrix[i - 1][j - 1]
|
|
253
|
+
: 1 + Math.min(
|
|
254
|
+
matrix[i - 1][j], matrix[i][j - 1],
|
|
255
|
+
matrix[i - 1][j - 1]
|
|
256
|
+
)
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return matrix[a.length][b.length]
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Determine the closest match between a string and allowed values
|
|
265
|
+
* from the Levenshtein distance.
|
|
266
|
+
*
|
|
267
|
+
* @param {string} input The input string to resolve
|
|
268
|
+
* @param {Array<string>} allowedValues The values which are permitted
|
|
269
|
+
* @param {number} [threshold] Max edit distance for a "close match"
|
|
270
|
+
* @returns {string} Suggested, probable match.
|
|
271
|
+
*/
|
|
272
|
+
static findClosestMatch(input, allowedValues, threshold=2) {
|
|
273
|
+
let closestMatch = null
|
|
274
|
+
let closestDistance = Infinity
|
|
275
|
+
let closestLengthDiff = Infinity
|
|
276
|
+
|
|
277
|
+
for(const value of allowedValues) {
|
|
278
|
+
const distance = Util.levenshteinDistance(input, value)
|
|
279
|
+
const lengthDiff = Math.abs(input.length - value.length)
|
|
280
|
+
|
|
281
|
+
if(distance < closestDistance && distance <= threshold) {
|
|
282
|
+
closestMatch = value
|
|
283
|
+
closestDistance = distance
|
|
284
|
+
closestLengthDiff = lengthDiff
|
|
285
|
+
} else if(distance === closestDistance &&
|
|
286
|
+
distance <= threshold &&
|
|
287
|
+
lengthDiff < closestLengthDiff) {
|
|
288
|
+
closestMatch = value
|
|
289
|
+
closestLengthDiff = lengthDiff
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return closestMatch
|
|
294
|
+
}
|
|
233
295
|
}
|
package/src/types/Util.d.ts
CHANGED
|
@@ -174,6 +174,46 @@ declare class Util {
|
|
|
174
174
|
* @returns Resolves when all listeners have completed
|
|
175
175
|
*/
|
|
176
176
|
static asyncEmitAnon(emitter: { listeners(event: string): Function[], on(event: string, listener: Function): any, emit(event: string, ...args: unknown[]): any }, event: string, ...args: unknown[]): Promise<void>
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Determine the Levenshtein distance between two string values.
|
|
180
|
+
* The Levenshtein distance is the minimum number of single-character edits
|
|
181
|
+
* (insertions, deletions, or substitutions) required to change one string into another.
|
|
182
|
+
*
|
|
183
|
+
* @param a - The first string for comparison
|
|
184
|
+
* @param b - The second string for comparison
|
|
185
|
+
* @returns The Levenshtein distance (number of edits needed)
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```typescript
|
|
189
|
+
* Util.levenshteinDistance("kitten", "sitting") // 3
|
|
190
|
+
* Util.levenshteinDistance("book", "back") // 2
|
|
191
|
+
* Util.levenshteinDistance("hello", "hello") // 0
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
static levenshteinDistance(a: string, b: string): number
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Find the closest match between an input string and an array of allowed values
|
|
198
|
+
* using Levenshtein distance. Returns the closest match if it's within a threshold
|
|
199
|
+
* of 2 edits, otherwise returns null.
|
|
200
|
+
*
|
|
201
|
+
* Useful for fuzzy string matching, such as suggesting corrections for typos
|
|
202
|
+
* in command-line arguments or configuration values.
|
|
203
|
+
*
|
|
204
|
+
* @param input - The input string to find a match for
|
|
205
|
+
* @param allowedValues - Array of allowed string values to match against
|
|
206
|
+
* @param threshold - Maximum edit distance for a match (default: 2)
|
|
207
|
+
* @returns The closest matching string, or null if no match within threshold
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* ```typescript
|
|
211
|
+
* const commands = ["help", "build", "test", "deploy"]
|
|
212
|
+
* Util.findClosestMatch("bulid", commands) // "build"
|
|
213
|
+
* Util.findClosestMatch("xyz", commands) // null
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
216
|
+
static findClosestMatch(input: string, allowedValues: string[], threshold?: number): string | null
|
|
177
217
|
}
|
|
178
218
|
|
|
179
219
|
export default Util
|