aboutsummaryrefslogtreecommitdiff
path: root/node_modules/concordance/lib/Circular.js
blob: c5c02026798991e6eba1912ddc583b39e1e222d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict'

class Circular {
  constructor () {
    this.stack = new Map()
  }

  add (descriptor) {
    if (this.stack.has(descriptor)) throw new Error('Already in stack')

    if (descriptor.isItem !== true && descriptor.isMapEntry !== true && descriptor.isProperty !== true) {
      this.stack.set(descriptor, this.stack.size + 1)
    }
    return this
  }

  delete (descriptor) {
    if (this.stack.has(descriptor)) {
      if (this.stack.get(descriptor) !== this.stack.size) throw new Error('Not on top of stack')
      this.stack.delete(descriptor)
    }
    return this
  }

  has (descriptor) {
    return this.stack.has(descriptor)
  }

  get (descriptor) {
    return this.stack.has(descriptor)
      ? this.stack.get(descriptor)
      : 0
  }
}
module.exports = Circular