{"size": 1004, "ext": "js", "max_stars_repo_path": "common/utils.js", "max_stars_repo_name": "agauniyal/mw-server", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "common/utils.js", "max_issues_repo_name": "agauniyal/mw-server", "max_issues_repo_issues_event_min_datetime": "2017-03-19T09:41:00.000Z", "max_issues_repo_issues_event_max_datetime": "2017-12-25T16:44:32.000Z", "max_forks_repo_path": "common/utils.js", "max_forks_repo_name": "agauniyal/mw-server", "max_forks_repo_forks_event_min_datetime": "2017-03-19T09:04:49.000Z", "max_forks_repo_forks_event_max_datetime": "2017-03-19T09:04:49.000Z"}
{"max_stars_count": null, "max_issues_count": 17, "max_forks_count": 1, "avg_line_length": 20.9166666667, "max_line_length": 81, "alphanum_fraction": 0.6185258964}
'use strict';

const http = require('http');
const config = require('../config/config');


const safePort = (val) => {
  const port = parseInt(val, 10);
  if (isNaN(port)) {
    return config.port;
  }
  if (port >= 0 && port < 65535) {
    return port;
  }
  return config.port;
};

const isValidParam = (path, callback) => {
  if (Object.prototype.toString.call(path) !== '[object String]' ||
    path.length < 1 || typeof callback !== 'function' || callback.length !== 2) {
    return false;
  } else {
    return true;
  }
};

const setCors = (res) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Origin');
};


const setStatus = (res, status, end = false) => {
  res.statusCode = status;
  res.statusMessage = http.STATUS_CODES[status];
  if (end === true) {
    res.end();
  }
};

module.exports = {
  safePort,
  isValidParam,
  setCors,
  setStatus
};
