aboutsummaryrefslogtreecommitdiff
path: root/src/services/theme_data/iss_deserializer.js
blob: 431e1b947a6f5fb714a85d346a84aacc136ee7db (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
36
37
38
39
40
41
42
43
// this works nearly the same as HTML tree converter
export const deserialize = (input) => {
  const buffer = []
  let textBuffer = ''

  const getCurrentBuffer = () => {
    let current = buffer[buffer.length - 1][1]
    if (current == null) {
      current = { name: null, content: [] }
    }
    buffer.push(current)
    return current
  }

  // Processes current line buffer, adds it to output buffer and clears line buffer
  const flushText = (content) => {
    if (textBuffer === '') return
    if (content) {
      getCurrentBuffer().content.push(textBuffer)
    } else {
      getCurrentBuffer().name = textBuffer
    }
    textBuffer = ''
  }

  for (let i = 0; i < input.length; i++) {
    const char = input[i]

    if (char === ';') {
      flushText(true)
    } else if (char === '{') {
      flushText(false)
    } else if (char === '}') {
      buffer.push({ name: null, content: [] })
      textBuffer = ''
    } else {
      textBuffer += char
    }
  }

  flushText()
  return buffer
}