{"size": 1097, "ext": "js", "max_stars_repo_path": "lib/utils/detectSelectorType.js", "max_stars_repo_name": "newagebegins/phantom-lord", "max_stars_repo_stars_event_min_datetime": "2020-10-27T16:09:39.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-22T10:46:04.000Z", "max_issues_repo_path": "lib/utils/detectSelectorType.js", "max_issues_repo_name": "newagebegins/phantom-lord", "max_issues_repo_issues_event_min_datetime": "2020-11-06T14:25:08.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-14T14:33:50.000Z", "max_forks_repo_path": "lib/utils/detectSelectorType.js", "max_forks_repo_name": "newagebegins/phantom-lord", "max_forks_repo_forks_event_min_datetime": "2020-11-06T13:15:19.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-17T13:53:41.000Z"}
{"max_stars_count": 25, "max_issues_count": 12, "max_forks_count": 5, "avg_line_length": 28.8684210526, "max_line_length": 79, "alphanum_fraction": 0.6499544211}
/* eslint-disable no-prototype-builtins */
const supportedSelectorType = ['css', 'xpath'];

/**
 * @param {string|{type: string, path: string}} selector
 * @returns {{type: string, path: string}}
 */
const detectSelectorType = (selector) => {
  const selectorObject = {
    toString() {
      return `${this.type} selector: ${this.path}`;
    },
  };
  if (typeof selector === 'string') {
    // defaults to CSS selector
    selectorObject.type = 'css';
    selectorObject.path = selector;
    return selectorObject;
  }

  if (typeof selector === 'object') {
    // validation
    if (!selector.hasOwnProperty('type') || !selector.hasOwnProperty('path')) {
      throw new Error('Incomplete selector object');
    } else if (supportedSelectorType.indexOf(selector.type) === -1) {
      throw new Error(`Unsupported selector type: ${selector.type}`);
    }
    if (!selector.hasOwnProperty('toString')) {
      selector.toString = selectorObject.toString;
    }
    return selector;
  }

  throw new Error(`Unsupported selector type: ${typeof selector}`);
};

module.exports = detectSelectorType;
