All files index.js

85.59% Statements 101/118
79.05% Branches 83/105
90.48% Functions 19/21
88.99% Lines 97/109

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218      1x 72x 72x     50x   140x   1x   1x 95x     1x 61x 61x 61x 61x 301x         301x 301x   140x 140x 8x                     132x 41x 39x 42x 5x     2x     125x                 57x 57x 57x         57x 57x   34x 17x 41x           41x 40x   41x     17x 31x 31x 14x   31x       6x 6x 3x 3x                       3x 3x                 64x 64x           61x 61x 61x   61x 61x 4x     57x       1x 61x     1x 50x 50x 50x 45x 38x   45x 1x   45x             1x 2x 2x 2x 8x     8x 5x     2x     1x 5x 5x 21x 3x 3x     5x     1x 4x 4x 21x 21x   1x   1x       20x       2x 2x     4x     1x 30x 61x          
import { Parser } from "./parser"
import memo from "fast-memoize"
 
export const parseDPML = string => {
    const parser = new Parser(string)
    return parser.parse()
}
 
const isFn = val => typeof val === "function"
 
const isArr = val => Array.isArray(val)
 
const isStr = val => typeof val == "string"
 
const toArray = val => {
    return isArr(val) ? val : val ? [val] : []
}
 
const createMatcherByAST = root => {
    let stepIndex = 0
    let lastNode = root
    let parents = []
    const matchPath = (path, node, start = 0) => {
        Iif (!node) {
            if (path[start + 1]) return false
            if (start == path.length - 1) return true
        }
 
        Eif (node) {
            switch (node.type) {
                case "Identifier":
                    lastNode = node
                    if (node.after && node.after.type === "ExpandOperator") {
                        return (
                            node.value ===
                                String(path[start]).substring(
                                    0,
                                    node.value.length
                                ) &&
                            (node.after.after
                                ? matchPath(path, node.after.after, start)
                                : !!path[start])
                        )
                    }
                    if (path[start + 1] && !node.after) {
                        if (parents.length) {
                            for (let i = parents.length - 1; i >= 0; i--) {
                                if (!parents[i].after || !parents[i].filter)
                                    return false
                            }
                        } else {
                            return false
                        }
                    }
                    return (
                        node.value == path[start] &&
                        (node.after
                            ? matchPath(path, node.after, start)
                            : !!path[start])
                    )
                case "ExpandOperator":
                    return matchPath(path, node.after, start)
                case "WildcardOperator":
                    lastNode = node
                    parents.push(node)
                    const result = node.filter
                        ? matchPath(path, node.filter, start)
                        : node.after
                        ? matchPath(path, node.after, start)
                        : !!path[start]
                    parents.pop()
                    return result
                case "GroupExpression":
                    if (node.isExclude) {
                        return toArray(node.value).every((_node, index) => {
                            const unmatched = !matchPath(
                                path,
                                _node,
                                start,
                                node
                            )
                            if (unmatched) {
                                stepIndex = start
                            }
                            return unmatched
                        })
                    } else {
                        return toArray(node.value).some(_node => {
                            const matched = matchPath(path, _node, start, node)
                            if (!matched) {
                                stepIndex = start
                            }
                            return matched
                        })
                    }
                case "RangeExpression":
                    const parent = parents[parents.length - 1]
                    if (node.start) {
                        Eif (node.end) {
                            return (
                                path[start] >= parseInt(node.start.value) &&
                                path[start] <= parseInt(node.end.value) &&
                                matchPath(path, parent.after, start)
                            )
                        } else {
                            return (
                                path[start] >= parseInt(node.start.value) &&
                                matchPath(path, parent.after, start)
                            )
                        }
                    } else {
                        Eif (node.end) {
                            return (
                                path[start] <= parseInt(node.end.value) &&
                                matchPath(path, parent.after, start)
                            )
                        } else {
                            return matchPath(path, parent.after, start)
                        }
                    }
                case "DotOperator":
                    stepIndex++
                    return matchPath(path, node.after, start + 1)
            }
        }
 
        return true
    }
    return path => {
        stepIndex = 0
        const matchResult = matchPath(path, root)
 
        Iif (!lastNode) return false
        if (lastNode == root && lastNode.type === "WildcardOperator") {
            return true
        }
 
        return matchResult
    }
}
 
const matchAll = memo((string, path) => {
    return createMatcherByAST(parseDPML(string))(toArray(path))
})
 
const traverse = (ast, callback) => {
    Iif (!isFn(callback)) return
    const result = callback(ast)
    if (result === false) return
    if (ast.after) {
        traverse(ast.after, callback)
    }
    if (ast.filter) {
        traverse(ast.filter, callback)
    }
    Iif (isArr(ast.value)) {
        ast.value.forEach(node => {
            traverse(node, callback)
        })
    }
}
 
export const getPathSegments = memo(pattern => {
    let segments = []
    let isAbsolutePath = true
    traverse(parseDPML(pattern), ({ type, value }) => {
        Iif (type !== "Identifier" && type !== "DotOperator") {
            isAbsolutePath = false
            return false
        } else if (type == "Identifier") {
            segments.push(value)
        }
    })
    return isAbsolutePath ? segments : []
})
 
export const isAbsolutePath = memo(pattern => {
    let isAbsolutePath = true
    traverse(parseDPML(pattern), ({ type }) => {
        if (type !== "Identifier" && type !== "DotOperator") {
            isAbsolutePath = false
            return false
        }
    })
    return isAbsolutePath
})
 
export const isWildMatchPath = memo(pattern => {
    let hasWildMatchPath = false
    traverse(parseDPML(pattern), node => {
        const { type } = node
        if (type === "RangeExpression") {
            const isWild =
                (!node.end.value && !!node.start.value) ||
                (!node.start.value && !!node.end.value)
            Iif (isWild) {
                hasWildMatchPath = true
                return false
            }
        } else if (
            (type === "WildcardOperator" && !node.filter) ||
            type === "ExpandOperator"
        ) {
            hasWildMatchPath = true
            return false
        }
    })
    return hasWildMatchPath
})
 
export const createMatcher = (string, cache) => {
    return path => {
        return matchAll(string, path)
    }
}
 
export default createMatcher