<commit_before>// - [ ] Make a request to USGS
const requestEarthquakeData = function*(){
  const quakeEndpoint = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson';
  while(true){
    yield fetch(quakeEndpoint, {method: 'get'}).then(function(d){
      return d.json();
    });
  }
};
const refreshQuakeData = function() {
  return requestEarthquakeData().next().value.then(data => data);
};

// - [ ] Parse request
const parseQuakeData = function(data) {
  const entriesPerPage = 20;
  const quakeEntries = data['features'];
  const totalEntries = quakeEntries.length;
  const currPage = window.location.hash.split('#')[1] || 1;
  const startEntryIndex = currPage * entriesPerPage;
  const endEntryIndex = startEntryIndex + entriesPerPage;

  let quakes = '';
  for (let i = startEntryIndex; i < endEntryIndex; i++) {
    let quake = quakeEntries[i];

    if (quake === undefined) {
      break;
    }

    let props = quake['properties'];
    let geo = quake['geometry'];
    let date = computeDate(props['time']);
    let time = computeTime(props['time']);
    quakes += `<div class="quakeEntry">
      ${date} - ${props['title']}
      <div id="${quake['id']}" class="quakeDetails">
        <ul>
          <li>id: <a href="${props['url']}">${quake['id']}</a></li>
          <li>place: ${props['place']}</li>
          <li>time: ${time}</li>
          <li>magnitude: ${props['mag']}</li>
          <li>type: ${props['type']}</li>
          <li>coordinates: ${geo['coordinates']}</li>
        </ul>
      </div>
    </div><br/>`;
  }
  return quakes;
};

const buildAppNav = function() {
  let nav = '<a href="#refresh" class="button refresh">Refresh</a> ';
  nav += '<a href="#share" class="button share">Share</a> ';
  // nav += '<a href="#change" class="button change">Change Days</a>';
  return nav;
};

const buildPageLinks = function(totalEntries) {
  totalEntries = totalEntries || quakeData['features'].length;

  const entriesPerPage = 20;
  const currPage = parseInt(window.location.hash.split('#')[1]) || 1;
  const totalPages = Math.round(totalEntries / entriesPerPage);

  let links = `<a href="#1" class="firstPage"><<</a> `;
  links += `<a href="#${computeBackPage(currPage)}" class="backPage"><</a> `;
  links += buildPageLinksWithContext(currPage, totalPages);
  links += ` <a href="#${computeNextPage(currPage, totalPages)}" class="nextPage">></a>`;
  links += ` <a href="#${totalPages}" class="lastPage">>></a>`;

  return links;
};

const buildPageLinksWithContext = function(currPage, totalPages) {
  // Validate params
  currPage = currPage != '' && parseInt(currPage) || 1;

  const maxPageLinks = 9;
  const links = new Array(maxPageLinks);
  const startNum = computePageLinkStartNum(currPage, totalPages, maxPageLinks);
  const lastLinkNum = startNum + maxPageLinks;

  for (let i = startNum; i < lastLinkNum; i++) {
    const linkNum = i + 1;
    links[i] = `<a href="#${linkNum}" class="pageLink">${linkNum}</a> | `;
  }
  links[currPage - 1] = `<span class="currPage">${currPage}</span> | `;
  links[lastLinkNum - 1] = links[lastLinkNum - 1].split('|')[0];
  return links.join('');
};

const computePageLinkStartNum = function(currPage, totalPages, maxPageLinks) {
  const middleLinkIndex = Math.round(maxPageLinks / 2);
  if (currPage >= totalPages - middleLinkIndex) {
    return totalPages - 9;
  }
  else if (currPage >= middleLinkIndex) {
    return currPage - 5;
  }
  return 0;
};

const computeDate = function(timestamp) {
  return new Date(timestamp).toLocaleDateString();
};

const computeTime = function(timestamp) {
  return new Date(timestamp).toLocaleTimeString();
};

const computeNextPage = function(currPage, totalPages) {
  if (currPage + 1 < totalPages) {
    return currPage + 1;
  }
  return totalPages;
};

