All files matcher.ts

94.03% Statements 126/134
86.41% Branches 89/103
96% Functions 24/25
94.4% Lines 118/125

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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 2473x                                       3x   326x   3x                         112x 112x 112x 112x 112x 112x     3x 3x     112x 310x         252x 249x 242x 242x 149x 149x     242x       263x 260x 260x 51x 51x 56x 11x               249x 141x     249x 12x   12x     237x 230x       249x 91x 18x 16x   2x 2x         73x 5x   68x       158x     3x 3x           3x             3x 10x     3x 116x 116x 116x 116x 85x 31x     54x     31x   116x 116x     82x 79x 79x 79x 79x 131x 131x       79x 79x     3x 6x 3x 3x               3x 3x             3x 136x 136x     3x 616x 6x 3x 2x   610x 260x 350x 3x 347x   347x 10x 337x 116x 221x 79x 142x 6x 136x 136x           3x 112x 112x 112x 4x     108x     3x 6x 6x 4x 4x 4x 4x 4x   4x   4x 4x     4x   3x  
import {
  Segments,
  Node,
  isIdentifier,
  isExpandOperator,
  isWildcardOperator,
  isGroupExpression,
  isRangeExpression,
  isIgnoreExpression,
  isDotOperator,
  isDestructorExpression,
  IdentifierNode,
  IgnoreExpressionNode,
  DestructorExpressionNode,
  ExpandOperatorNode,
  WildcardOperatorNode,
  GroupExpressionNode,
  RangeExpressionNode,
  DotOperatorNode
} from './types'
import { isEqual, toArray } from './utils'
 
const isValid = val => val !== undefined && val !== null && val !== ''
 
export class Matcher {
  private tree: Node
 
  private pos: number
 
  private tail: Node
 
  private stack: any[]
 
  private excluding: boolean
 
  private record: any
 
  constructor(tree: Node, record?: any) {
    this.tree = tree
    this.pos = 0
    this.excluding = false
    this.record = record
    this.stack = []
  }
 
  currentElement(path: Segments) {
    return String(path[this.pos] || '').replace(/\s*/g, '')
  }
 
  matchNext = (node: any, path: any) => {
    return node.after
      ? this.matchAtom(path, node.after)
      : isValid(path[this.pos])
  }
 
  recordMatch(match: () => boolean) {
    return () => {
      const result = match()
      if (result) {
        Eif (this.record && this.record.score !== undefined) {
          this.record.score++
        }
      }
      return result
    }
  }
 
  matchIdentifier(path: Segments, node: IdentifierNode) {
    this.tail = node
    if (isValid(path[this.pos + 1]) && !node.after) {
      Eif (this.stack.length) {
        for (let i = this.stack.length - 1; i >= 0; i--) {
          if (!this.stack[i].after || !this.stack[i].filter) {
            return false
          }
        }
      } else {
        return false
      }
    }
    let current: any
    const next = () => {
      return this.matchNext(node, path)
    }
 
    if (isExpandOperator(node.after)) {
      current = this.recordMatch(
        () =>
          node.value === String(path[this.pos]).substring(0, node.value.length)
      )
    } else {
      current = this.recordMatch(() =>
        isEqual(String(node.value), String(path[this.pos]))
      )
    }
 
    if (this.excluding) {
      if (node.after) {
        if (this.pos < path.length) {
          return current() && next()
        } else {
          Eif (node.after && isWildcardOperator(node.after.after)) {
            return true
          }
          return false
        }
      } else {
        if (this.pos >= path.length) {
          return true
        }
        return current()
      }
    }
 
    return current() && next()
  }
 
  matchIgnoreExpression(path: Segments, node: IgnoreExpressionNode) {
    return (
      isEqual(node.value, this.currentElement(path)) &&
      this.matchNext(node, path)
    )
  }
 
  matchDestructorExpression(path: Segments, node: DestructorExpressionNode) {
    return (
      isEqual(node.source, this.currentElement(path)) &&
      this.matchNext(node, path)
    )
  }
 
  matchExpandOperator(path: Segments, node: ExpandOperatorNode) {
    return this.matchAtom(path, node.after)
  }
 
  matchWildcardOperator(path: Segments, node: WildcardOperatorNode) {
    this.tail = node
    this.stack.push(node)
    let matched = false
    if (node.filter) {
      if (node.after) {
        matched =
          this.matchAtom(path, node.filter) && this.matchAtom(path, node.after)
      } else {
        matched = this.matchAtom(path, node.filter)
      }
    } else {
      matched = this.matchNext(node, path)
    }
    this.stack.pop()
    return matched
  }
 
  matchGroupExpression(path: Segments, node: GroupExpressionNode) {
    const current = this.pos
    this.excluding = !!node.isExclude
    const method = this.excluding ? 'every' : 'some'
    const result = toArray(node.value)[method](_node => {
      this.pos = current
      return this.excluding
        ? !this.matchAtom(path, _node)
        : this.matchAtom(path, _node)
    })
    this.excluding = false
    return result
  }
 
  matchRangeExpression(path: Segments, node: RangeExpressionNode) {
    if (node.start) {
      Eif (node.end) {
        return (
          path[this.pos] >= parseInt(node.start.value) &&
          path[this.pos] <= parseInt(node.end.value)
        )
      } else {
        return path[this.pos] >= parseInt(node.start.value)
      }
    } else {
      Eif (node.end) {
        return path[this.pos] <= parseInt(node.end.value)
      } else {
        return true
      }
    }
  }
 
  matchDotOperator(path: Segments, node: DotOperatorNode) {
    this.pos++
    return this.matchNext(node, path)
  }
 
  matchAtom(path: Segments, node: Node) {
    if (!node) {
      if (this.stack.length > 0) return true
      if (isValid(path[this.pos + 1])) return false
      Eif (this.pos == path.length - 1) return true
    }
    if (isIdentifier(node)) {
      return this.matchIdentifier(path, node)
    } else if (isIgnoreExpression(node)) {
      return this.matchIgnoreExpression(path, node)
    } else Iif (isDestructorExpression(node)) {
      return this.matchDestructorExpression(path, node)
    } else if (isExpandOperator(node)) {
      return this.matchExpandOperator(path, node)
    } else if (isWildcardOperator(node)) {
      return this.matchWildcardOperator(path, node)
    } else if (isGroupExpression(node)) {
      return this.matchGroupExpression(path, node)
    } else if (isRangeExpression(node)) {
      return this.matchRangeExpression(path, node)
    } else Eif (isDotOperator(node)) {
      return this.matchDotOperator(path, node)
    }
 
    return true
  }
 
  match(path: Segments) {
    const matched = this.matchAtom(path, this.tree)
    Iif (!this.tail) return { matched: false }
    if (this.tail == this.tree && isWildcardOperator(this.tail)) {
      return { matched: true }
    }
 
    return { matched, record: this.record }
  }
 
  static matchSegments(source: Segments, target: Segments, record?: any) {
    let pos = 0
    if (source.length !== target.length) return false
    const match = (pos: number) => {
      const current = () => {
        const res = isEqual(source[pos], target[pos])
        Eif (record && record.score !== undefined) {
          record.score++
        }
        return res
      }
      const next = () => (pos < source.length - 1 ? match(pos + 1) : true)
      return current() && next()
    }
 
    return { matched: match(pos), record }
  }
}