Debian Codegraph @

workspaces/arborist/test/fixtures/utils.js

raw
1 // some utils that we use in a bunch of tests
2
3 const normalizePath = path => path.replace(/[A-Z]:/, '').replace(/\\/g, '/')
4 const normalizePaths = obj => {
5 if (obj instanceof Set) {
6 return new Set(normalizePaths([...obj]))
7 }
8
9 if (Array.isArray(obj)) {
10 return obj.map(normalizePaths)
11 }
12
13 if (typeof obj === 'string') {
14 return normalizePath(obj)
15 }
16
17 if (obj instanceof Map) {
18 return new Map([...obj].map(([name, val]) => [name, normalizePaths(val)]))
19 }
20
21 for (const key in obj) {
22 if (['location', 'path', 'realpath', 'resolved', 'spec'].includes(key)) {
23 obj[key] = normalizePath(obj[key])
24 } else if (typeof obj[key] === 'object' && obj[key] !== null) {
25 obj[key] = normalizePaths(obj[key])
26 }
27 }
28 return obj
29 }
30
31 const printTree = tree => normalizePaths(tree.toJSON())
32
33 module.exports = {
34 normalizePath,
35 normalizePaths,
36 printTree,
37 }