All files index.js

100% Statements 40/40
100% Branches 24/24
100% Functions 5/5
100% Lines 39/39

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 631x 63x   486x 196x   290x 290x     165x   63x 63x     183x 13x     170x 13x     157x 37x   120x     328x   157x 14x 14x 14x 14x 14x 14x 14x 143x 22x 22x 22x 22x 22x 22x 22x   121x 121x 308x 120x           63x   63x    
module.exports = function(_src, _txt, _opts) {
  const is = {
    object: function(x) {
      if (Object.prototype.toString.call(x) !== '[object Object]') {
        return false
      } else {
        var prototype = Object.getPrototypeOf(x)
        return prototype === null || prototype === Object.prototype
      }
    },
    string: (x) => typeof x === 'string'
  }
  let result = { found: false }
  let path = ''
 
  function run(source, txt, options = { rootLabel: 'root' }, inner = { prop: 'root', parent: null }) {
    if (!source || !is.object(source)) {
      return result
    }
 
    if (!txt || !is.string(txt)) {
      return result
    }
 
    if (!path) {
      path = options.rootLabel
    } else {
      path += `.${inner.prop}`
    }
 
    let lastKeyInPath = txt.split('.').filter(s => s && s).pop()
 
    if (txt in source) {
      result.found = true
      result.search = txt
      result.value = source[txt]
      result.parent = inner.parent
      path += `.${txt}`
      result.path = path
      return result
    } else if (lastKeyInPath in source) {
      result.found = true
      result.search = txt
      result.value = source[lastKeyInPath]
      result.parent = inner.parent
      path += `.${txt}`
      result.path = path
      return result
    } else {
      let keys = Object.keys(source)
      for (let i = 0; i < keys.length; i++) {
        if (is.object(source[keys[i]])) {
          run(source[keys[i]], txt, options, { prop: keys[i], parent: { name: inner.prop, content: source} })
        }
      }
    }
  }
 
  run(_src, _txt, _opts)
 
  return result
}