Debian Codegraph @

node/argparse/lib/difflib.js

raw
1 // Minimal port of Python's difflib used by argparse suggestions.
2
3 'use strict'
4
5 function calculate_ratio (matches, length) {
6 return length ? 2 * matches / length : 1
7 }
8
9 class SequenceMatcher {
10 constructor () {
11 this.a = []
12 this.b = []
13 this.b2j = new Map()
14 this.fullbcount = undefined
15 this.matching_blocks = undefined
16 }
17
18 set_seq1 (a) {
19 this.a = Array.from(a)
20 this.matching_blocks = undefined
21 }
22
23 set_seq2 (b) {
24 this.b = Array.from(b)
25 this.matching_blocks = undefined
26 this.fullbcount = undefined
27 this._chain_b()
28 }
29
30 _chain_b () {
31 const b2j = new Map()
32 for (const [i, element] of this.b.entries()) {
33 const indices = b2j.get(element) || []
34 indices.push(i)
35 b2j.set(element, indices)
36 }
37
38 const n = this.b.length
39 if (n >= 200) {
40 const ntest = Math.floor(n / 100) + 1
41 for (const [element, indices] of b2j) {
42 if (indices.length > ntest) b2j.delete(element)
43 }
44 }
45 this.b2j = b2j
46 }
47
48 find_longest_match (alo, ahi, blo, bhi) {
49 const { a, b, b2j } = this
50 let besti = alo
51 let bestj = blo
52 let bestsize = 0
53 let j2len = new Map()
54
55 for (let i = alo; i < ahi; i++) {
56 const newj2len = new Map()
57 for (const j of b2j.get(a[i]) || []) {
58 if (j < blo) continue
59 if (j >= bhi) break
60 const k = (j2len.get(j - 1) || 0) + 1
61 newj2len.set(j, k)
62 if (k > bestsize) {
63 besti = i - k + 1
64 bestj = j - k + 1
65 bestsize = k
66 }
67 }
68 j2len = newj2len
69 }
70
71 while (besti > alo && bestj > blo && a[besti - 1] === b[bestj - 1]) {
72 besti--
73 bestj--
74 bestsize++
75 }
76 while (besti + bestsize < ahi && bestj + bestsize < bhi &&
77 a[besti + bestsize] === b[bestj + bestsize]) {
78 bestsize++
79 }
80
81 return [besti, bestj, bestsize]
82 }
83
84 get_matching_blocks () {
85 if (this.matching_blocks !== undefined) return this.matching_blocks
86
87 const la = this.a.length
88 const lb = this.b.length
89 const queue = [[0, la, 0, lb]]
90 const matching_blocks = []
91 while (queue.length) {
92 const [alo, ahi, blo, bhi] = queue.pop()
93 const match = this.find_longest_match(alo, ahi, blo, bhi)
94 const [i, j, k] = match
95 if (k) {
96 matching_blocks.push(match)
97 if (alo < i && blo < j) queue.push([alo, i, blo, j])
98 if (i + k < ahi && j + k < bhi) {
99 queue.push([i + k, ahi, j + k, bhi])
100 }
101 }
102 }
103 matching_blocks.sort((x, y) => x[0] - y[0] || x[1] - y[1] || x[2] - y[2])
104
105 let i1 = 0
106 let j1 = 0
107 let k1 = 0
108 const non_adjacent = []
109 for (const [i2, j2, k2] of matching_blocks) {
110 if (i1 + k1 === i2 && j1 + k1 === j2) {
111 k1 += k2
112 } else {
113 if (k1) non_adjacent.push([i1, j1, k1])
114 i1 = i2
115 j1 = j2
116 k1 = k2
117 }
118 }
119 if (k1) non_adjacent.push([i1, j1, k1])
120 non_adjacent.push([la, lb, 0])
121 this.matching_blocks = non_adjacent
122 return non_adjacent
123 }
124
125 ratio () {
126 const matches = this.get_matching_blocks()
127 .reduce((total, match) => total + match[2], 0)
128 return calculate_ratio(matches, this.a.length + this.b.length)
129 }
130
131 quick_ratio () {
132 if (this.fullbcount === undefined) {
133 this.fullbcount = new Map()
134 for (const element of this.b) {
135 this.fullbcount.set(element, (this.fullbcount.get(element) || 0) + 1)
136 }
137 }
138
139 const avail = new Map()
140 let matches = 0
141 for (const element of this.a) {
142 const numb = avail.has(element)
143 ? avail.get(element)
144 : this.fullbcount.get(element) || 0
145 avail.set(element, numb - 1)
146 if (numb > 0) matches++
147 }
148 return calculate_ratio(matches, this.a.length + this.b.length)
149 }
150
151 real_quick_ratio () {
152 return calculate_ratio(
153 Math.min(this.a.length, this.b.length),
154 this.a.length + this.b.length)
155 }
156 }
157
158 function compare_strings (a, b) {
159 const ac = Array.from(a)
160 const bc = Array.from(b)
161 for (let i = 0; i < Math.min(ac.length, bc.length); i++) {
162 const difference = ac[i].codePointAt(0) - bc[i].codePointAt(0)
163 if (difference) return difference
164 }
165 return ac.length - bc.length
166 }
167
168 function get_close_matches (word, possibilities, n = 3, cutoff = 0.6) {
169 if (!(n > 0)) throw new TypeError(`n must be > 0: ${n}`)
170 if (!(cutoff >= 0 && cutoff <= 1)) {
171 throw new TypeError(`cutoff must be in [0.0, 1.0]: ${cutoff}`)
172 }
173
174 const result = []
175 const matcher = new SequenceMatcher()
176 matcher.set_seq2(word)
177 for (const possibility of possibilities) {
178 matcher.set_seq1(possibility)
179 if (matcher.real_quick_ratio() >= cutoff &&
180 matcher.quick_ratio() >= cutoff &&
181 matcher.ratio() >= cutoff) {
182 result.push([matcher.ratio(), possibility])
183 }
184 }
185
186 result.sort((a, b) => b[0] - a[0] || -compare_strings(a[1], b[1]))
187 return result.slice(0, n).map(match => match[1])
188 }
189
190 module.exports = { get_close_matches }