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

nicholaswmin / automap / 9927329623

14 Jul 2024 10:35AM UTC coverage: 92.3%. Remained the same
9927329623

push

github

Nicholas Kyriakides
docs: fix wrong example

511 of 523 branches covered (97.71%)

Branch coverage included in aggregate %.

2186 of 2399 relevant lines covered (91.12%)

24.04 hits per line

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

79.14
/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, ({ value, meta }) => {
×
12
    if (!value?.includes?.(':')) return
×
13

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

×
17
    if (!traitsJSON)
×
18
      return
×
19

×
20
    const traits = JSON.parse(traitsJSON)
×
21

×
22
    if (traits.lazy)
×
23
      return
×
24

×
25
    subs.push({ path, nodePath, traits })
×
26
  })
×
27

×
28
  if (subs.length) {
×
29
    const subsWithItems = await addItemsToSubstitutionsFn(subs)
×
30

×
31
    subsWithItems.forEach(subWithItems => {
×
32
      setPathValue(root, subWithItems.nodePath, subWithItems.items)
×
33
    })
×
34

×
35
    return expand(root, addItemsToSubstitutionsFn)
×
36
  }
×
37

×
38
  return root // @FIXME might be broken, dont trust this
×
39
}
×
40

1✔
41
const flatten = root =>  {
1✔
42
  const branches = []
44✔
43

44✔
44
  traverse(root, ({ value, meta }) => {
44✔
45
    if (!(value instanceof List))
1,496✔
46
      return
1,496✔
47

296✔
48
    const branch = branches[0]
296✔
49
    const lastnode = branch?.[0]
1,496✔
50
    const node = new Node({ root, dotpath: meta.nodePath })
1,496✔
51

1,496✔
52
    return lastnode?.isAncestorOrSibling(node) ?
1,496✔
53
      branch.unshift(node) : branches.unshift([ node ])
1,496✔
54
  })
44✔
55

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

1✔
67
class Node {
1✔
68
  #root
296✔
69

296✔
70
  constructor({ root, dotpath }) {
296✔
71
    this.#root = root
296✔
72

296✔
73
    this.dotpath = dotpath
296✔
74
    this.storepath = this.#getRootpath() + this.#storepathFromDotpath(dotpath)
296✔
75
    this.value = null
296✔
76
  }
296✔
77

296✔
78
  captureValue() {
296✔
79
    const prop = this.dotpath.split('.').pop()
296✔
80
    const result = { [prop]: this.storepath }
296✔
81

296✔
82
    dot.transfer(this.dotpath, 'value', this.#root, result)
296✔
83

296✔
84
    const traits = result.value.constructor.traits
296✔
85
    result[prop] = this.storepath + ' ' + JSON.stringify(traits)
296✔
86

296✔
87
    dot.copy(prop, this.dotpath, result, this.#root)
296✔
88

296✔
89
    return Object.assign({}, result.value.exportForSave(), { key: this.storepath })
296✔
90
  }
296✔
91

296✔
92
  hasItems() {
296✔
93
    return Array.isArray(this.value) ? !!this.value.length : !!this.value
×
94
  }
×
95

296✔
96
  isAncestorOrSibling(node) {
296✔
97
    const myRoutepath = this.#routepathFromDotpath(this.dotpath)
252✔
98
    const otherRoutepath = this.#routepathFromDotpath(node.dotpath)
252✔
99

252✔
100
    return otherRoutepath.includes(myRoutepath)
252✔
101
  }
252✔
102

296✔
103
  #storepathFromDotpath (dotpath) {
296✔
104
    return dotpath.split('.').reduce((current, dotpart, i, arr) => {
296✔
105
      const indexpath = current.indexpath + ( i ? '.' : '') + dotpart
624✔
106
      const curritem = getNodeByPath(this.#root, indexpath)
624✔
107
      const id = curritem.id || dotpart
624✔
108
      const idAtIndex = Array.isArray(curritem) ? dotpart : id
624✔
109
      const storepath = current.storepath + ( i ? ':' : '' ) + idAtIndex
624✔
110

624✔
111
      return i < arr.length - 1 ?
624✔
112
        { storepath, indexpath } : storepath
624✔
113
    }, { storepath: '', indexpath: '' })
296✔
114
  }
296✔
115

296✔
116
  #routepathFromDotpath(dotpath) {
296✔
117
    return dotpath.split('.')
504✔
118
      .reduce((acc, dotpart, i) =>
504✔
119
        acc += ( i ? '.' : '') + (isNaN(dotpart) ? dotpart : '*'), '')
504✔
120
  }
504✔
121

296✔
122
  #getRootpath() {
296✔
123
    return this.#root.constructor.name.toLowerCase()
296✔
124
      + (this.#root.id ? (':' + this.#root.id) : '' )
296!
125
      + ':'
296✔
126
  }
296✔
127
}
296✔
128

1✔
129
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