moment-recur
Advanced tools
Comparing version 1.0.0 to 1.0.4
{ | ||
"name": "moment-recur", | ||
"main": "moment-recur.js", | ||
"version": "1.0.0", | ||
"version": "1.0.4", | ||
"homepage": "https://github.com/c-trimm/moment-recur", | ||
@@ -17,4 +17,8 @@ "authors": [ | ||
"dependencies": { | ||
"moment": "~2.4.0" | ||
"moment": "< 3.0.0" | ||
}, | ||
"devDependencies": { | ||
"jasmine": "~1.3.0", | ||
"moment-timezone": "~0.3.0" | ||
}, | ||
"ignore": [ | ||
@@ -21,0 +25,0 @@ "**/.*", |
@@ -11,9 +11,9 @@ (function (root, factory) { | ||
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 | ||
@@ -30,3 +30,3 @@ var Interval = (function() { | ||
} | ||
return { | ||
@@ -37,8 +37,17 @@ measure: measure.toLowerCase(), | ||
} | ||
function matchInterval(type, units, start, date) { | ||
// Get the difference between the start date and the provded date, | ||
// using the required measure based on the type of rule | ||
var diff = start.diff(date, type, true); | ||
// 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 | ||
@@ -48,4 +57,4 @@ for (var unit in units) { | ||
unit = parseInt(unit, 10); | ||
// If the units devide evenly into the difference, we have a match | ||
// If the units divide evenly into the difference, we have a match | ||
if ((diff % unit) === 0) { | ||
@@ -56,6 +65,6 @@ return true; | ||
} | ||
return false; | ||
} | ||
return { | ||
@@ -66,3 +75,3 @@ create: createInterval, | ||
})(); | ||
// Calendar object for creating and matching calendar-based rules | ||
@@ -78,3 +87,3 @@ var Calendar = (function (){ | ||
}; | ||
// Dictionary of ranges based on measures | ||
@@ -88,3 +97,3 @@ var ranges = { | ||
}; | ||
// Private function for cehcking the range of calendar values | ||
@@ -98,3 +107,3 @@ function checkRange(low, high, list) { | ||
} | ||
// Private function to convert day and month names to numbers | ||
@@ -104,11 +113,11 @@ function namesToNumbers(list, nameType) { | ||
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); | ||
@@ -118,9 +127,9 @@ newList[unitNum] = list[unit]; | ||
} | ||
return newList; | ||
} | ||
function createCalendarRule(list, measure) { | ||
var keys = []; | ||
// Convert day/month names to numbers, if needed | ||
@@ -130,14 +139,14 @@ if (measure === "daysOfWeek") { | ||
} | ||
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, | ||
checkRange( ranges[measure].low, | ||
ranges[measure].high, | ||
keys ); | ||
return { | ||
@@ -148,10 +157,10 @@ measure: measure, | ||
} | ||
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 | ||
@@ -162,5 +171,15 @@ if ( list[unit] ) { | ||
// 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 { | ||
@@ -171,6 +190,6 @@ create: createCalendarRule, | ||
})(); | ||
// 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 | ||
@@ -188,3 +207,3 @@ var ruleTypes = { | ||
}; | ||
// a dictionary of plural and singular measures | ||
@@ -202,4 +221,4 @@ var measures = { | ||
}; | ||
///////////////////////////////// | ||
@@ -209,3 +228,3 @@ // Private Methods // | ||
///////////////////////////////// | ||
// Private method that tries to set a rule. | ||
@@ -215,7 +234,7 @@ function trigger() { | ||
var ruleType = ruleTypes[this.measure]; | ||
if (!(this instanceof Recur)) { | ||
throw Error("Private method trigger() was called directly or not called as instance of Recur!"); | ||
} | ||
// Make sure units and measure is defined and not null | ||
@@ -225,3 +244,3 @@ if ((typeof this.units === "undefined" || this.units === null) || !this.measure) { | ||
} | ||
// Error if we don't have a valid ruleType | ||
@@ -231,3 +250,3 @@ if (ruleType !== "calendar" && ruleType !== "interval") { | ||
} | ||
// Create the rule | ||
@@ -238,14 +257,14 @@ if (ruleType === "interval") { | ||
} | ||
rule = Interval.create(this.units, this.measure); | ||
} | ||
if (ruleType === "calendar") { | ||
rule = Calendar.create(this.units, this.measure); | ||
} | ||
// Remove the temporary rule data | ||
this.units = null; | ||
this.measure = null; | ||
// Remove existing rule based on measure | ||
@@ -257,26 +276,47 @@ for (var i = 0; i < this.rules.length; i++) { | ||
} | ||
this.rules.push(rule); | ||
return this; | ||
} | ||
// Private method to get next and previous occurances | ||
// Private method to get next, previous or all occurances | ||
function getOccurances(num, format, type) { | ||
var currentDate, date; | ||
var dates = []; | ||
if (!(this instanceof Recur)) { | ||
throw Error("Private method trigger() was called directly or not called as instance of Recur!"); | ||
} | ||
if ( !this.start && !this.from ) { | ||
throw Error("Cannot get occurances without start or from date."); | ||
} | ||
if ( type === "all" && !this.end ) { | ||
throw Error("Cannot get all occurances without an end date."); | ||
} | ||
if( !!this.end && (this.start > this.end) ) { | ||
throw Error("Start date cannot be later than end date."); | ||
} | ||
// Return empty set if the caller doesn't want any for next/prev | ||
if(type !== "all" && !(num > 0)) { | ||
return dates; | ||
} | ||
// Start from the from date, or the start date if from is not set. | ||
currentDate = (this.from || this.start).clone(); | ||
// Get the next N dates | ||
while(dates.length < num) { | ||
if (type === "next") { | ||
// Include the initial date to the results if wanting all dates | ||
if(type === "all") { | ||
if (this.matches(currentDate, false)) { | ||
date = format ? currentDate.format(format) : currentDate.clone(); | ||
dates.push(date); | ||
} | ||
} | ||
// Get the next N dates, if num is null then infinite | ||
while(dates.length < (num===null ? dates.length+1 : num)) { | ||
if (type === "next" || type === "all") { | ||
currentDate.add(1, "day"); | ||
@@ -287,19 +327,23 @@ } | ||
} | ||
console.log("Match: " + currentDate.format("L") + " - " + this.matches(currentDate, true)); | ||
if (this.matches(currentDate, true)) { | ||
//console.log("Match: " + currentDate.format("L") + " - " + this.matches(currentDate, true)); | ||
// Don't match outside the date if generating all dates within start/end | ||
if (this.matches(currentDate, (type==="all"?false:true))) { | ||
date = format ? currentDate.format(format) : currentDate.clone(); | ||
dates.push(date); | ||
} | ||
if(type === "all" && currentDate >= this.end) { | ||
break; | ||
} | ||
} | ||
return dates; | ||
} | ||
/////////////////////// | ||
// Private Functions // | ||
/////////////////////// | ||
// Private function to see if a date is within range of start/end | ||
@@ -311,8 +355,8 @@ function inRange(start, end, date) { | ||
} | ||
// Private function to turn units into objects | ||
function unitsToObject(units) { | ||
var list = {}; | ||
if ( toString.call(units) == '[object Array]' ) { | ||
if ( Object.prototype.toString.call(units) == '[object Array]' ) { | ||
units.forEach(function(v) { | ||
@@ -325,3 +369,3 @@ list[v] = true; | ||
} | ||
else if ( (toString.call(units) == '[object Number]') || (toString.call(units) == '[object String]') ) { | ||
else if ( (Object.prototype.toString.call(units) == '[object Number]') || (Object.prototype.toString.call(units) == '[object String]') ) { | ||
list[units] = true; | ||
@@ -332,6 +376,6 @@ } | ||
} | ||
return list; | ||
} | ||
// Private function to check if a date is an exception | ||
@@ -344,6 +388,5 @@ function isException(exceptions, date) { | ||
} | ||
return false; | ||
} | ||
// Private function to pluralize measure names for use with dictionaries. | ||
@@ -354,27 +397,27 @@ function pluralize(measure) { | ||
return "days"; | ||
case "week": | ||
return "weeks"; | ||
case "month": | ||
return "months"; | ||
case "year": | ||
return "years"; | ||
case "dayOfWeek": | ||
return "daysOfWeek"; | ||
case "dayOfMonth": | ||
return "daysOfMonth"; | ||
case "weekOfMonth": | ||
return "weeksOfMonth"; | ||
case "weekOfYear": | ||
return "weeksOfYear"; | ||
case "monthOfYear": | ||
return "monthsOfYear"; | ||
default: | ||
@@ -384,11 +427,11 @@ return measure; | ||
} | ||
// Private funtion to see if all rules matche | ||
function matchAllRules(rules, date, start) { | ||
var i, len, rule, type; | ||
for ( i = 0, len = rules.length; i < len; i++ ) { | ||
rule = rules[i]; | ||
type = ruleTypes[rule.measure]; | ||
if (type === "interval") { | ||
@@ -408,6 +451,6 @@ if ( !Interval.match(rule.measure, rule.units, start, date) ) { | ||
} | ||
return true; | ||
} | ||
// Private function to create measure functions | ||
@@ -420,8 +463,8 @@ function createMeasure(measure) { | ||
} | ||
////////////////////// | ||
// Public Functions // | ||
////////////////////// | ||
// Recur Object Constrcutor | ||
@@ -432,25 +475,29 @@ var Recur = function(options) { | ||
} | ||
if ( options.end ) { | ||
this.end = moment(options.end).dateOnly(); | ||
} | ||
// Our list of rules, all of which must match | ||
this.rules = options.rules || []; | ||
// Our list of exceptions. Match always fails on these dates. | ||
this.exceptions = options.exceptions || []; | ||
var exceptions = options.exceptions || []; | ||
this.exceptions = []; | ||
for(i = 0; i < exceptions.length; i++) { | ||
this.except(exceptions[i]); | ||
} | ||
// Temporary units integer, array, or object. Does not get imported/exported. | ||
this.units = null; | ||
// Tempoarary measure type. Does not get imported/exported. | ||
this.measure = null; | ||
// Tempoarary from date for next/previous. Does not get imported/exported. | ||
this.from = null; | ||
return this; | ||
}; | ||
// Get/Set start date | ||
@@ -462,3 +509,3 @@ Recur.prototype.startDate = function(date) { | ||
} | ||
if (date) { | ||
@@ -468,6 +515,6 @@ this.start = moment(date).dateOnly(); | ||
} | ||
return this.start; | ||
}; | ||
// Get/Set end date | ||
@@ -479,3 +526,3 @@ Recur.prototype.endDate = function(date) { | ||
} | ||
if (date) { | ||
@@ -485,6 +532,6 @@ this.end = moment(date).dateOnly(); | ||
} | ||
return this.end; | ||
}; | ||
// Get/Set a temporary from date | ||
@@ -496,3 +543,3 @@ Recur.prototype.fromDate = function(date) { | ||
} | ||
if (date) { | ||
@@ -502,18 +549,18 @@ this.from = moment(date).dateOnly(); | ||
} | ||
return this.from; | ||
}; | ||
// Export the settings, rules, and exceptions of this recurring date | ||
Recur.prototype.save = function() { | ||
var data = {}; | ||
if (this.start && moment(this.start).isValid()) { | ||
data.start = this.start.format("L"); | ||
} | ||
if (this.end && moment(this.end).isValid()) { | ||
data.end = this.end.format("L"); | ||
} | ||
data.exceptions = []; | ||
@@ -523,8 +570,8 @@ for (var i = 0, len = this.exceptions.length; i < len; i++) { | ||
} | ||
data.rules = this.rules; | ||
return data; | ||
}; | ||
// Return boolean value based on whether this date repeats (has rules or not) | ||
@@ -535,20 +582,20 @@ Recur.prototype.repeats = function() { | ||
} | ||
return false; | ||
}; | ||
// Set the units and, optionally, the measure | ||
Recur.prototype.every = function(units, measure) { | ||
if ((typeof units !== "undefined") && (units !== null)) { | ||
this.units = unitsToObject(units); | ||
} | ||
if ((typeof measure !== "undefined") && (measure !== null)) { | ||
this.measure = pluralize(measure); | ||
} | ||
return trigger.call(this); | ||
}; | ||
// Creates an exception date to prevent matches, even if rules match | ||
@@ -560,3 +607,3 @@ Recur.prototype.except = function(date) { | ||
}; | ||
// Forgets rules (by passing measure) and exceptions (by passing date) | ||
@@ -566,5 +613,6 @@ Recur.prototype.forget = function(dateOrRule) { | ||
var whatMoment = moment(dateOrRule); | ||
// If valid date, try to remove it from exceptions | ||
if (whatMoment.isValid()) { | ||
whatMoment = whatMoment.dateOnly(); // change to date only for perfect comparison | ||
for (i = 0, len = this.exceptions.length; i < len; i++) { | ||
@@ -576,6 +624,6 @@ if (whatMoment.isSame(this.exceptions[i])) { | ||
} | ||
return this; | ||
} | ||
// Otherwise, try to remove it from the rules | ||
@@ -588,7 +636,7 @@ for (i = 0, len = this.rules.length; i < len; i++) { | ||
}; | ||
// Attempts to match a date to the rules | ||
Recur.prototype.matches = function(dateToMatch, ignoreStartEnd) { | ||
var date = moment(dateToMatch).dateOnly(); | ||
if (!date.isValid()) { | ||
@@ -599,11 +647,11 @@ throw Error("Invalid date supplied to match method: " + dateToMatch); | ||
if (!ignoreStartEnd && !inRange(this.start, this.end, date)) { return false } | ||
if (isException(this.exceptions, date)) { return false; } | ||
if (!matchAllRules(this.rules, date, this.start)) { return false; } | ||
// if we passed everything above, then this date matches | ||
return true; | ||
}; | ||
// Get next N occurances | ||
@@ -613,3 +661,3 @@ Recur.prototype.next = function(num, format) { | ||
}; | ||
// Get previous N occurances | ||
@@ -619,3 +667,8 @@ Recur.prototype.previous = function(num, format) { | ||
}; | ||
// Get all occurances between start and end date | ||
Recur.prototype.all = function(format) { | ||
return getOccurances.call(this, null, format, "all"); | ||
}; | ||
// Create the measure functions (days(), months(), daysOfMonth(), monthsOfYear(), etc.) | ||
@@ -627,6 +680,6 @@ for (var measure in measures) { | ||
} | ||
return Recur; | ||
}()); | ||
// Recur can be created the following ways: | ||
@@ -639,6 +692,6 @@ // moment.recur() | ||
// If we have an object, use it as a set of options | ||
if ( start === Object(start) ) { | ||
if ( start === Object(start) && !moment.isMoment(start)) { | ||
return new Recur( start ); | ||
} | ||
// else, use the values passed | ||
@@ -656,3 +709,3 @@ return new Recur({ start: start, end: end }); | ||
// If we have an object, use it as a set of options | ||
if ( start === Object(start) ) { | ||
if ( start === Object(start) && !moment.isMoment(start)) { | ||
// if we have no start date, use the moment | ||
@@ -662,6 +715,6 @@ if ( typeof start.start === 'undefined' ) { | ||
} | ||
return new Recur( start ); | ||
} | ||
// if there is no end value, use the start value as the end | ||
@@ -672,3 +725,3 @@ if ( !end ) { | ||
} | ||
// use the moment for the start value | ||
@@ -678,6 +731,6 @@ if (!start) { | ||
} | ||
return new Recur({ start: start, end: end, moment: this }); | ||
}; | ||
// Plugin for calculating the week of the month of a date | ||
@@ -687,16 +740,20 @@ moment.fn.monthWeek = function() { | ||
var week0 = this.clone().startOf("month").startOf("week"); | ||
// First day of week | ||
var day0 = this.clone().startOf("week"); | ||
return day0.diff(week0, "weeks"); | ||
}; | ||
// Plugin for removing all time information from a given date | ||
moment.fn.dateOnly = function() { | ||
return this.hours(0).minutes(0).seconds(0).milliseconds(0); | ||
if (this.tz && typeof(moment.tz) == 'function' ) { | ||
return moment.tz(this.format('YYYY/MM/DD'), 'UTC'); | ||
} else { | ||
return this.hours(0).minutes(0).seconds(0).milliseconds(0).add('minute',this.utcOffset()).utcOffset(0); | ||
} | ||
}; | ||
return moment; | ||
})); | ||
})); |
{ | ||
"name": "moment-recur", | ||
"version": "1.0.0", | ||
"version": "1.0.4", | ||
"description": "A momentjs plugin for matching and generating recurring dates.", | ||
"main": "moment-recur.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
@@ -19,3 +16,17 @@ "type": "git", | ||
"author": "Casey Trimm", | ||
"license": "Unlicense" | ||
} | ||
"license": "Unlicense", | ||
"devDependencies": { | ||
"gulp": "~3.8.10", | ||
"karma": "~0.12.31", | ||
"karma-chrome-launcher": "~0.1.7", | ||
"run-sequence": "~1.0.2", | ||
"gulp-uglifyjs": "~0.5.0", | ||
"jasmine-core": "~2.1.3", | ||
"karma-jasmine": "~0.3.4" | ||
}, | ||
"scripts": { | ||
"test": "gulp test", | ||
"preinstall": "npm install bower gulp -g", | ||
"postinstall": "bower install" | ||
} | ||
} |
@@ -15,2 +15,4 @@ moment-recur | ||
Getting Started | ||
@@ -32,3 +34,3 @@ --------------- | ||
define(["moment", "moment-recur"], function(moment){ | ||
//you probably won't need a reference to mocur itself, so include it last. | ||
//you probably won't need a reference to moment-recur itself, so include it last. | ||
}); | ||
@@ -290,2 +292,11 @@ ``` | ||
With both a start date and an end date set, you can generate all dates within that range that match the pattern (including the start/end dates). | ||
```js | ||
var recurrence = moment().recur("01/01/2014", "01/07/2014").every(2).days(); | ||
// Outputs: ["01/01/2014", "01/03/2014", "01/05/2014", "01/07/2014"] | ||
allDates = recurrence.all("L"); | ||
``` | ||
**Important Note:** These functions may be very inefficient/slow. They work by attempting to match every date from the start of a range until the desired number of dates have been generated. So if you attempt to get 10 dates for a rule that matches once a year, it will run the match function for ~3650 days. | ||
@@ -292,0 +303,0 @@ |
@@ -15,7 +15,7 @@ 'use strict'; | ||
describe("Creating a recurring moment", function() { | ||
var nowMoment = moment(); | ||
var nowDate = nowMoment.format("L"); | ||
it("from moment constructor, with options parameter - moment.recur(options)", function() { | ||
@@ -26,3 +26,3 @@ var recur = moment.recur({ start:startDate, end:endDate }); | ||
}); | ||
it("from moment constructor, with start parameter only - moment.recur(start)", function() { | ||
@@ -32,3 +32,3 @@ var recur = moment.recur(startDate); | ||
}); | ||
it("from moment constructor, with start and end parameters - moment.recur(start, end)", function() { | ||
@@ -39,3 +39,3 @@ var recur = moment.recur(startDate, endDate); | ||
}); | ||
it("from moment function, with options parameter - moment().recur(options)", function() { | ||
@@ -46,3 +46,3 @@ var recur = moment().recur({ start:startDate, end:endDate }); | ||
}); | ||
it("from moment function, with start and end parameters - moment().recur(start, end)", function() { | ||
@@ -53,3 +53,3 @@ var recur = moment().recur(startDate, endDate); | ||
}); | ||
it("from moment function, with starting moment and end parameter - moment(start).recur(end)", function() { | ||
@@ -60,3 +60,3 @@ var recur = moment(startDate).recur(endDate); | ||
}); | ||
it("from moment function, starting now, with end parameter - moment().recur(end)", function() { | ||
@@ -67,3 +67,3 @@ var recur = nowMoment.recur(endDate); | ||
}); | ||
it("from moment function, starting now - moment().recur()", function() { | ||
@@ -73,2 +73,10 @@ var recur = nowMoment.recur(); | ||
}); | ||
it("from moment function, with starting moment and end parameter, which is a moment object - moment(start).recur(end)", function() { | ||
var startMoment = moment(startDate); | ||
var endMoment = moment(endDate); | ||
var recur = moment(startMoment).recur(endMoment); | ||
expect(recur.start.format("L")).toBe(startDate); | ||
expect(recur.end.format("L")).toBe(endDate); | ||
}); | ||
}); | ||
@@ -78,7 +86,7 @@ | ||
var recur; | ||
beforeEach(function() { | ||
recur = moment().recur(); | ||
}); | ||
it("'start' should be getable/setable with startDate()", function() { | ||
@@ -88,3 +96,3 @@ recur.startDate(startDate); | ||
}); | ||
it("'end' should be getable/setable with endDate()", function() { | ||
@@ -94,3 +102,3 @@ recur.endDate(endDate); | ||
}); | ||
it("'from' should be getable/setable with fromDate()", function() { | ||
@@ -107,3 +115,3 @@ recur.fromDate(startDate); | ||
}); | ||
it("should not create a rule when only a unit is passed", function() { | ||
@@ -113,3 +121,3 @@ var recurrence = moment().recur().every(1); | ||
}); | ||
it("should set the temporary units property", function() { | ||
@@ -119,3 +127,3 @@ var recurrence = moment().recur().every(1); | ||
}); | ||
it("should accept an array", function() { | ||
@@ -135,3 +143,3 @@ var recurrence = moment().recur().every([1, 2]); | ||
}); | ||
it("should not match a date after the end date", function() { | ||
@@ -144,3 +152,3 @@ var start = moment(startDate); | ||
}); | ||
it("can be daily", function() { | ||
@@ -151,9 +159,10 @@ var recurrence = moment(startDate).recur().every(2).days(); | ||
}); | ||
it("can be weekly", function() { | ||
var recurrence = moment(startDate).recur().every(2).weeks(); | ||
expect(recurrence.matches( moment(startDate).add(2, "weeks") )).toBe(true); | ||
expect(recurrence.matches( moment(startDate).add(2, "days") )).toBe(false); | ||
expect(recurrence.matches( moment(startDate).add(3, "weeks") )).toBe(false); | ||
}); | ||
it("can be monthly", function() { | ||
@@ -163,4 +172,5 @@ var recurrence = moment(startDate).recur().every(3).months(); | ||
expect(recurrence.matches( moment(startDate).add(2, "months") )).toBe(false); | ||
expect(recurrence.matches( moment(startDate).add(2, "days"))).toBe(false); | ||
}); | ||
it("can be yearly", function() { | ||
@@ -170,4 +180,5 @@ var recurrence = moment(startDate).recur().every(2).years(); | ||
expect(recurrence.matches( moment(startDate).add(3, "year") )).toBe(false); | ||
expect(recurrence.matches( moment(startDate).add(2, "days") )).toBe(false); | ||
}); | ||
it("can be an array of intervals", function() { | ||
@@ -177,3 +188,5 @@ var recurrence = moment(startDate).recur().every([3,5]).days(); | ||
expect(recurrence.matches( moment(startDate).add(5, "days"))).toBe(true); | ||
expect(recurrence.matches( moment(startDate).add(10, "days"))).toBe(true); | ||
expect(recurrence.matches( moment(startDate).add(4, "days"))).toBe(false); | ||
expect(recurrence.matches( moment(startDate).add(8, "days"))).toBe(false); | ||
}); | ||
@@ -183,16 +196,30 @@ }); | ||
describe("The Calendar Interval", function() { | ||
it("daysOfWeek should work", function() { | ||
var recurrence = moment.recur().every(["Sunday", 1]).daysOfWeek(); | ||
expect(recurrence.matches( moment().day("Sunday") )).toBe(true); | ||
expect(recurrence.matches( moment().day(1) )).toBe(true); | ||
expect(recurrence.matches( moment().day(3) )).toBe(false); | ||
}); | ||
describe("daysOfWeek", function() { | ||
it("should work", function () { | ||
var recurrence = moment.recur().every(["Sunday", 1]).daysOfWeek(); | ||
expect(recurrence.matches(moment().day("Sunday"))).toBe(true); | ||
expect(recurrence.matches(moment().day(1))).toBe(true); | ||
expect(recurrence.matches(moment().day(3))).toBe(false); | ||
}); | ||
it("should work with timezones", function () { | ||
var recurrence = moment.tz('2015-01-25',"America/Vancouver").recur().every(["Sunday", 1]).daysOfWeek(); | ||
var check = moment.tz('2015-02-01',"Asia/Hong_Kong"); | ||
expect(recurrence.matches(check)).toBe(true); | ||
}); | ||
}); | ||
it("daysOfMonth should work", function() { | ||
var recurrence = moment.recur().every([1, 10]).daysOfMonth(); | ||
expect(recurrence.matches( moment().date(1) )).toBe(true); | ||
expect(recurrence.matches( moment().date(10) )).toBe(true); | ||
expect(recurrence.matches( moment().date(15) )).toBe(false); | ||
var recurrence = moment('2015-01-01').recur().every([1, 10]).daysOfMonth(); | ||
expect(recurrence.matches( moment('2015-01-01'))).toBe(true); | ||
expect(recurrence.matches( moment('2015-01-02'))).toBe(false); | ||
expect(recurrence.matches( moment('2015-01-10'))).toBe(true); | ||
expect(recurrence.matches( moment('2015-01-15'))).toBe(false); | ||
expect(recurrence.matches( moment('2015-02-01'))).toBe(true); | ||
expect(recurrence.matches( moment('2015-02-02'))).toBe(false); | ||
expect(recurrence.matches( moment('2015-02-10'))).toBe(true); | ||
expect(recurrence.matches( moment('2015-02-15'))).toBe(false); | ||
}); | ||
it("weeksOfMonth should work", function() { | ||
@@ -203,4 +230,4 @@ var recurrence = moment.recur().every([1, 3]).weeksOfMonth(); | ||
expect(recurrence.matches( moment(startDate).date(27) )).toBe(false); | ||
}); | ||
}); | ||
it("weeksOfYear should work", function() { | ||
@@ -210,4 +237,4 @@ var recurrence = moment.recur().every(20).weekOfYear(); | ||
expect(recurrence.matches( moment(startDate) )).toBe(false); | ||
}); | ||
}); | ||
it("monthsOfYear should work", function() { | ||
@@ -217,4 +244,4 @@ var recurrence = moment.recur().every("January").monthsOfYear(); | ||
expect(recurrence.matches( moment().month("February") )).toBe(false); | ||
}); | ||
}); | ||
it("rules can be combined", function() { | ||
@@ -226,3 +253,3 @@ var valentines = moment.recur().every(14).daysOfMonth() | ||
}); | ||
it("can be passed units, without every()", function() { | ||
@@ -242,3 +269,3 @@ var recurrence = moment.recur().daysOfMonth([1,3]); | ||
}); | ||
it("should be forgettable", function() { | ||
@@ -255,5 +282,3 @@ var recurrence = moment("01/01/2014").recur().every(1).day(); | ||
recurrence = moment("01/01/2014").recur().every(2).days(); | ||
console.log(recurrence); | ||
nextDates = recurrence.next(3, "L"); | ||
console.log(nextDates); | ||
expect(nextDates.length).toBe(3); | ||
@@ -264,3 +289,3 @@ expect(nextDates[0]).toBe("01/03/2014"); | ||
}); | ||
it("can start from a temporary 'from' date", function() { | ||
@@ -290,11 +315,51 @@ var recurrence, nextDates; | ||
describe("All Dates", function() { | ||
it("can be generated", function() { | ||
var recurrence, allDates; | ||
recurrence = moment("01/01/2014").recur("01/07/2014").every(2).days(); | ||
allDates = recurrence.all("L"); | ||
expect(allDates.length).toBe(4); | ||
expect(allDates[0]).toBe("01/01/2014"); | ||
expect(allDates[1]).toBe("01/03/2014"); | ||
expect(allDates[2]).toBe("01/05/2014"); | ||
expect(allDates[3]).toBe("01/07/2014"); | ||
}); | ||
it("can start from a temporary 'from' date", function() { | ||
var recurrence, allDates; | ||
recurrence = moment().recur("01/01/2014", "01/08/2014").every(2).days(); | ||
recurrence.fromDate("01/05/2014"); | ||
allDates = recurrence.all("L"); | ||
expect(allDates.length).toBe(2); | ||
expect(allDates[0]).toBe("01/05/2014"); | ||
expect(allDates[1]).toBe("01/07/2014"); | ||
}); | ||
it('should throw error if start date is after end date', function() { | ||
var recurrence, allDates; | ||
recurrence = moment().recur("07/26/2017", "08/01/2013").every(2).days(); | ||
expect(function() { | ||
recurrence.all("L"); | ||
}).toThrow(new Error("Start date cannot be later than end date.")); | ||
}); | ||
it('should only generate a single date when start date and end date are the same', function() { | ||
var recurrence, allDates; | ||
recurrence = moment().recur("01/01/2014", "01/01/2014").every(1).days(); | ||
allDates = recurrence.all("L"); | ||
expect(allDates.length).toBe(1); | ||
expect(allDates[0]).toBe("01/01/2014"); | ||
}); | ||
}); | ||
describe("Exceptions", function() { | ||
var mo, exception, recur; | ||
var mo, exception, recur, exceptionWithTz; | ||
beforeEach(function() { | ||
mo = moment(startDate); | ||
exception = mo.clone().add(1, "day"); | ||
exception = mo.clone().add(3, "day"); | ||
recur = mo.clone().recur().every(1, "days"); | ||
exceptionWithTz = moment.tz(exception.format('YYYY-MM-DD'), 'Asia/Hong_Kong'); | ||
}); | ||
it("should prevent exception days from matching", function() { | ||
@@ -304,3 +369,8 @@ recur.except(exception); | ||
}); | ||
it('should work when the passed in exception is in a different time zone', function() { | ||
recur.except(exception); | ||
expect(recur.matches(exceptionWithTz)).toBe(false); | ||
}); | ||
it("should be removeable", function() { | ||
@@ -313,2 +383,25 @@ recur.except(exception); | ||
describe('Exceptions with weeks', function() { | ||
var mo, exception, recur, exceptionWithTz; | ||
beforeEach(function() { | ||
mo = moment(startDate); | ||
exception = mo.clone().add(7, "day"); | ||
recur = mo.clone().recur().every(1, "weeks"); | ||
exceptionWithTz = moment.tz(exception.format('YYYY-MM-DD'), 'Asia/Hong_Kong'); | ||
}); | ||
it('should not match on the exception day', function() { | ||
expect(recur.matches(exception)).toBe(true); | ||
recur.except(exception); | ||
expect(recur.matches(exception)).toBe(false); | ||
}); | ||
it('should not match on the exception day', function() { | ||
expect(recur.matches(exceptionWithTz)).toBe(true); | ||
recur.except(exception); | ||
expect(recur.matches(exceptionWithTz)).toBe(false); | ||
}); | ||
}); | ||
describe("Options", function() { | ||
@@ -324,3 +417,3 @@ it("should be importable", function() { | ||
}); | ||
expect(recurrence.startDate().format("L")).toBe("01/01/2014"); | ||
@@ -333,3 +426,3 @@ expect(recurrence.endDate().format("L")).toBe("12/31/2014"); | ||
}); | ||
it("shold be exportable", function() { | ||
@@ -351,3 +444,3 @@ var recurrence = moment("01/01/2014").recur("12/31/2014").every(2, "days").except("01/05/2014"); | ||
}); | ||
it("should return false when there are no rules set", function() { | ||
@@ -357,2 +450,2 @@ var recurrence = moment().recur(); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 2 instances in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
161750
15
3880
365
7
2
2
1