• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lucaong / minisearch / #493

pending completion
#493

push

GitHub Actions

lucaong
Rebuild docs after typo fixes

212 of 217 branches covered (97.7%)

Branch coverage included in aggregate %.

668 of 668 relevant lines covered (100.0%)

755.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/SearchableMap/TreeIterator.ts
1
import { RadixTree, Entry, LeafType } from './types'
2

3
/** @ignore */
4
const ENTRIES = 'ENTRIES'
2✔
5

6
/** @ignore */
7
const KEYS = 'KEYS'
2✔
8

9
/** @ignore */
10
const VALUES = 'VALUES'
2✔
11

12
/** @ignore */
13
const LEAF = '' as LeafType
2✔
14

15
interface Iterators<T> {
16
  ENTRIES: Entry<T>
17
  KEYS: string
18
  VALUES: T
19
}
20

21
type Kind<T> = keyof Iterators<T>
22
type Result<T, K extends keyof Iterators<T>> = Iterators<T>[K]
23

24
type IteratorPath<T> = {
25
  node: RadixTree<T>,
26
  keys: string[]
27
}[]
28

29
export type IterableSet<T> = {
30
  _tree: RadixTree<T>,
31
  _prefix: string
32
}
33

34
/**
35
 * @private
36
 */
37
class TreeIterator<T, K extends Kind<T>> implements Iterator<Result<T, K>> {
2✔
38
  set: IterableSet<T>
39
  _type: K
40
  _path: IteratorPath<T>
41

42
  constructor (set: IterableSet<T>, type: K) {
43
    const node = set._tree
516✔
44
    const keys = Array.from(node.keys())
516✔
45
    this.set = set
516✔
46
    this._type = type
516✔
47
    this._path = keys.length > 0 ? [{ node, keys }] : []
516✔
48
  }
49

50
  next (): IteratorResult<Result<T, K>> {
2✔
51
    const value = this.dive()
2,003✔
52
    this.backtrack()
2,003✔
53
    return value
2,003✔
54
  }
55

56
  dive (): IteratorResult<Result<T, K>> {
2✔
57
    if (this._path.length === 0) { return { done: true, value: undefined } }
3,538✔
58
    const { node, keys } = last(this._path)!
3,022✔
59
    if (last(keys) === LEAF) { return { done: false, value: this.result() } }
3,022✔
60

61
    const child = node.get(last(keys)!)!
1,535✔
62
    this._path.push({ node: child, keys: Array.from(child.keys()) })
1,535✔
63
    return this.dive()
1,535✔
64
  }
65

66
  backtrack (): void {
2✔
67
    if (this._path.length === 0) { return }
3,817✔
68
    const keys = last(this._path)!.keys
3,022✔
69
    keys.pop()
3,022✔
70
    if (keys.length > 0) { return }
3,022✔
71
    this._path.pop()
1,814✔
72
    this.backtrack()
1,814✔
73
  }
74

75
  key (): string {
2✔
76
    return this.set._prefix + this._path
1,455✔
77
      .map(({ keys }) => last(keys))
3,260✔
78
      .filter(key => key !== LEAF)
3,260✔
79
      .join('')
80
  }
81

82
  value (): T {
2✔
83
    return last(this._path)!.node.get(LEAF)!
1,384✔
84
  }
85

86
  result (): Result<T, K> {
2✔
87
    switch (this._type) {
1,487✔
88
      case VALUES: return this.value() as Result<T, K>
32✔
89
      case KEYS: return this.key() as Result<T, K>
103✔
90
      default: return [this.key(), this.value()] as Result<T, K>
1,352✔
91
    }
92
  }
93

94
  [Symbol.iterator] () {
2✔
95
    return this
211✔
96
  }
97
}
2✔
98

99
const last = <T>(array: T[]): T | undefined => {
2✔
100
  return array[array.length - 1]
15,245✔
101
}
102

103
export { TreeIterator, ENTRIES, KEYS, VALUES, LEAF }
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc