Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement iterable/valueOf for decoupling from libraries #203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ CronDate.prototype.toDate = function() {
return this._date.toDate();
};

CronDate.prototype.valueOf = function() {
return this._date.valueOf();
}

CronDate.prototype.isLastDayOfMonth = function() {
var newDate = this._date.clone();
//next day
Expand Down
21 changes: 21 additions & 0 deletions lib/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,27 @@ CronExpression.prototype._findSchedule = function _findSchedule (reverse) {
return currentDate;
};

if (Symbol && Symbol.iterator) {
/**
* ES6 iterable
*
* @public
* @return {Object}
*/
CronExpression.prototype[Symbol.iterator] = function() {
var self = this
return {
next: function() {
var schedule = self._findSchedule();
return {
value: schedule,
done: !self.hasNext()
}
}
};
}
}

/**
* Find next suitable date
*
Expand Down
5 changes: 4 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ declare class CronDate {
getTime(): number
toString(): string
toDate(): Date
valueOf(): number
}

interface ParserOptions {
Expand All @@ -58,7 +59,7 @@ interface ParserOptions {
tz?: string
}

declare class CronExpression {
declare class CronExpression implements Iterable<CronDate> {
constructor(fields: {}, options: {})

/** Find next suitable date */
Expand All @@ -79,6 +80,8 @@ declare class CronExpression {

/** Parse input expression (async) */
parse(expression: string, options?: ParserOptions, callback?: () => any): CronExpression

[Symbol.iterator](): Iterator<CronDate>
}

interface StringResult {
Expand Down
22 changes: 22 additions & 0 deletions test/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,28 @@ test('should work with both dayOfMonth and nth occurence of dayOfWeek', function
t.end();
});

test('should be iterable', function(t) {
var options = {
currentDate: new CronDate('Wed, 26 Dec 2012 14:38:53'),
startDate: new CronDate('Wed, 26 Dec 2012 12:40:00'),
endDate: new CronDate('Wed, 26 Dec 2012 15:40:00')
};

var interval = CronExpression.parse('*/20 * * * *', options);
t.ok(interval, 'Interval parsed');

var iterated = [...interval].map((date) => date.toJSON());
var expected = [
"2012-12-26T14:40:00.000Z",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer single quotes over double quotes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep fair enough. I'm just used to eslint formatting on save :)

"2012-12-26T15:00:00.000Z",
"2012-12-26T15:20:00.000Z",
];

t.deepEqual(expected, iterated);

t.end();
})

test('should error when passed invalid occurence value', function(t) {
var expressions = [
'0 0 0 ? * 1#',
Expand Down