<commit_before>(function (root, factory) {
    if (typeof exports === 'object') {
        module.exports = factory(require('moment'));
    } else if (typeof define === 'function' && define.amd) {
        define('moment-recur', ['moment'], factory);
    } else {
        root.moment = factory(root.moment);
    }
}(this, function (moment) {
    var hasModule;

    hasModule = (typeof module !== "undefined" && module !== null) && (module.exports != null);

    if (typeof moment === 'undefined') {
        throw Error("Can't find moment");
    }

    // Interval object for creating and matching interval-based rules
    var Interval = (function() {
        function createInterval(units, measure) {
            // Make sure all of the units are integers greater than 0.
            for (var unit in units) {
                if (units.hasOwnProperty(unit)) {
                    if ( parseInt(unit, 10) <= 0 ) {
                        throw Error('Intervals must be greater than zero');
                    }
                }
            }

            return {
                measure: measure.toLowerCase(),
                units: units
            };
        }

        function matchInterval(type, units, start, date) {
            // Get the difference between the start date and the provided date,
            // using the required measure based on the type of rule'
            var diff = null;
            if( date.isBefore(start) ) {
                diff = start.diff(date, type, true);
            } else {
                diff = date.diff(start, type, true);
            }
            if( type == 'days') {
                // if we are dealing with days, we deal with whole days only.
                diff = parseInt(diff);
            }

            // Check to see if any of the units provided match the date
            for (var unit in units) {
                if (units.hasOwnProperty(unit)) {
                    unit = parseInt(unit, 10);

                    // If the units divide evenly into the difference, we have a match
                    if ((diff % unit) === 0) {
                        return true;
                    }
                }
            }

            return false;
        }

        return {
            create: createInterval,
            match: matchInterval
        };
    })();

    // Calendar object for creating and matching calendar-based rules
    var Calendar = (function (){
        // Dictionary of unit types based on measures
        var unitTypes = {
            "daysOfMonth": "date",
            "daysOfWeek": "day",
            "weeksOfMonth": "monthWeek",
            "weeksOfMonthByDay": "monthWeekByDay",
            "weeksOfYear": "weeks",
            "monthsOfYear": "months"
        };

        // Dictionary of ranges based on measures
        var ranges = {
            "daysOfMonth"       : { low: 1, high: 31 },
            "daysOfWeek"        : { low: 0, high: 6 },
            "weeksOfMonth"      : { low: 0, high: 4 },
            "weeksOfMonthByDay" : { low: 0, high: 4 },
            "weeksOfYear"       : { low: 0, high: 52 },
            "monthsOfYear"      : { low: 0, high: 11 }
        };

        // Private function for checking the range of calendar values
        function checkRange(low, high, list) {
            list.forEach(function(v) {
                if ( v < low || v > high ) {
                    throw Error('Value should be in range ' + low + ' to ' + high);
                }
            });
        }

        // Private function to convert day and month names to numbers
        function namesToNumbers(list, nameType) {
            var unit, unitInt, unitNum;
            var newList = {};

            for(unit in list) {
                if (list.hasOwnProperty(unit)) {
                    unitInt = parseInt(unit, 10);

                    if (isNaN(unitInt)) {
                        unitInt = unit;
                    }

                    unitNum = moment().set(nameType, unitInt).get(nameType);
                    newList[unitNum] = list[unit];
                }
            }

            return newList;
        }

        function createCalendarRule(list, measure) {
            var keys = [];

            // Convert day/month names to numbers, if needed
            if (measure === "daysOfWeek") {
                list = namesToNumbers(list, "days");
            }

            if (measure === "monthsOfYear") {
                list = namesToNumbers(list, "months");
            }

            for (var key in list) if (hasOwnProperty.call(list, key)) keys.push(key);

            // Make sure the listed units are in the measure's range
            checkRange( ranges[measure].low,
                        ranges[measure].high,
                        keys );

            return {
                measure: measure,
                units: list
            };
        }

        function matchCalendarRule(measure, list, date) {
            // Get the unit type (i.e. date, day, week, monthWeek, weeks, months)
            var unitType = unitTypes[measure];

            // Get the unit based on the required measure of the date
            var unit = date[unitType]();

            // If the unit is in our list, return true, else return false
            if ( list[unit] ) {
                return true;
            }

            // match on end of month days
            if ( unitType === 'date' && unit == date.add(1, 'months').date(0).format('D') && unit < 31) {
                while ( unit <= 31 ) {
                    if ( list[unit] ) {
                        return true;
                    }
                    unit++;
                }
            }

            return false;
        }

        return {
            create: createCalendarRule,
            match: matchCalendarRule
        };
    })();

    // The main Recur object to provide an interface for settings, rules, and matching
    var Recur = (function() {

        // A dictionary used to match rule measures to rule types
        var ruleTypes = {
            "days": "interval",
            "weeks": "interval",
            "months": "interval",
            "years": "interval",
            "daysOfWeek": "calendar",
            "daysOfMonth": "calendar",
            "weeksOfMonth": "calendar",
            "weeksOfMonthByDay": "calendar",
            "weeksOfYear": "calendar",
            "monthsOfYear": "calendar"
        };

        // a dictionary of plural and singular measures
        var measures = {
            "days": "day",
            "weeks": "week",
            "months": "month",
            "years": "year",
            "daysOfWeek": "dayOfWeek",
            "daysOfMonth": "dayOfMonth",
            "weeksOfMonth": "weekOfMonth",
            "weeksOfMonthByDay": "weekOfMonthByDay",
            "weeksOfYear": "weekOfYear",
            "monthsOfYear": "monthOfYear"
        };

        var recurPattern = {
            index: 0,
            pattern: null,
            rules: [],

            get measure() {
                return ruleTypes[this.rules[0].measure];
            },

            nextDate: function( workingDate ) {
                var day;

                if(this.measure == "daysOfMonth") {
                    if(this.index >= this.pattern.length) {
                        workingDate.add(1, "month");
                        this.index = 0;
                    }
                    day = this.pattern[this.index];

                    workingDate.date(day);

                } else if(this.measure == "daysOfWeek") {
                    // Sunday = 0, Saturday = 6
                    var diff = 0;
                    if(this.index >= this.pattern.length) {
                        diff += 7;  // Jump ahead by a week
                        this.index = 0;
                    }
                    daydiff += this.pattern[this.index];

                    workingDate.day( dayDiff );
                }

                this.index++;
            },

            rebuild: function( rules ) {
                this.reset();

                this.rules = rules;

                var days;
                var rule = rules[0];
                var list = rule.units;
                // var measure = ruleTypes[rule.measure];

                if (rules.length == 1 && this.measure == "daysOfMonth") {
                    // Populate with the days in the rule (e.g. 1, 5, 31)
                    days = Object.keys( rules );

                    // Set up the pattern
                    if(days && days.length > 0) {
                        this.pattern = days;
                    }

                } else if (rules.length == 1 && this.measure == "daysOfWeek" ) {
                    // Populate with the days of the week in the rule (e.g. 0, 1, 7)
                    days = Object.keys( rules );

                    // Set up the pattern
                    if(days && days.length > 0) {
                        this.pattern = days;
                    }<commit_msg>Adjustments to support interval rule w/ daysOfWeek<commit_after>(function (root, factory) {
    if (typeof exports === 'object') {
        module.exports = factory(require('moment'));
    } else if (typeof define === 'function' && define.amd) {
        define('moment-recur', ['moment'], factory);
    } else {
        root.moment = factory(root.moment);
    }
}(this, function (moment) {
    var hasModule;

    hasModule = (typeof module !== "undefined" && module !== null) && (module.exports != null);

    if (typeof moment === 'undefined') {
        throw Error("Can't find moment");
    }

    // Interval object for creating and matching interval-based rules
    var Interval = (function() {
        function createInterval(units, measure) {
            // Make sure all of the units are integers greater than 0.
            for (var unit in units) {
                if (units.hasOwnProperty(unit)) {
                    if ( parseInt(unit, 10) <= 0 ) {
                        throw Error('Intervals must be greater than zero');
                    }
                }
            }

            return {
                measure: measure.toLowerCase(),
                units: units
            };
        }

        function matchInterval(type, units, start, date) {
            // Get the difference between the start date and the provided date,
            // using the required measure based on the type of rule'
            var diff = null;
            if( date.isBefore(start) ) {
                diff = start.diff(date, type, true);
            } else {
                diff = date.diff(start, type, true);
            }
            if( type == 'days') {
                // if we are dealing with days, we deal with whole days only.
                diff = parseInt(diff);
            }

            // Check to see if any of the units provided match the date
            for (var unit in units) {
                if (units.hasOwnProperty(unit)) {
                    unit = parseInt(unit, 10);

                    // If the units divide evenly into the difference, we have a match
                    if ((diff % unit) === 0) {
                        return true;
                    }
                }
            }

            return false;
        }

        return {
            create: createInterval,
            match: matchInterval
        };
    })();

    // Calendar object for creating and matching calendar-based rules
    var Calendar = (function (){
        // Dictionary of unit types based on measures
        var unitTypes = {
            "daysOfMonth": "date",
            "daysOfWeek": "day",
            "weeksOfMonth": "monthWeek",
            "weeksOfMonthByDay": "monthWeekByDay",
            "weeksOfYear": "weeks",
            "monthsOfYear": "months"
        };

        // Dictionary of ranges based on measures
        var ranges = {
            "daysOfMonth"       : { low: 1, high: 31 },
            "daysOfWeek"        : { low: 0, high: 6 },
            "weeksOfMonth"      : { low: 0, high: 4 },
            "weeksOfMonthByDay" : { low: 0, high: 4 },
            "weeksOfYear"       : { low: 0, high: 52 },
            "monthsOfYear"      : { low: 0, high: 11 }
        };

        // Private function for checking the range of calendar values
        function checkRange(low, high, list) {
            list.forEach(function(v) {
                if ( v < low || v > high ) {
                    throw Error('Value should be in range ' + low + ' to ' + high);
                }
            });
        }

        // Private function to convert day and month names to numbers
        function namesToNumbers(list, nameType) {
            var unit, unitInt, unitNum;
            var newList = {};

            for(unit in list) {
                if (list.hasOwnProperty(unit)) {
                    unitInt = parseInt(unit, 10);

                    if (isNaN(unitInt)) {
                        unitInt = unit;
                    }

                    unitNum = moment().set(nameType, unitInt).get(nameType);
                    newList[unitNum] = list[unit];
                }
            }

            return newList;
        }

        function createCalendarRule(list, measure) {
            var keys = [];

            // Convert day/month names to numbers, if needed
            if (measure === "daysOfWeek") {
                list = namesToNumbers(list, "days");
            }

            if (measure === "monthsOfYear") {
                list = namesToNumbers(list, "months");
            }

            for (var key in list) if (hasOwnProperty.call(list, key)) keys.push(key);

            // Make sure the listed units are in the measure's range
            checkRange( ranges[measure].low,
                        ranges[measure].high,
                        keys );

            return {
                measure: measure,
                units: list
            };
        }

        function matchCalendarRule(measure, list, date) {
            // Get the unit type (i.e. date, day, week, monthWeek, weeks, months)
            var unitType = unitTypes[measure];

            // Get the unit based on the required measure of the date
            var unit = date[unitType]();

            // If the unit is in our list, return true, else return false
            if ( list[unit] ) {
                return true;
            }

            // match on end of month days
            if ( unitType === 'date' && unit == date.add(1, 'months').date(0).format('D') && unit < 31) {
                while ( unit <= 31 ) {
                    if ( list[unit] ) {
                        return true;
                    }
                    unit++;
                }
            }

            return false;
        }

        return {
            create: createCalendarRule,
            match: matchCalendarRule
        };
    })();

    // The main Recur object to provide an interface for settings, rules, and matching
    var Recur = (function() {

        // A dictionary used to match rule measures to rule types
        var ruleTypes = {
            "days": "interval",
            "weeks": "interval",
            "months": "interval",
            "years": "interval",
            "daysOfWeek": "calendar",
            "daysOfMonth": "calendar",
            "weeksOfMonth": "calendar",
            "weeksOfMonthByDay": "calendar",
            "weeksOfYear": "calendar",
            "monthsOfYear": "calendar"
        };

        // a dictionary of plural and singular measures
        var measures = {
            "days": "day",
            "weeks": "week",
            "months": "month",
            "years": "year",
            "daysOfWeek": "dayOfWeek",
            "daysOfMonth": "dayOfMonth",
            "weeksOfMonth": "weekOfMonth",
            "weeksOfMonthByDay": "weekOfMonthByDay",
            "weeksOfYear": "weekOfYear",
            "monthsOfYear": "monthOfYear"
        };

        var recurPattern = {
            index: 0,
            pattern: null,
            _interval: null,
            rules: [],

            set interval( rule ) {
                var keys = Object.keys( rule.units );
                this._interval = {
                    measure: rule.measure,
                    unit: keys[0]
                };
            },

            get interval() {
                return this._interval;
            },

            get measure() {
                return ruleTypes[this.rules[0].measure];
            },

            nextDate: function( workingDate ) {
                var day;

                if(this.measure == "daysOfMonth") {
                    if(this.index >= this.pattern.length) {
                        workingDate.add(1, "month");
                        this.index = 0;
                    }
                    day = this.pattern[this.index];

                    workingDate.date(day);

                } else if(this.measure == "daysOfWeek") {
                    // Sunday = 0, Saturday = 6
                    if(this.index >= this.pattern.length) {
                        workingDate.add( this.interval.unit, this.interval.measure);
                        this.index = 0;
                    }
                    day = this.pattern[this.index];

                    workingDate.day( day );
                }

                this.index++;
            },

            rebuild: function( rules ) {
                this.reset();

                this.rules = rules;

                var days;
                var rule = rules[0];
                var list = rule.units;
                // var measure = ruleTypes[rule.measure];

                if (rules.length == 1 && this.measure == "daysOfMonth") {
                    // Populate with the days in the rule (e.g. 1, 5, 31)
                    days = Object.keys( rules );

                    // Set up the pattern
                    if(days && days.length > 0) {
                        this.pattern = days;
                    }

                } else if ((rules.length == 1 || rules.length == 2) && this.measure == "daysOfWeek" ) {
                    // Populate with the days of the week in the rule (e.g. 0, 1, 7)
                    days = Object.keys( rules );

                    // Set up the pattern
                    if(days && days.length > 0) {
                        this.pattern = days;
                    }
                    if(rules.length == 1) {
                        this.interval = {
                            units: { 1: true },
                            measure: "weeks"
                        };
                    } else {
                        this.interval = rules[1];
                    }
{'fraction_non_alphanumeric': 0.108577227088229, 'fraction_numerical': 0.005607776116214484, 'mean_word_length': 1.180114099429503, 'pattern_counts': {'":': 52, '<': 11, '<?xml version=': 0, '>': 13, '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': '2044998', 'n_tokens_mistral': 4877, 'n_tokens_neox': 4459, 'n_words': 1362}