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

nicholaswmin / automap / 9855082686

09 Jul 2024 10:08AM UTC coverage: 92.515% (+0.03%) from 92.486%
9855082686

Pull #11

github

web-flow
Merge a3d5d9db2 into e6bd4a332
Pull Request #11: Benchmark

459 of 463 branches covered (99.14%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

2013 of 2209 relevant lines covered (91.13%)

18.39 hits per line

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

81.13
/src/map.js
1
import { traverse, getNodeByPath } from 'object-traversal'
1✔
2
import { setPathValue } from 'pathval'
1✔
3

1✔
4
import dot from 'dot-object'
1✔
5

1✔
6
import { List } from './list.js'
1✔
7

1✔
8
const expand = async (root, addItemsToSubstitutionsFn) => {
1✔
9
  const subs = []
×
10

×
11
  traverse(root, ({ key, value, meta }) => {
×
12
    if (!value?.includes?.(':')) return
×
13

×
14
    const [ path, traitsJSON ] = value.split(' ')
×
15
    const { nodePath } = meta
×
16
    const traits = JSON.parse(traitsJSON)
×
17

×
18
    if (traits.lazy)
×
19
      return
×
20

×
21
    subs.push({ path, nodePath, traits })
×
22
  })
×
23

×
24
  if (subs.length) {
×
25
    const subsWithItems = await addItemsToSubstitutionsFn(subs)
×
26

×
27
    subsWithItems.forEach(subWithItems => {
×
28
      setPathValue(root, subWithItems.nodePath, subWithItems.items)
×
29
    })
×
30

×
31
    return expand(root, addItemsToSubstitutionsFn)
×
32
  }
×
33

×
34
  return root // @FIXME might be broken, dont trust this
×
35
}
×
36

1✔
37
const flatten = root =>  {
1✔
38
  const branches = []
41✔
39

41✔
40
  traverse(root, ({ value, meta }) => {
41✔
41
    if (!(value instanceof List))
1,141✔
42
      return
1,141✔
43

164✔
44
    const branch = branches[0]
164✔
45
    const lastnode = branch?.[0]
1,141✔
46
    const node = new Node({ root, dotpath: meta.nodePath })
1,141✔
47

1,141✔
48
    return lastnode?.isAncestorOrSibling(node) ?
1,141✔
49
      branch.unshift(node) : branches.unshift([ node ])
1,141✔
50
  })
41✔
51

41✔
52
  return {
41✔
53
    lists: branches.flat()
41✔
54
      .map(node => node.captureValue())
41✔
55
      .filter(result => Object.keys(result.value).length),
41✔
56
    root: {
41✔
57
      key: root.constructor.name.toLowerCase().trim() + ':' + root.id.trim(),
41✔
58
      value: JSON.stringify(root)
41✔
59
    }
41✔
60
  }
41✔
61
}
41✔
62

1✔
63
class Node {
1✔
64
  #root
164✔
65

164✔
66
  constructor({ root, dotpath }) {
164✔
67
    this.#root = root
164✔
68

164✔
69
    this.dotpath = dotpath
164✔
70
    this.storepath = this.#getRootpath() + this.#storepathFromDotpath(dotpath)
164✔
71
    this.value = null
164✔
72
  }
164✔
73

164✔
74
  captureValue() {
164✔
75
    const prop = this.dotpath.split('.').pop()
164✔
76
    const result = { [prop]: this.storepath }
164✔
77

164✔
78
    dot.transfer(this.dotpath, 'value', this.#root, result)
164✔
79

164✔
80
    const traits = result.value.constructor.traits
164✔
81
    result[prop] = this.storepath + ' ' + JSON.stringify(traits)
164✔
82

164✔
83
    dot.copy(prop, this.dotpath, result, this.#root)
164✔
84

164✔
85
    return Object.assign({}, result.value.exportForSave(), { key: this.storepath })
164✔
86
  }
164✔
87

164✔
88
  hasItems() {
164✔
89
    return Array.isArray(this.value) ? !!this.value.length : !!this.value
×
90
  }
×
91

164✔
92
  isAncestorOrSibling(node) {
164✔
93
    const myRoutepath = this.#routepathFromDotpath(this.dotpath)
123✔
94
    const otherRoutepath = this.#routepathFromDotpath(node.dotpath)
123✔
95

123✔
96
    return otherRoutepath.includes(myRoutepath)
123✔
97
  }
123✔
98

164✔
99
  #storepathFromDotpath (dotpath) {
164✔
100
    return dotpath.split('.').reduce((current, dotpart, i, arr) => {
164✔
101
      const indexpath = current.indexpath + ( i ? '.' : '') + dotpart
328✔
102
      const curritem = getNodeByPath(this.#root, indexpath)
328✔
103
      const id = curritem.id || dotpart
328✔
104
      const idAtIndex = Array.isArray(curritem) ? dotpart : id
328✔
105
      const storepath = current.storepath + ( i ? ':' : '' ) + idAtIndex
328✔
106

328✔
107
      return i < arr.length - 1 ?
328✔
108
        { storepath, indexpath } : storepath
328✔
109
    }, { storepath: '', indexpath: '' })
164✔
110
  }
164✔
111

164✔
112
  #routepathFromDotpath(dotpath) {
164✔
113
    return dotpath.split('.')
246✔
114
      .reduce((acc, dotpart, i) =>
246✔
115
        acc += ( i ? '.' : '') + (isNaN(dotpart) ? dotpart : '*'), '')
246✔
116
  }
246✔
117

164✔
118
  #getRootpath() {
164✔
119
    return this.#root.constructor.name.toLowerCase()
164✔
120
      + (this.#root.id ? (':' + this.#root.id) : '' )
164!
121
      + ':'
164✔
122
  }
164✔
123
}
164✔
124

1✔
125
export { flatten, expand }
1✔
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

© 2026 Coveralls, Inc