Skip to content

Commit

Permalink
Remove RRule support (ampproject#25230)
Browse files Browse the repository at this point in the history
It doesn't work.

Fixes ampproject#24297
  • Loading branch information
jridgewell authored Oct 24, 2019
1 parent d8df5df commit 11e9de2
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 134 deletions.
1 change: 0 additions & 1 deletion build-system/compile/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const COMMON_GLOBS = [
'third_party/webcomponentsjs/ShadowCSS.js',
'node_modules/dompurify/dist/purify.es.js',
'node_modules/promise-pjs/promise.mjs',
'node_modules/rrule/dist/esm/src/index.js',
'node_modules/web-animations-js/web-animations.install.js',
'node_modules/web-activities/activity-ports.js',
'node_modules/@ampproject/animations/dist/animations.mjs',
Expand Down
13 changes: 0 additions & 13 deletions build-system/externs/amp.extern.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,6 @@ window.vg;
* @type {function(*)}
*/
let ReactRender = function() {};
let RRule;
/**
* @param {Date} unusedDt
* @param {boolean} unusedInc
* @return {?Date}
*/
RRule.prototype.before = function(unusedDt, unusedInc) {};
/**
* @param {Date} unusedDt
* @param {boolean} unusedInc
* @return {?Date}
*/
RRule.prototype.after = function(unusedDt, unusedInc) {};

/**
* @dict
Expand Down
81 changes: 5 additions & 76 deletions extensions/amp-date-picker/0.1/dates-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
*/

import {requireExternal} from '../../../src/module';
import {rrulestr} from 'rrule/dist/esm/src/index.js';

/** @enum {string} */
const DateType = {
INVALID: 'invalid',
RRULE: 'rrule',
DATE: 'date',
};

/**
* A class which wraps a list of moment or RRULE dates.
* A class which wraps a list of moment dates.
*/
export class DatesList {
/**
Expand All @@ -40,11 +38,6 @@ export class DatesList {
/** @private @const */
this.moment_ = requireExternal('moment');

/** @private @const */
this.rrulestrs_ = dates
.filter(d => this.getDateType_(d) === DateType.RRULE)
.map(d => tryParseRrulestr(d));

/** @private @const */
this.dates_ = dates
.filter(d => this.getDateType_(d) == DateType.DATE)
Expand All @@ -53,14 +46,14 @@ export class DatesList {
}

/**
* Determines if the given date is contained within the RRULEs or moment
* dates contained in the date list.
* Determines if the given date is contained within the moment dates
* contained in the date list.
* @param {!moment|string} date
* @return {boolean}
*/
contains(date) {
const m = this.moment_(date);
return this.matchesDate_(m) || this.matchesRrule_(m);
return this.matchesDate_(m);
}

/**
Expand All @@ -79,11 +72,6 @@ export class DatesList {
break;
}
}
const rruleDates = this.rrulestrs_
.map((/** {RRule} */ rrule) => rrule.after(date))
.filter(Boolean)
.map(normalizeRruleReturn);
firstDatesAfter.concat(rruleDates);

return firstDatesAfter.sort((a, b) => a.toDate() - b.toDate())[0];
}
Expand All @@ -99,30 +87,7 @@ export class DatesList {
}

/**
* Determines if any internal RRULE object matches the given date.
* @param {!moment} date
* @return {boolean}
* @private
*/
matchesRrule_(date) {
const nextDate = date
.clone()
.startOf('day')
.add(1, 'day')
.toDate();
return this.rrulestrs_.some((/** {RRule} */ rrule) => {
const rruleUTCDate = rrule.before(nextDate);
if (!rruleUTCDate) {
return false;
}
const rruleLocalDate = normalizeRruleReturn(rruleUTCDate);
const rruleMoment = this.moment_(rruleLocalDate);
return this.ReactDates_['isSameDay'](rruleMoment, date);
});
}

/**
* Distinguish between RRULE dates and moment dates.
* Distinguish between moment dates.
* @param {!moment|string} date
* @return {!DateType}
* @private
Expand All @@ -132,42 +97,6 @@ export class DatesList {
return DateType.DATE;
}

const dateStr = /** @type {string} */ (date);
if (tryParseRrulestr(dateStr)) {
return DateType.RRULE;
}

return DateType.INVALID;
}
}

