{"size": 2302, "ext": "js", "max_stars_repo_path": "random-posts/fetch.js", "max_stars_repo_name": "MilanAryal/jekyll-snippets", "max_stars_repo_stars_event_min_datetime": "2020-12-04T18:54:56.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-04T18:54:56.000Z", "max_issues_repo_path": "random-posts/fetch.js", "max_issues_repo_name": "MilanAryal/jekyll-snippets", "max_issues_repo_issues_event_min_datetime": "2021-02-10T20:52:25.000Z", "max_issues_repo_issues_event_max_datetime": "2021-02-10T20:52:25.000Z", "max_forks_repo_path": "random-posts/fetch.js", "max_forks_repo_name": "MilanAryal/jekyll-snippets", "max_forks_repo_forks_event_min_datetime": "2017-12-17T22:38:35.000Z", "max_forks_repo_forks_event_max_datetime": "2020-10-15T18:41:07.000Z"}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": 2, "avg_line_length": 31.9722222222, "max_line_length": 117, "alphanum_fraction": 0.4304952215}
/**
 * ------------------------------------------------------------------------
 * Generate ramdom posts
 * ------------------------------------------------------------------------
 */

;(function ($, window, document, undefined) {
  'use strict';

  function generateRandomPosts () {
    fetch('/posts.json')
      .then(function(response) {
        return response.json();
      })
      .then(function(json) {

        var element = $('#random-posts');
        var contents = '';
        var numberOfPosts = 5;

        console.log('Loaded [posts.json] for random posts.');

        for (var count = 0; count < numberOfPosts; ++count) {
            // Generate an random index
            var index = Math.floor(Math.random() * json.length);
            // Get the post
            var post = json[index];
            // Random post metadata
            var url = post.url;
            var title = post.title;
            var excerpt = post.summary;
            var readTime = post.reading_time;
            var date = post.date_published;
            // Template
            var link = '<div class="random-posts-item clearfix">' +
                          '<a href="' + url + '">' +
                            '<h3 class="random-posts-item-title">' + title + '</h3>' +
                            '<p class="random-posts-item-excerpt">' + excerpt + '</p>' +
                          '</a>' +
                          '<div class="random-posts-item-meta">' + readTime + ' &middot; Posted ' + date + '</div>' +
                        '</div>';
            contents = contents + link;
            json.splice(index, 1);
        }
        element.html(contents);
      }).catch(function(error) {
        console.error(error);
      });
  }


  // Only run once the page Document Object Model (DOM)
  // is ready for JavaScript code to execute.
  // $(document).ready(function() { ... });
  $(function () { // BEGIN document ready function

    /**
     * ------------------------------------------------------------------------
     * Random posts init
     * ------------------------------------------------------------------------
     */

    $('.random-posts').removeAttr('hidden');

    // init
    generateRandomPosts();

  }); // END document ready function


})(jQuery, window, document);
