@muze-labs/simplyflow 0.9.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.
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Return the closest valid option name to a possibly misspelled name.
3
+ *
4
+ * Used for developer-friendly typo suggestions. Short unknown names such as
5
+ * `api` or `db` are ignored by default because they are common app extension
6
+ * properties and edit-distance suggestions are noisy for short strings.
7
+ *
8
+ * @param {string} name The unknown name to compare.
9
+ * @param {Iterable<string>} options Valid option names to compare against.
10
+ * @param {object} [settings] Optional matching settings.
11
+ * @param {number} [settings.maxDistance=2] Maximum edit distance to accept.
12
+ * @param {number} [settings.minLength=4] Minimum unknown-name length to check.
13
+ * @returns {string|undefined} The closest option, or undefined when no useful suggestion exists.
14
+ */
15
+ export function closest(name, options, { maxDistance = 2, minLength = 4 } = {})
16
+ {
17
+ if (name.length < minLength) {
18
+ return
19
+ }
20
+
21
+ let result
22
+ let resultDistance = Infinity
23
+ for (const option of options) {
24
+ const distance = editDistance(name, option, maxDistance)
25
+ if (distance < resultDistance) {
26
+ result = option
27
+ resultDistance = distance
28
+ }
29
+ }
30
+ return resultDistance <= maxDistance ? result : undefined
31
+ }
32
+
33
+ /**
34
+ * Return the edit distance between two short strings.
35
+ *
36
+ * This is used only for friendly typo suggestions in developer warnings, so it
37
+ * intentionally caps large length differences early instead of doing extra work.
38
+ *
39
+ * @param {string} a First string to compare.
40
+ * @param {string} b Second string to compare.
41
+ * @param {number} [maxDistance=2] Maximum useful distance for the caller.
42
+ * @returns {number} The Levenshtein edit distance, or maxDistance + 1 when the strings are too far apart.
43
+ */
44
+ export function editDistance(a, b, maxDistance = 2)
45
+ {
46
+ const tooFar = maxDistance + 1
47
+ if (Math.abs(a.length - b.length) > maxDistance) {
48
+ return tooFar
49
+ }
50
+
51
+ const previous = Array.from({ length: b.length + 1 }, (_, index) => index)
52
+ const current = new Array(b.length + 1)
53
+
54
+ for (let ai = 1; ai <= a.length; ai++) {
55
+ current[0] = ai
56
+ for (let bi = 1; bi <= b.length; bi++) {
57
+ const cost = a[ai - 1] === b[bi - 1] ? 0 : 1
58
+ current[bi] = Math.min(
59
+ previous[bi] + 1,
60
+ current[bi - 1] + 1,
61
+ previous[bi - 1] + cost
62
+ )
63
+ }
64
+ previous.splice(0, previous.length, ...current)
65
+ }
66
+
67
+ return previous[b.length]
68
+ }
@@ -0,0 +1,9 @@
1
+ export const DEP = {
2
+ ITERATE: Symbol.for('@simplyedit/simplyflow.iterate'),
3
+ XRAY: Symbol.for('@simplyedit/simplyflow.xRay'),
4
+ SIGNAL: Symbol.for('@simplyedit/simplyflow.Signal'),
5
+ TEMPLATE: Symbol.for('@simplyedit/simplyflow.bindTemplate'),
6
+ VALUE: Symbol.for('@simplyedit/simplyflow.bindValue'),
7
+ LENGTH: 'length',
8
+ SIZE: 'size'
9
+ }