Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
index.js54 linesDownload Raw Back to lib
1module.exports = abbrev
2
3function abbrev (...args) {
4  let list = args
5  if (args.length === 1 && (Array.isArray(args[0]) || typeof args[0] === 'string')) {
6    list = [].concat(args[0])
7  }
8
9  for (let i = 0, l = list.length; i < l; i++) {
10    list[i] = typeof list[i] === 'string' ? list[i] : String(list[i])
11  }
12
13  // sort them lexicographically, so that they're next to their nearest kin
14  list = list.sort(lexSort)
15
16  // walk through each, seeing how much it has in common with the next and previous
17  const abbrevs = {}
18  let prev = ''
19  for (let ii = 0, ll = list.length; ii < ll; ii++) {
20    const current = list[ii]
21    const next = list[ii + 1] || ''
22    let nextMatches = true
23    let prevMatches = true
24    if (current === next) {
25      continue
26    }
27    let j = 0
28    const cl = current.length
29    for (; j < cl; j++) {
30      const curChar = current.charAt(j)
31      nextMatches = nextMatches && curChar === next.charAt(j)
32      prevMatches = prevMatches && curChar === prev.charAt(j)
33      if (!nextMatches && !prevMatches) {
34        j++
35        break
36      }
37    }
38    prev = current
39    if (j === cl) {
40      abbrevs[current] = current
41      continue
42    }
43    for (let a = current.slice(0, j); j <= cl; j++) {
44      abbrevs[a] = current
45      a += current.charAt(j)
46    }
47  }
48  return abbrevs
49}
50
51function lexSort (a, b) {
52  return a === b ? 0 : a > b ? 1 : -1
53}
54 
codekingpro/portable-devtools · Team Ai