{"size": 2372, "ext": "js", "max_stars_repo_path": "parsers/utils.js", "max_stars_repo_name": "ddangdol/webCrawler", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "parsers/utils.js", "max_issues_repo_name": "ddangdol/webCrawler", "max_issues_repo_issues_event_min_datetime": "2018-09-23T03:09:21.000Z", "max_issues_repo_issues_event_max_datetime": "2018-09-23T10:47:33.000Z", "max_forks_repo_path": "parsers/utils.js", "max_forks_repo_name": "ddangdol/webCrawler", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 6, "max_forks_count": null, "avg_line_length": 28.2380952381, "max_line_length": 108, "alphanum_fraction": 0.7508431703}
const fs = require('fs');

function multiSubstringWithWord(targetHtml, startWithWord, endWithWord, limit) {
  const results = [];
  let currentIndex = 0;
  for (let count = 0; count < limit-1; count++) {
    targetHtml = targetHtml.substring(currentIndex);
    currentIndex = substringWithWordThenReturnEndPosition(targetHtml, results, startWithWord, endWithWord);
  }
  return results;
}

function substringAllWithWord(targetHtml, startWithWord, endWithWord) {
  const results = [];
  let currentIndex = 0;
  while(currentIndex >= 0) {
    targetHtml = targetHtml.substring(currentIndex);
    currentIndex = substringWithWordThenReturnEndPosition(targetHtml, results, startWithWord, endWithWord);
  }
  return results;
}

function substringWithWord(targetHtml, startWithWord, endWithWord) {
  const startWithPagination = targetHtml.substring(targetHtml.search(startWithWord) + startWithWord.length);
  const substringHtml = startWithPagination.substring(0, startWithPagination.search(endWithWord));
  return substringHtml;
}

function substringWithWordThenReturnEndPosition(targetHtml, resultsArray, startWithWord, endWithWord) {
  const targetPosition = targetHtml.indexOf(startWithWord);
  if (targetPosition < 0) {
    return targetPosition;
  }
  const startPosition = targetPosition + startWithWord.length;
  const endPosition = targetHtml.indexOf(endWithWord, startPosition);
  resultsArray.push(targetHtml.substring(startPosition, endPosition));

  return endPosition;
}

function convertDateFormat(date) {
  const result = [];
  date.split(' ').forEach(function(item) {
    result.push(prefixZeroForOneDigit(item.substring(0, item.length - 1)));
  });
  return result.join('-');
}

function prefixZeroForOneDigit(digit) {

  if (parseInt(digit) < 10) {
    return '0' + digit;
  }
  return digit;
}

function saveOutputFile(filename, data) {
  fs.writeFileSync(filename, data, 'utf8');
}

function readOutputFile(filename) {
  const data = fs.readFileSync(filename);
  return JSON.parse(data.toString());
}

function loadUrlMap(posts) {
  const urlMap = new Map();
  posts.forEach(function (item) {
    urlMap.set(item.url, true);
  });
  return urlMap;
}

module.exports = {
  substringWithWord,
  substringWithWordThenReturnEndPosition,
  multiSubstringWithWord,
  substringAllWithWord,
  convertDateFormat,
  saveOutputFile,
  readOutputFile,
  loadUrlMap,
};
