1const {parse, sep, normalize: norm} = require('path')
2
3function* commonArrayMembers (a, b) {
4  const [l, s] = a.length > b.length ? [a, b] : [b, a]
5  for (const x of s) {
6    if (x === l.shift())
7      yield x
8    else
9      break
10  }
11}
12
13const commonAncestorPath = (a, b) => a === b ? a
14  : parse(a).root !== parse(b).root ? null
15  : [...commonArrayMembers(norm(a).split(sep), norm(b).split(sep))].join(sep)
16
17module.exports = (...paths) => paths.reduce(commonAncestorPath)
18