const computeBackPage = function(currPage) {<commit_msg>Fix off-by-one pagination UI bug<commit_after>// - [ ] Make a request to USGS
const requestEarthquakeData = function*(){
  const quakeEndpoint = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson';
  while(true){
    yield fetch(quakeEndpoint, {method: 'get'}).then(function(d){
      return d.json();
    });
  }
};
const refreshQuakeData = function() {
  return requestEarthquakeData().next().value.then(data => data);
};

// - [ ] Parse request
const parseQuakeData = function(data) {
  const entriesPerPage = 20;
  const quakeEntries = data['features'];
  const totalEntries = quakeEntries.length;
  const currPage = window.location.hash.split('#')[1] || 1;
  const startEntryIndex = currPage * entriesPerPage;
  const endEntryIndex = startEntryIndex + entriesPerPage;

  let quakes = '';
  for (let i = startEntryIndex; i < endEntryIndex; i++) {
    let quake = quakeEntries[i];

    if (quake === undefined) {
      break;
    }

    let props = quake['properties'];
    let geo = quake['geometry'];
    let date = computeDate(props['time']);
    let time = computeTime(props['time']);
    quakes += `<div class="quakeEntry">
      ${date} - ${props['title']}
      <div id="${quake['id']}" class="quakeDetails">
        <ul>
          <li>id: <a href="${props['url']}">${quake['id']}</a></li>
          <li>place: ${props['place']}</li>
          <li>time: ${time}</li>
          <li>magnitude: ${props['mag']}</li>
          <li>type: ${props['type']}</li>
          <li>coordinates: ${geo['coordinates']}</li>
        </ul>
      </div>
    </div><br/>`;
  }
  return quakes;
};

const buildAppNav = function() {
  let nav = '<a href="#refresh" class="button refresh">Refresh</a> ';
  nav += '<a href="#share" class="button share">Share</a> ';
  // nav += '<a href="#change" class="button change">Change Days</a>';
  return nav;
};

const buildPageLinks = function(totalEntries) {
  totalEntries = totalEntries || quakeData['features'].length;

  const entriesPerPage = 20;
  const currPage = parseInt(window.location.hash.split('#')[1]) || 1;
  const totalPages = Math.round(totalEntries / entriesPerPage);

  let links = `<a href="#1" class="firstPage"><<</a> `;
  links += `<a href="#${computeBackPage(currPage)}" class="backPage"><</a> `;
  links += buildPageLinksWithContext(currPage, totalPages);
  links += ` <a href="#${computeNextPage(currPage, totalPages)}" class="nextPage">></a>`;
  links += ` <a href="#${totalPages}" class="lastPage">>></a>`;

  return links;
};

const buildPageLinksWithContext = function(currPage, totalPages) {
  // Validate params
  currPage = currPage != '' && parseInt(currPage) || 1;

  const maxPageLinks = 9;
  const links = new Array(maxPageLinks);
  const startNum = computePageLinkStartNum(currPage, totalPages, maxPageLinks);
  const lastLinkNum = startNum + maxPageLinks;

  for (let i = startNum; i < lastLinkNum; i++) {
    const linkNum = i + 1;
    links[i] = `<a href="#${linkNum}" class="pageLink">${linkNum}</a> | `;
  }
  links[currPage - 1] = `<span class="currPage">${currPage}</span> | `;
  links[lastLinkNum - 1] = links[lastLinkNum - 1].split('|')[0];
  return links.join('');
};

const computePageLinkStartNum = function(currPage, totalPages, maxPageLinks) {
  const middleLinkIndex = Math.round(maxPageLinks / 2);
  if (currPage >= totalPages - middleLinkIndex + 1) {
    return totalPages - 9;
  }
  else if (currPage >= middleLinkIndex) {
    return currPage - 5;
  }
  return 0;
};

const computeDate = function(timestamp) {
  return new Date(timestamp).toLocaleDateString();
};

const computeTime = function(timestamp) {
  return new Date(timestamp).toLocaleTimeString();
};

const computeNextPage = function(currPage, totalPages) {
  if (currPage + 1 < totalPages) {
    return currPage + 1;
  }
  return totalPages;
};

const computeBackPage = function(currPage) {
{'fraction_non_alphanumeric': 0.1800129785853342, 'fraction_numerical': 0.0063595068137573, 'mean_word_length': 4.046496398166339, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '24768', 'n_tokens_mistral': 2569, 'n_tokens_neox': 2517, 'n_words': 549}