{"size": 2632, "ext": "js", "max_stars_repo_path": "tasks/lib/Comparator.js", "max_stars_repo_name": "pixel-shock/grunt-npm-check-updates", "max_stars_repo_stars_event_min_datetime": "2018-05-14T07:16:53.000Z", "max_stars_repo_stars_event_max_datetime": "2018-05-14T07:16:53.000Z", "max_issues_repo_path": "tasks/lib/Comparator.js", "max_issues_repo_name": "pixel-shock/grunt-npm-check-updates", "max_issues_repo_issues_event_min_datetime": "2018-07-02T20:54:02.000Z", "max_issues_repo_issues_event_max_datetime": "2018-09-03T11:45:03.000Z", "max_forks_repo_path": "tasks/lib/Comparator.js", "max_forks_repo_name": "pixel-shock/grunt-npm-check-updates", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 37.0704225352, "max_line_length": 92, "alphanum_fraction": 0.5262158055}
'use strict';

const _ = require('lodash');
const semver = require('semver');

function Comperator() {
    if (!(this instanceof Comperator)) {
        return new Comperator();
    }
}

Comperator.prototype = {
    compare: (moduleName, installedVersion, latestVersion, versions) => {
        /* eslint-disable */
        let result = {
            moduleName: moduleName,
            installedVersion: installedVersion,
            latestVersion: latestVersion,
            missedMajors: 0,
            missedMinors: 0,
            missedPatches: 0,
            versions: versions,
        };
        /* eslint-enable */
        const installedVersionIndex = _.indexOf(versions, installedVersion);
        const latestVersionIndex = _.indexOf(versions, latestVersion);
        let lastMajor = -1;
        let lastMinor = -1;

        if (installedVersionIndex !== -1 && latestVersionIndex !== -1) {
            for (let i = installedVersionIndex; i <= latestVersionIndex; i += 1) {
                const validAvailable = semver.valid(versions[i]);
                const validInstalled = semver.valid(installedVersion);
                const isStableRelease = /^[0-9.]+$/.test(validInstalled);

                if (validInstalled !== null && validAvailable !== null && isStableRelease) {
                    const majorInstalled = semver.major(validInstalled);
                    const minorInstalled = semver.minor(validInstalled);
                    const patchInstalled = semver.patch(validInstalled);
                    const majorAvailable = semver.major(validAvailable);
                    const minorAvailable = semver.minor(validAvailable);
                    const patchAvailable = semver.patch(validAvailable);

                    if (majorInstalled === majorAvailable
                        && minorInstalled === minorAvailable
                        && patchInstalled !== patchAvailable) {
                        result.missedPatches += 1;
                    }

                    if (majorInstalled === majorAvailable
                        && minorInstalled !== minorAvailable
                        && lastMinor !== minorAvailable) {
                        result.missedMinors += 1;
                        lastMinor = minorAvailable;
                    }

                    if (majorInstalled < majorAvailable
                        && lastMajor !== majorAvailable) {
                        result.missedMajors += 1;
                        lastMajor = majorAvailable;
                    }
                }
            }
        }

        return result;
    },
};

module.exports = Comperator;
