Skip to content

Commit

Permalink
Improve Location field for CSV export
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Dec 1, 2022
1 parent ae340a4 commit f1e219d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hebcal/rest-api",
"version": "4.3.8",
"version": "4.3.9",
"author": "Michael J. Radwin (https://github.com/mjradwin)",
"keywords": [
"hebcal"
Expand Down
21 changes: 9 additions & 12 deletions src/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ const csvHeader = '"Subject","Start Date","Start Time","End Date","End Time","Al

const CATEGORY = {
dafyomi: 'Daf Yomi',
mishnayomi: 'Mishna Yomi',
yerushalmi: 'Yerushalmi Yomi',
hebdate: 'Hebrew Date',
holiday: 'Jewish Holidays',
mevarchim: null,
molad: null,
omer: null,
mevarchim: '',
molad: '',
omer: '',
parashat: 'Torah Reading',
roshchodesh: 'Jewish Holidays',
user: 'Personal',
zmanim: '',
};

/**
Expand Down Expand Up @@ -45,19 +48,13 @@ export function eventToCsv(e, options) {

let loc = 'Jewish Holidays';
const mask = e.getFlags();
if (timed && options.location && options.location.name) {
const locationName = options.location.name;
if (timed && typeof options.location === 'object') {
const locationName = options.location.getShortName();
const comma = locationName.indexOf(',');
loc = (comma === -1) ? locationName : locationName.substring(0, comma);
} else if (mask & flags.DAF_YOMI) {
const colon = subj.indexOf(': ');
if (colon != -1) {
loc = subj.substring(0, colon);
subj = subj.substring(colon + 2);
}
} else {
const category = CATEGORY[getEventCategories(e)[0]];
if (category) {
if (typeof category === 'string') {
loc = category;
}
}
Expand Down
24 changes: 23 additions & 1 deletion src/csv.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable max-len */
import test from 'ava';
import {HebrewCalendar, Location, HDate, HebrewDateEvent} from '@hebcal/core';
import {HebrewCalendar, Location, HDate, HebrewDateEvent,
YerushalmiYomiEvent, DafYomiEvent, MishnaYomiEvent,
OmerEvent} from '@hebcal/core';
import {eventToCsv, eventsToCsv} from './csv';

test('eventToCsv', (t) => {
Expand Down Expand Up @@ -105,3 +107,23 @@ test('newline', (t) => {
const actual = eventToCsv(ev, {locale: 'en'});
t.is(actual, '"2nd of Sivan","5/15/2010",,,,"true","foo / bar / baaz","3","Hebrew Date"');
});

test('CSV Location', (t) => {
const hd = new HDate(new Date(2022, 10, 15));
const toTest = [
[new DafYomiEvent(hd),
'"Nedarim 21","11/15/2022",,,,"true","","3","Daf Yomi"'],
[new MishnaYomiEvent(hd,
[{k: 'Bikkurim', v: '4:1'}, {k: 'Bikkurim', v: '4:2'}]),
'"Bikkurim 4:1-2","11/15/2022",,,,"true","","3","Mishna Yomi"'],
[new YerushalmiYomiEvent(hd,
{name: 'Berakhot', blatt: 2, ed: 'vilna'}),
'"Berakhot 2","11/15/2022",,,,"true","","3","Yerushalmi Yomi"'],
[new OmerEvent(new HDate(28, 'Nisan', 5783), 13),
'"13th day of the Omer","4/19/2023",,,,"true","","3",""'],
];
for (const [ev, expected] of toTest) {
const actual = eventToCsv(ev, {});
t.is(actual, expected);
}
});

0 comments on commit f1e219d

Please sign in to comment.