{"size": 876, "ext": "js", "max_stars_repo_path": "node_modules/utility/lib/optimize.js", "max_stars_repo_name": "thenodejsboilerplate/discussion", "max_stars_repo_stars_event_min_datetime": "2015-08-09T18:05:47.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-25T07:18:08.000Z", "max_issues_repo_path": "node_modules/utility/lib/optimize.js", "max_issues_repo_name": "hanyibaby/cookMedia", "max_issues_repo_issues_event_min_datetime": "2021-01-28T20:13:49.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T17:46:07.000Z", "max_forks_repo_path": "node_modules/utility/lib/optimize.js", "max_forks_repo_name": "hanyibaby/cookMedia", "max_forks_repo_forks_event_min_datetime": "2015-08-10T07:09:49.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-16T11:43:50.000Z"}
{"max_stars_count": 23, "max_issues_count": 7, "max_forks_count": 16, "avg_line_length": 15.1034482759, "max_line_length": 55, "alphanum_fraction": 0.5331050228}
/*!
 * utility - lib/optimize.js
 *
 * Copyright(c) 2012 - 2014 fengmk2 <fengmk2@gmail.com>
 * MIT Licensed
 */

"use strict";

/**
 * optimize try catch
 * @param {Function} fn
 * @return {Object}
 *   - {Error} error
 *   - {Mix} value
 */
exports.try = function (fn) {
  var res = {
    error: undefined,
    value: undefined
  };

  try {
    res.value = fn();
  } catch (err) {
    res.error = err instanceof Error
      ? err
      : new Error(err);
  }

  return res;
};

/**
 * avoid if (a && a.b && a.b.c)
 * @param {Object} obj
 * @param {...String} keys
 * @return {Object}
 */
exports.dig = function (obj) {
  if (!obj) {
    return;
  }
  if (arguments.length <= 1) {
    return obj;
  }

  var value = obj[arguments[1]];
  for (var i = 2; i < arguments.length; i++) {
    if (!value) {
      break;
    }
    value = value[arguments[i]];
  }

  return value;
};
