All files path.js

26.7% Statements 47/176
7.94% Branches 10/126
14.81% Functions 4/27
27.71% Lines 46/166

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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352      1x   1x                                                                                                                                                                                                                                           2x 2x 2x 2x 2x 2x                             2x 2x               2x 2x             2x 2x 2x 2x 2x                                                                     2x             2x             2x                 2x               2x                 2x             2x             2x             2x       2x 1x 1x             1x     1x 1x 3x       1x     2x                             2x                                             2x   2x   2x     1x                 1x 1x 1x     1x 1x 1x          
import { Parser } from './parser'
import { isStr, isArr, isEqual } from './utils'
 
const pathCache = new Map()
 
const match = (path, tree) => {
  let stepIndex = 0
  let lastNode = tree
  let parents = []
  const _match = (path, node, start = 0) => {
    if (!node) {
      if (path[start + 1]) return false
      if (start == path.length - 1) return true
    }
 
    if (node) {
      switch (node.type) {
        case 'Identifier':
        case 'DestructorExpression':
          lastNode = node
          if (node.after && node.after.type === 'ExpandOperator') {
            return (
              node.value ===
                String(path[start]).substring(0, node.value.length) &&
              (node.after.after
                ? _match(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 (
            isEqual(node.value, path[start]) &&
            (node.after ? _match(path, node.after, start) : !!path[start])
          )
        case 'ExpandOperator':
          return _match(path, node.after, start)
        case 'WildcardOperator':
          lastNode = node
          parents.push(node)
          const result = node.filter
            ? _match(path, node.filter, start)
            : node.after
            ? _match(path, node.after, start)
            : !!path[start]
          parents.pop()
          return result
        case 'GroupExpression':
          if (node.isExclude) {
            return toArray(node.value).every((_node, index) => {
              const unmatched = !_match(path, _node, start, node)
              if (unmatched) {
                stepIndex = start
              }
              return unmatched
            })
          } else {
            return toArray(node.value).some(_node => {
              const matched = _match(path, _node, start, node)
              if (!matched) {
                stepIndex = start
              }
              return matched
            })
          }
        case 'RangeExpression':
          const parent = parents[parents.length - 1]
          if (node.start) {
            if (node.end) {
              return (
                path[start] >= parseInt(node.start.value) &&
                path[start] <= parseInt(node.end.value) &&
                _match(path, parent.after, start)
              )
            } else {
              return (
                path[start] >= parseInt(node.start.value) &&
                _match(path, parent.after, start)
              )
            }
          } else {
            if (node.end) {
              return (
                path[start] <= parseInt(node.end.value) &&
                _match(path, parent.after, start)
              )
            } else {
              return _match(path, parent.after, start)
            }
          }
        case 'DotOperator':
          stepIndex++
          return _match(path, node.after, start + 1)
      }
    }
 
    return true
  }
  const result = _match(path, tree)
 
  if (!lastNode) return false
  if (lastNode == tree && lastNode.type === 'WildcardOperator') {
    return true
  }
 
  return result
}
 
export class Path {
  constructor(input) {
    const {
      tree,
      segments,
      entire,
      isMatchPattern,
      isWildMatchPattern
    } = this.parse(input)
    this.entire = entire
    this.segments = segments
    this.isMatchPattern = isMatchPattern
    this.isWildMatchPattern = isWildMatchPattern
    this.tree = tree
  }
 
  toString() {
    return this.entire
  }
 
  toArray() {
    return this.segments
  }
 
  get length() {
    return this.segments.length
  }
 
  parse = pattern => {
    Iif (pattern instanceof Path) {
      return {
        entire: pattern.entire,
        segments: pattern.segments.slice(),
        isWildMatchPattern: pattern.isWildMatchPattern,
        isMatchPattern: pattern.isMatchPattern,
        tree: pattern.tree
      }
    } else Eif (isStr(pattern)) {
      Iif (!pattern)
        return {
          entire: '',
          segments: [],
          isWildMatchPattern: false,
          isMatchPattern: false
        }
      const parser = new Parser(pattern)
      const tree = parser.parse()
      Eif (!parser.isMatchPattern) {
        const segments = parser.data.segments
        return {
          entire: pattern,
          segments,
          tree,
          isWildMatchPattern: false,
          isMatchPattern: false
        }
      } else {
        return {
          entire: pattern,
          segments: [],
          isWildMatchPattern: parser.isWildMatchPattern,
          isMatchPattern: true,
          tree
        }
      }
    } else if (isArr(pattern)) {
      return {
        entire: pattern.join('.'),
        segments: pattern.reduce((buf, key) => {
          return buf.concat(this.parse(key).segments)
        }, []),
        isWildMatchPattern: false,
        isMatchPattern: false
      }
    } else {
      return {
        entire: '',
        segments: [],
        isWildMatchPattern: false,
        isMatchPattern: false
      }
    }
  }
 
  concat = (...args) => {
    if (this.isMatchPattern) {
      throw new Error(`${this.entire} cannot be concat`)
    }
    return FormPath.getPath(this.segments.concat(...args))
  }
 
  slice = (...args) => {
    if (this.isMatchPattern) {
      throw new Error(`${this.entire} cannot be slice`)
    }
    return FormPath.getPath(this.segments.slice(...args))
  }
 
  push = (...args) => {
    if (this.isMatchPattern) {
      throw new Error(`${this.entire} cannot be push`)
    }
    this.segments.push(...args)
    this.entire = this.segments.join('.')
    return this
  }
 
  pop = (...args) => {
    if (this.isMatchPattern) {
      throw new Error(`${this.entire} cannot be pop`)
    }
    this.segments.pop(...args)
    this.entire = this.segments.join('.')
  }
 
  splice = (...args) => {
    if (this.isMatchPattern) {
      throw new Error(`${this.entire} cannot be splice`)
    }
    this.segments.splice(...args)
    this.entire = this.segments.join('.')
    return this
  }
 
  forEach = callback => {
    if (this.isMatchPattern) {
      throw new Error(`${this.entire} cannot be each`)
    }
    each(this.segments, callback)
  }
 
  map = callback => {
    if (this.isMatchPattern) {
      throw new Error(`${this.entire} cannot be map`)
    }
    return map(this.segments, callback)
  }
 
  reduce = (callback, initial) => {
    if (this.isMatchPattern) {
      throw new Error(`${this.entire} cannot be reduce`)
    }
    return reduce(this.segments, callback, initial)
  }
 
  parent = () => {
    return this.slice(0, this.length - 1)
  }
 
  includes = pattern => {
    const { entire, segments, isMatchPattern } = Path.getPath(pattern)
    Iif (this.isMatchPattern) {
      if (!isMatchPattern) {
        return this.match(segments)
      } else {
        throw new Error(`${this.entire} cannot be used to match ${entire}`)
      }
    }
    Iif (isMatchPattern) {
      throw new Error(`${this.entire} cannot be used to match ${entire}`)
    }
    Iif (segments.length > this.segments.length) return false
    for (let i = 0; i < segments.length; i++) {
      Iif (!isEqual(segments[i], this.segments[i])) {
        return false
      }
    }
    return true
  }
 
  transform = (regexp, callback) => {
    if (!isFn(callback)) return ''
    if (this.isMatchPattern) {
      throw new Error(`${this.entire} cannot be transformed`)
    }
    const args = reduce(
      this.segments,
      (buf, key) => {
        return new RegExp(regexp).test(key) ? buf.concat(key) : buf
      },
      []
    )
    return callback(...args)
  }
 
  match = pattern => {
    const path = Path.getPath(pattern)
    if (path.isMatchPattern) {
      if (this.isMatchPattern) {
        throw new Error(`${path.entire} cannot match ${this.entire}`)
      } else {
        return path.match(this.segments)
      }
    } else {
      if (this.isMatchPattern) {
        return match(path.segments, this.tree)
      } else {
        if (path.segments.length != this.segments.length) return false
        for (let i = 0; i < path.segments.length; i++) {
          if (!isEqual(path.segments[i], this.segments[i])) {
            return false
          }
        }
      }
    }
    return true
  }
 
  getIn = () => {}
 
  setIn = () => {}
 
  deleteIn = () => {}
 
  static getPath(path = '') {
    Iif (path instanceof Path) {
      const found = pathCache.get(path.entire)
      if (found) {
        return found
      } else {
        pathCache.set(path.entire, path)
        return path
      }
    } else {
      const key = path.toString()
      const found = pathCache.get(key)
      Iif (found) {
        return found
      } else {
        path = new Path(path)
        pathCache.set(key, path)
        return path
      }
    }
  }
}