chain-able
Version:
interfaces that describe their intentions.
40 lines (33 loc) • 998 B
JavaScript
const isArray = require('../is/array')
const isUndefined = require('../is/undefined')
const lengthMinusOne = require('../util/lengthMinusOne')
let cache
module.exports = path => {
if (!cache) cache = new Map()
if (cache.has(path)) return cache.get(path)
if (isArray(path)) return path
const pathArr = path.split('.')
const parts = []
for (let i = 0; i < pathArr.length; i++) {
let p = pathArr[i]
/**
* @example 1
* '\.eh' -1 === '\\' (true)
* +1 !== undefined (true, eh)
*
* @example 2
* '.eh' -1 === '\\' (false, undefined)
* +1 !== undefined (true, eh)
*
* @example 3
* '\.' -1 === '\\' (true)
* +1 !== undefined (false, eh)
*/
while (p[lengthMinusOne(p)] === '\\' && !isUndefined(pathArr[i + 1])) {
p = p.slice(0, -1) + '.' + pathArr[++i]
}
parts.push(p)
}
cache.set(path, parts)
return parts
}