Skip to content

Commit

Permalink
Add range with start/end to classic REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed May 15, 2022
1 parent a8fda6b commit 4344a31
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hebcal/rest-api",
"version": "3.16.0",
"version": "3.17.0",
"author": "Michael J. Radwin (https://github.com/mjradwin)",
"keywords": [
"hebcal"
Expand All @@ -21,7 +21,7 @@
"url": "https://github.com/hebcal/hebcal-rest-api/issues"
},
"dependencies": {
"@hebcal/core": "^3.37.2",
"@hebcal/core": "^3.38.0",
"@hebcal/leyning": "^4.13.3"
},
"scripts": {
Expand Down Expand Up @@ -56,7 +56,7 @@
"eslint-config-google": "^0.14.0",
"jsdoc": "^3.6.10",
"jsdoc-to-markdown": "^7.1.1",
"rollup": "^2.72.1",
"rollup": "^2.73.0",
"rollup-plugin-terser": "^7.0.2"
}
}
15 changes: 15 additions & 0 deletions src/classic-rest-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import {
} from './common';
import holidayDescription from './holidays.json';

/**
* @private
* @param {Event} ev
* @return {string}
*/
function eventIsoDate(ev) {
return toISOString(ev.getDate().greg());
}

/**
* Formats a list events for the classic Hebcal.com JSON API response
* @param {Event[]} events
Expand All @@ -23,6 +32,12 @@ export function eventsToClassicApi(events, options, leyning=true) {
date: new Date().toISOString(),
};
result.location = locationToPlainObj(options.location);
if (events.length) {
result.range = {
start: eventIsoDate(events[0]),
end: eventIsoDate(events[events.length - 1]),
};
}
result.items = events.map((ev) => eventToClassicApiObject(ev, options, leyning));
return result;
}
Expand Down
19 changes: 19 additions & 0 deletions src/classic-rest-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ test('classic-api-no-sedra', (t) => {
location: {
geo: 'none',
},
range: {
start: '2022-05-15',
end: '2022-05-31',
},
items: [
{
title: 'Pesach Sheni',
Expand Down Expand Up @@ -468,6 +472,10 @@ test('location-zip', (t) => {
state: 'RI',
stateName: 'Rhode Island',
},
range: {
start: '2022-03-04',
end: '2022-03-04',
},
items: [
{
title: '1st of Adar II, 5782',
Expand Down Expand Up @@ -507,3 +515,14 @@ test('omer', (t) => {
};
t.deepEqual(obj, expected);
});

test('classic-api-empty', (t) => {
const apiResult = eventsToClassicApi([], {}, false);
delete apiResult.date;
const expected = {
title: 'Hebcal Diaspora',
location: {geo: 'none'},
items: [],
};
t.deepEqual(apiResult, expected);
});
4 changes: 2 additions & 2 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ export function getCalendarTitle(events, options) {
if (options.subscribe == '1') {
return title;
}
if (options.isHebrewYear || events.length == 0) {
if (options.year && (options.isHebrewYear || events.length == 0)) {
title += ' ' + options.year;
} else {
} else if (events.length) {
const start = events[0].getDate().greg();
const end = events[events.length - 1].getDate().greg();
if (start.getFullYear() != end.getFullYear()) {
Expand Down

0 comments on commit 4344a31

Please sign in to comment.