<commit_before>/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets */

/**
* Link File CDNManager
*/
define(function CdnManager(require, exports, module) {
    'use strict';

    var API_URL     = "https://data.jsdelivr.com/v1/",
        OLD_API_URL = "https://api.jsdelivr.com/v1/",
        CDN_URL     = "https://cdn.jsdelivr.net/";

    var _currentLibs = [],
        _currentPage;

    function _fetchApiPage(pageNamber) {
        var deferred = new $.Deferred(),
            requestURL = API_URL + "stats/packages?page=" + pageNamber;

        $.getJSON(requestURL).done(function (libs) {
            if (libs.length > 0) {
                deferred.resolve(libs);
            } else {
                deferred.reject();
            }
        }).fail(function () {
            deferred.reject();
        });
        return deferred.promise();
    }

    function fetchPage(pageNumber) {
        var deferred = new $.Deferred(),
            targetPages = [];

        // Reset libs
        _currentLibs = [];

        // A page in this manager contains 5 api pages
        pageNumber = pageNumber * 1;
        if (pageNumber < 1) deferred.resolve();
        var endPage = pageNumber * 5,
            startPage = endPage - 4;

        for (var page = startPage; page <= endPage; page++) {
            targetPages.push(page);
        }
        _fetchApiPage(targetPages[0]).done(function (libs) {
            _currentLibs = _currentLibs.concat(libs);
            _fetchApiPage(targetPages[1]).done(function (libs) {
                _currentLibs = _currentLibs.concat(libs);
                _fetchApiPage(targetPages[2]).done(function (libs) {
                    _currentLibs = _currentLibs.concat(libs);
                    _fetchApiPage(targetPages[3]).done(function (libs) {
                        _currentLibs = _currentLibs.concat(libs);
                        _fetchApiPage(targetPages[4]).done(function (libs) {
                            _currentLibs = _currentLibs.concat(libs);
                            _currentPage = pageNumber;
                            deferred.resolve();
                        }).fail(function () {
                            deferred.reject();
                        });
                    }).fail(function () {
                        deferred.reject();
                    });
                }).fail(function () {
                    deferred.reject();
                });
            }).fail(function () {
                deferred.reject();
            });
        }).fail(function () {
            deferred.reject();
        });
        return deferred.promise();
    }

    function fetchNextPage() {
        var deferred = new $.Deferred();

        fetchPage(_currentPage + 1).done(function () {
            deferred.resolve();
        }).fail(function () {
            deferred.reject();
        });
        return deferred.promise();
    }

    function fetchPreviousPage() {
        var deferred = new $.Deferred();

        if (_currentPage - 1 > 0) {
            fetchPage(_currentPage - 1).done(function () {
                deferred.resolve();
            }).fail(function () {
                deferred.reject();
            });
        } else {
            deferred.reject();
        }
        return deferred.promise();
    }<commit_msg>cdnManager: fetchPage() refactoring.<commit_after>/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets */

/**
* Link File CDNManager
*/
define(function CdnManager(require, exports, module) {
    'use strict';

    var API_URL     = "https://data.jsdelivr.com/v1/",
        OLD_API_URL = "https://api.jsdelivr.com/v1/",
        CDN_URL     = "https://cdn.jsdelivr.net/";

    var _currentLibs = [],
        _currentPage;

    function _fetchApiPage(pageNamber) {
        var deferred = new $.Deferred(),
            requestURL = API_URL + "stats/packages?page=" + pageNamber;

        $.getJSON(requestURL).done(function (libs) {
            if (libs.length > 0) {
                deferred.resolve(libs);
            } else {
                deferred.reject();
            }
        }).fail(function () {
            deferred.reject();
        });
        return deferred.promise();
    }

    function fetchPage(pageNumber) {
        var deferred = new $.Deferred(),
            startApiPage;

        // Reset libs
        _currentLibs = [];

        // A page in this manager contains 5 api pages
        pageNumber = pageNumber * 1;
        if (pageNumber < 1) deferred.resolve();
        startApiPage = (pageNumber * 5) - 4;

        _fetchApiPage(startApiPage).then(function (libs) {
            _currentLibs = _currentLibs.concat(libs);
            return _fetchApiPage(startApiPage + 1);
        }).then(function (libs) {
            _currentLibs = _currentLibs.concat(libs);
            return _fetchApiPage(startApiPage + 2);
        }).then(function (libs) {
            _currentLibs = _currentLibs.concat(libs);
            return _fetchApiPage(startApiPage + 3);
        }).then(function (libs) {
            _currentLibs = _currentLibs.concat(libs);
            return _fetchApiPage(startApiPage + 4);
        }).then(function (libs) {
            _currentLibs = _currentLibs.concat(libs);
            _currentPage = pageNumber;
            deferred.resolve();
        }).fail(function () {
            deferred.reject();
        });

        return deferred.promise();
    }

    function fetchNextPage() {
        var deferred = new $.Deferred();

        fetchPage(_currentPage + 1).done(function () {
            deferred.resolve();
        }).fail(function () {
            deferred.reject();
        });
        return deferred.promise();
    }

    function fetchPreviousPage() {
        var deferred = new $.Deferred();

        if (_currentPage - 1 > 0) {
            fetchPage(_currentPage - 1).done(function () {
                deferred.resolve();
            }).fail(function () {
                deferred.reject();
            });
        } else {
            deferred.reject();
        }
        return deferred.promise();
    }
{'fraction_non_alphanumeric': 0.13002896684905052, 'fraction_numerical': 0.006276150627615063, 'mean_word_length': 1.6401869158878504, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 6, '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': '5748649', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1676, 'n_words': 329}