{"size": 1443, "ext": "js", "max_stars_repo_path": "web/backend/node_modules/property-expr/compiler.js", "max_stars_repo_name": "hugofeitosa/happy", "max_stars_repo_stars_event_min_datetime": "2015-12-01T13:55:56.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-06T16:08:36.000Z", "max_issues_repo_path": "web/backend/node_modules/property-expr/compiler.js", "max_issues_repo_name": "hugofeitosa/happy", "max_issues_repo_issues_event_min_datetime": "2021-05-29T16:32:51.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-13T16:05:29.000Z", "max_forks_repo_path": "web/backend/node_modules/property-expr/compiler.js", "max_forks_repo_name": "hugofeitosa/happy", "max_forks_repo_forks_event_min_datetime": "2017-12-29T17:46:00.000Z", "max_forks_repo_forks_event_max_datetime": "2021-09-15T14:54:30.000Z"}
{"max_stars_count": 17, "max_issues_count": 116, "max_forks_count": 14, "avg_line_length": 21.2205882353, "max_line_length": 79, "alphanum_fraction": 0.5419265419}
var { Cache, normalizePath, split, forEach } = require('./')

var setCache = new Cache(512),
  getCache = new Cache(512)

function makeSafe(path, param) {
  var result = param,
    parts = split(path),
    isLast

  forEach(parts, function (part, isBracket, isArray, idx, parts) {
    isLast = idx === parts.length - 1

    part = isBracket || isArray ? '[' + part + ']' : '.' + part

    result += part + (!isLast ? ' || {})' : ')')
  })

  return new Array(parts.length + 1).join('(') + result
}

function expr(expression, safe, param) {
  expression = expression || ''

  if (typeof safe === 'string') {
    param = safe
    safe = false
  }

  param = param || 'data'

  if (expression && expression.charAt(0) !== '[') expression = '.' + expression

  return safe ? makeSafe(expression, param) : param + expression
}

module.exports = {
  expr,
  setter: function (path) {
    if (
      path.indexOf('__proto__') !== -1 ||
      path.indexOf('constructor') !== -1 ||
      path.indexOf('prototype') !== -1
    ) {
      return (obj) => obj
    }

    return (
      setCache.get(path) ||
      setCache.set(
        path,
        new Function('data, value', expr(path, 'data') + ' = value')
      )
    )
  },

  getter: function (path, safe) {
    var key = path + '_' + safe
    return (
      getCache.get(key) ||
      getCache.set(
        key,
        new Function('data', 'return ' + expr(path, safe, 'data'))
      )
    )
  },
}