/**
* RRULE returns dates as local time formatted at UTC, so the
* Date.prototype.getUTC* methods must be used to create a new date object.
* {@link https://github.com/jakubroztocil/rrule#important-use-utc-dates}
* @param {!Date} rruleDate
* @return {!Date}
*/
function normalizeRruleReturn(rruleDate) {
const year = rruleDate.getUTCFullYear();
const month = rruleDate.getUTCMonth();
const day = rruleDate.getUTCDate();
const hours = rruleDate.getUTCHours();
const minutes = rruleDate.getUTCMinutes();
const seconds = rruleDate.getUTCSeconds();
const ms = rruleDate.getUTCMilliseconds();
return new Date(year, month, day, hours, minutes, seconds, ms);
}

/**
* Tries to parse a string into an RRULE object.
* @param {string} str A string which represents a repeating date RRULE spec.
* @return {?JsonObject}
*/
function tryParseRrulestr(str) {
try {
return rrulestr(str);
} catch (e) {
return null;
}
}
8 changes: 3 additions & 5 deletions extensions/amp-date-picker/0.1/test/test-amp-date-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ describes.realWin(
"templates": [{
"id": "srcTemplate",
"dates": [
"2018-01-01",
"FREQ=WEEKLY;DTSTART=20180101T080000Z;WKST=SU;BYDAY=WE"
"2018-01-01"
]
}, {
"id": "defaultTemplate"
Expand Down Expand Up @@ -233,7 +232,7 @@ describes.realWin(

describe('templates', () => {
describe('element templates', () => {
it('should parse RRULE and date templates', () => {
it('should parse date templates', () => {
const template = createDateTemplate('{{template}}', {
dates: '2018-01-01',
});
Expand All @@ -246,7 +245,7 @@ describes.realWin(
});

describe('src templates', () => {
it('should parse RRULE and date templates', function() {
it('should parse date templates', function() {
this.timeout(4000);
const template = createDateTemplate('{{val}}', {
dates: '2018-01-01',
Expand All @@ -264,7 +263,6 @@ describes.realWin(
);
expect(srcTemplates[0].dates.contains('2018-01-01')).to.be.true;
expect(srcTemplates[0].dates.contains('2018-01-02')).to.be.false;
expect(srcTemplates[0].dates.contains('2018-01-03')).to.be.true;
expect(srcTemplates[0].template).to.equal(template);
expect(srcDefaultTemplate).to.equal(defaultTemplate);
});
Expand Down
12 changes: 3 additions & 9 deletions extensions/amp-date-picker/0.1/test/test-dates-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,14 @@ import {requireExternal} from '../../../../src/module';
describes.sandboxed('DatesList', {}, () => {
const moment = requireExternal('moment');

it('should accept date strings and RRULE strings', function() {
it('should accept date strings', function() {
this.timeout(3000);
const containedDate = '09/04/1998';
const notContainedDate = '09/03/1998';
const containedRrule =
'FREQ=WEEKLY;COUNT=10;DTSTART=20180101T000000Z;WKST=SU;BYDAY=TU,SA';
const matchesRrule = '01/02/2018';
const datesList = new DatesList([containedDate, containedRrule]);
const datesList = new DatesList([containedDate]);

expect(datesList.contains(containedDate)).to.be.true;
expect(datesList.contains(notContainedDate)).to.be.false;
expect(datesList.contains(matchesRrule)).to.be.true;
});

it('should accept moment objects', () => {
Expand Down Expand Up @@ -66,10 +62,8 @@ describes.sandboxed('DatesList', {}, () => {
const containedDate = '09/04/1998';
const dateBefore = '01/01/1998';
// const notContainedDate = '09/03/1998';
const containedRrule =
'FREQ=WEEKLY;COUNT=10;DTSTART=20180101T000000Z;WKST=SU;BYDAY=TU,SA';
// const matchesRrule = '01/02/2018';
const datesList = new DatesList([containedDate, containedRrule]);
const datesList = new DatesList([containedDate]);

const result = datesList.firstDateAfter(dateBefore);

Expand Down
22 changes: 7 additions & 15 deletions extensions/amp-date-picker/amp-date-picker.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,12 @@ and the user can select a date range with a starting date and ending date.

## Date formats

`amp-date-picker` attributes accept dates in ISO 8601 and RFC 5545 RRULE formats.
`amp-date-picker` attributes accept dates in ISO 8601.

[ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) formats dates as `YYYY-MM-DD`
and is the standard for sharing dates between electronic systems.
For example, ISO 8601 formats the date February 28 2018 as `2018-02-28`.

[RFC 5545 Recurrence Rules (RRULEs)](https://icalendar.org/iCalendar-RFC-5545/3-3-10-recurrence-rule.html)
standardize a format for specifying repeating dates.
For example, RFC 5545 formats Halloween as `RRULE:FREQ=YEARLY;BYMONTH=10;BYMONTHDAY=31`.
More complex dates are also possible, such as the United States Thanksgiving holiday,
which is every November on the fourth Thursday: `RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=+4TH`.
The API is not friendly to memorize, but there are various
[RRULE generators](https://jakubroztocil.github.io/rrule) available online.

## Attributes

##### mode
Expand Down Expand Up @@ -381,11 +373,11 @@ The day to specify as the first day of the week (0-6). The default value is `"0"

##### blocked

A space-separated list of ISO 8601 dates or RFC 5545 RRULE repeating dates to prevent the user from selecting on the calendar.
A space-separated list of ISO 8601 dates to prevent the user from selecting on the calendar.

##### highlighted

A space-separated list of ISO 8601 dates or RFC 5545 RRULE repeating dates to specially style as highlighted to draw the user's attention.
A space-separated list of ISO 8601 dates to specially style as highlighted to draw the user's attention.
Default styling is a blue dot on the date.

##### day-size
Expand Down Expand Up @@ -430,7 +422,7 @@ The following table lists the properties that you can specify in the JSON data:
</tr>
<tr>
<td><code>blocked</code></td>
<td>An array of ISO 8601 single dates or RFC 5545 RRULE repeating dates to render as blocked in the calendar view. The user is prevented from selecting these dates.</td>
<td>An array of ISO 8601 single dates to render as blocked in the calendar view. The user is prevented from selecting these dates.</td>
</tr>
<tr>
<td><code>date</code></td>
Expand All @@ -442,7 +434,7 @@ The following table lists the properties that you can specify in the JSON data:
</tr>
<tr>
<td><code>highlighted</code></td>
<td>An array of ISO 8601 single dates or RFC 5545 RRULE repeating dates to render as highlighted in the calendar view.</td>
<td>An array of ISO 8601 single dates to render as highlighted in the calendar view.</td>
</tr>
<tr>
<td><code>startDate</code></td>
Expand All @@ -459,7 +451,7 @@ The `src` attribute may be updated after a user gesture with [`amp-bind`](https:

###### template definition objects

The `dates` property is an array of ISO 8601 single dates or RFC 5545 RRULE repeating dates.
The `dates` property is an array of ISO 8601 single dates.
The `id` property specifies the `id` of a template that the date picker can use to
render the specified dates in the calendar view.

Expand Down Expand Up @@ -766,7 +758,7 @@ Using `src` prevents chached AMP documents from showing out-of-date information.

A `date-template` must have a `dates` or `default` attribute.

- **dates**: A space-separated list of ISO 8601 single dates or RFC 5545 RRULE repeating dates.
- **dates**: A space-separated list of ISO 8601 single dates.
The template content will render for the dates matching the dates in the attribute.
- **default**: If the `default` attribute is present, the template content will render for
all dates not matching an existing template.
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"promise-pjs": "1.1.4",
"prop-types": "15.7.2",
"react-dates": "15.5.3",
"rrule": "2.6.2",
"web-activities": "1.13.0",
"web-animations-js": "2.3.1"
},
Expand Down
14 changes: 0 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8940,11 +8940,6 @@ lru-queue@0.1:
dependencies:
es5-ext "~0.10.2"

luxon@^1.3.3:
version "1.19.3"
resolved "https://registry.yarnpkg.com/luxon/-/luxon-1.19.3.tgz#86c04a1395529b4386ae235a8fe16d0a82a0695b"
integrity sha512-YwTDjGRQC0QC9Iya2g2eKZfgEFqRId4ZoLHORQcfTMB/5xrTx427V7ZPjQJ1vzvhA2vJfG2bh1Kv8V8IFMWCUA==

magic-string@0.25.4:
version "0.25.4"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.4.tgz#325b8a0a79fc423db109b77fd5a19183b7ba5143"
Expand Down Expand Up @@ -11968,15 +11963,6 @@ rocambole@0.7.0:
dependencies:
esprima "^2.1"

rrule@2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/rrule/-/rrule-2.6.2.tgz#f175c6c0b20d6f798739e4e1d979ffb3d79589e8"
integrity sha512-xL38CM1zOYOIp4OO8hdD6zHH5UdR9siHMvPiv+CCSh7o0LYJ0owg87QcFW7GXJ0PfpLBHjanEMvvBjJxbRhAcQ==
dependencies:
tslib "^1.9.0"
optionalDependencies:
luxon "^1.3.3"

rsvp@^4.8.4:
version "4.8.5"
resolved "https://registry.yarnpkg.com/rsvp/-/rsvp-4.8.5.tgz#c8f155311d167f68f21e168df71ec5b083113734"
Expand Down

0 comments on commit 11e9de2

Please sign in to comment.