Commit 3b39b87e authored by Johannes Bill's avatar Johannes Bill

parse json

parent 2704d42b
var tmUtils = require('./timeUtils');
var now = new Date();
var includeString = "810a6deb-46f2-4fa8-b779-bb5c2a6b5577";
var excludeString = "bb74c170-b06f-4d2b-ab88-2f6bf9ce8068";
var _epochStart = new Date();
_epochStart.setDate(_epochStart.getDate() - 14);
var _epochEnd = new Date(_epochStart.getTime() + 60 * 24 * 3600000);
function calculateOpeningHours(json) {
var jsonIncl = json["include"];
for(var key in jsonIncl) {
var dateA = getOpeningHours(jsonIncl[key]);
console.log(key, dateA);
}
var jsonIncl = json[includeString];
var jsonExcl = json[excludeString];
var resultIncl = getIntervals(jsonIncl);
var resultExcl = getIntervals(jsonExcl);
console.log(tmUtils.andNotOperator(resultIncl, resultExcl));
}
function getIntervals (json) {
var result = [];
for(var key in json) {
var x = getOpeningHours(json[key]);
if(x)
result.push(x);
}
return tmUtils.orOperator(result);
}
function getDaySpan(dayoption) {
if(dayoption < 7) return [dayoption, dayoption];
switch(dayoption) {
......@@ -46,22 +64,22 @@ function parseDate(date) {
var year = now.getFullYear();
var split = date.split(".");
var month = parseInt(split[1]);
var month = parseInt(split[1]) - 1;
var day = parseInt(split[0]);
return new Date(year, month, day);
}
function getOpeningHours(json) {
var dayoption = parseInt(json["dayoption"]["0"]);
function getOpeningHours(data) {
var dayoption = parseInt(data["dayoption"]["0"]);
var daySpan = getDaySpan(dayoption);
var option = json["option"]["0"];
var option = data["option"]["0"];
var startDate = parseDate(json["opening_day_from"]) || _epochStart;
var endDate = parseDate(json["opening_day_to"]) || _epochEnd;
var startDate = parseDate(data["opening_day_from"]) || _epochStart;
var endDate = parseDate(data["opening_day_to"]) || _epochEnd;
var timeFrom = parseTime(json["opening_from"]);
var timeTill = parseTime(json["opening_to"]);
var timeFrom = parseTime(data["opening_from"]);
var timeTill = parseTime(data["opening_to"]);
switch(option) {
case "default":
......@@ -69,7 +87,7 @@ function getOpeningHours(json) {
case "season":
return tmUtils.standardFromTillDay(daySpan[0], daySpan[1], timeFrom, timeTill, startDate, endDate, 7);
case "repeatable":
var repeatOption = parseInt(json["repeatoption"]["0"]);
var repeatOption = parseInt(data["repeatoption"]["0"]);
var noInMonth;
if(repeatOption <= 4) noInMonth = repeatOption;
else if(repeatOption == 30) noInMonth = -1;
......@@ -86,6 +104,7 @@ function getOpeningHours(json) {
throw "invalid repeat option";
}
}
// throw "invalid data";
}
calculateOpeningHours(require('./testData/data.json'));
\ No newline at end of file
calculateOpeningHours(require('./testData/hofman.json'));
\ No newline at end of file
{
"f86608fb-16aa-4c70-9d57-0b5cac1a9618": {
},
"4ae7b6cb-07e8-4184-a00b-9db001f55bd3": {
"0": {
"value": ""
}
},
"f64563c6-c102-4836-a5f7-8f9246fcceef": {
"option": {
"0": "direktvermarkter-mit-geschaeft"
},
"select": "1"
},
"f62d066f-bf0d-42e2-a7ae-6f926fbb49d7": {
"0": {
"value": ""
}
},
"915f382b-51fc-48ba-93e0-152040f8f0e4": {
"0": {
"value": ""
}
},
"1b99ac64-7879-4802-80d9-0de99613efc9": {
"option": {
"0": "basis"
},
"check": "1"
},
"8cf3f417-4d1c-4795-87de-42124dcb2c20": {
"option": {
"0": "validated"
},
"select": "1"
},
"f118eeff-e254-4155-8e23-8149cb8b5eea": {
"0": {
"value": "per mail freigegeben"
}
},
"dbc68015-df76-42ed-a033-503b87e35685": {
"check": "1"
},
"0b13b5c0-bec6-445c-bdf2-e201ba0d43e4": {
"file": "",
"title": "",
"link": "",
"target": "0",
"rel": "",
"lightbox_image": "",
"spotlight_effect": "",
"caption": ""
},
"17709bb7-acf7-4ef5-8b26-4ed4fe63f8c9": {
"0": {
"value": ""
}
},
"3d305054-e356-4c09-8f4c-7795433a4589": {
"file": "",
"title": "",
"link": "",
"target": "0",
"rel": "",
"lightbox_image": "",
"spotlight_effect": "",
"caption": ""
},
"e8c1fdc9-50e0-4a9b-994a-f48f738dac64": {
},
"ed9cdd4c-ae8b-4ecb-bca7-e12a5153bc02": {
"0": {
"value": ""
}
},
"514ccde0-0a79-4594-8b72-b906ee4cede9": {
"0": {
"opening_day": "Montag",
"opening_from": "13:30",
"opening_to": "18:00"
},
"1": {
"opening_day": "Dienstag",
"opening_from": "8:00",
"opening_to": "18:00"
},
"2": {
"opening_day": "Mittwoch",
"opening_from": "8:00",
"opening_to": "18:00"
},
"3": {
"opening_day": "Donnerstag",
"opening_from": "8:00",
"opening_to": "18:00"
},
"4": {
"opening_day": "Freitag",
"opening_from": "8:00",
"opening_to": "18:00"
},
"5": {
"opening_day": "Samstag",
"opening_from": "8:00",
"opening_to": "12:30"
}
},
"810a6deb-46f2-4fa8-b779-bb5c2a6b5577": {
"0": {
"option": {
"0": "default"
},
"dayoption": {
"0": "10"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "08:00",
"opening_to": "18:00"
}
},
"bb74c170-b06f-4d2b-ab88-2f6bf9ce8068": {
"0": {
"option": {
"0": "default"
},
"dayoption": {
"0": "1"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "00:00",
"opening_to": "13:29"
},
"1": {
"option": {
"0": "default"
},
"dayoption": {
"0": "6"
},
"repeatoption": {
"0": ""
},
"holidayoption": {
"0": ""
},
"opening_day_from": "",
"opening_day_to": "",
"opening_from": "12:31",
"opening_to": "23:59"
}
},
"7ac7d682-2862-443b-83bd-8a3cf837ad8e": {
"option": {
"0": "0"
}
},
"6b3b589d-8060-4193-92b9-ce430d96cfdd": {
"0": {
"value": ""
}
},
"65359d5a-faca-46b8-8644-767d382d8b9b": {
"check": "1"
},
"533ea2dd-e37d-4add-aa73-ad8419040c22": {
"0": {
"value": ""
}
},
"6d9ab971-f969-499b-bb6b-9b87b601a961": {
"check": "1"
},
"f67464f3-4475-46a9-a262-11c8a777865d": {
"0": {
"value": ""
}
},
"1d0a9c64-78d4-4767-8617-c8a3a66fc770": {
"0": {
"value": ""
}
},
"05192b86-fc84-43cb-81d3-ab804086a873": {
"0": {
"value": ""
}
},
"d230a60f-0016-4180-b83a-13e4fd938328": {
"0": {
"value": "http:\/\/www.rundherum-gsund.de\/index.php?content=2_1"
}
},
"c70daef6-ffee-40ef-ba29-3d0065a5aaf5": {
},
"a77f06fc-1561-453c-a429-8dd05cdc29f5": {
"0": {
"value": "<p>Eier, Kartoffeln<\/p>"
}
},
"1a85a7a6-2aba-4480-925b-6b97d311ee6c": {
"0": {
"value": "<p>Eier, Kartoffeln<\/p>"
}
},
"efd91873-dce0-48fd-b40e-5bc0e9599f3e": {
},
"9ca9fc28-03f8-472d-b49f-8e2513cc8740": {
"file": "images\/regionalkaufapp\/unternehmen\/logo.png",
"title": "",
"link": "",
"target": "0",
"rel": "",
"lightbox_image": "",
"spotlight_effect": "",
"caption": "",
"width": 144,
"height": 100
},
"ffcc1c50-8dbd-4115-b463-b43bdcd44a57": {
"file": "images\/regionalkaufapp\/unternehmen\/allgemein\/leerbildlogo.png",
"title": "",
"link": "",
"target": "0",
"rel": "",
"lightbox_image": "",
"spotlight_effect": "",
"caption": "",
"width": 195,
"height": 145
},
"e8f08959-19ff-4cfa-ba37-bc42414eeb45": {
"value": "\/allgemein",
"title": "Kennen Sie diesen Anbieter? Weisen Sie Ihn doch darauf hin, dass mit einem Basis- oder Premiumeintrag auch Bilder von uns gemacht und angezeigt werden."
},
"051452fe-692b-4ce2-8567-7dc4d0343fee": {
"url": "",
"width": "640",
"height": "360",
"autoplay": "0"
},
"b68108e0-e49b-420d-97ec-d7c88af2b01a": {
"file": "",
"hits": "0",
"download_limit": "",
"size": 0
},
"92fda174-119b-4ee7-b14c-98c7f65224de": {
},
"4339a108-f907-4661-9aab-d6f3f00e736e": {
"0": {
"value": "Irlbacher Stra\u00dfe 2, Stauffendorf"
}
},
"ea0666d7-51e3-4e52-8617-25e3ad61f8b8": {
"0": {
"value": "94469"
}
},
"90a18889-884b-4d53-a302-4e6e4595efa0": {
"0": {
"value": "Deggendorf"
}
},
"160bd40a-3e0e-48de-b6cd-56cdcc9db892": {
"location": "48.819945, 12.886710600000015"
},
"9ff517f0-d029-4e85-a411-fdb2bbae3b8c": {
"location": "48.819945, 12.886710600000015"
},
"33bfd53b-1f42-4628-bb3d-461cacecdd84": {
"0": {
"value": "12.886710600000015, 48.819945"
}
},
"7516ee89-8702-4675-806b-58fe5bc56305": {
},
"b870164b-fe78-45b0-b840-8ebceb9b9cb6": {
"0": {
"value": "09931 2574"
}
},
"272860a4-9ba0-4303-b2d3-77abb8d6fd42": {
"0": {
"value": ""
}
},
"5dcfa5ab-3769-4c5e-8cd2-1ac91643be70": {
"0": {
"value": ""
}
},
"8a91aab2-7862-4a04-bd28-07f1ff4acce5": {
"0": {
"value": "09931 906696"
}
},
"3f15b5e4-0dea-4114-a870-1106b85248de": {
"0": {
"value": "anita.halser@gmx.de",
"text": "",
"subject": "",
"body": ""
}
},
"0b3d983e-b2fa-4728-afa0-a0b640fa34dc": {
"0": {
"value": "",
"text": "",
"target": "0",
"custom_title": "",
"rel": ""
}
},
"b7628b63-d551-4b14-ac70-27cf0c6e95fc": {
"0": {
"value": ""
}
},
"2081965c-c781-4ae1-99c3-54b959145cfe": {
"value": "1"
},
"8eeeb531-0dc3-415a-8f4a-c7f88d1ac627": {
"value": "1"
},
"9bd1d374-6a54-4054-be1e-87839eb24fb6": {
},
"7056f1d2-5253-40b6-8efd-d289b10a8c69": {
"item": {
"0": "1823"
}
},
"6eaab8b7-b4f5-4d3c-8193-db3542aec0a5": {
},
"9eeb4b7d-f289-4137-91ea-5275bb3376a7": {
},
"cf6dd846-5774-47aa-8ca7-c1623c06e130": {
"votes": 0,
"value": 0
},
"cffd7b0a-317a-4cf9-852e-65fd31526a53": {
"favorites": 0
}
}
\ No newline at end of file
......@@ -188,7 +188,7 @@
"holidayoption": {
"0": ""
},
"opening_day_from": "02.01",
"opening_day_from": "04.01",
"opening_day_to": "",
"opening_from": "9:00",
"opening_to": "18:00"
......
......@@ -159,9 +159,21 @@ function singleDay(date) {
var startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
var endDate = new Date(startDate);
endDate.setDate(endDate.getDate() + 1);
return [[startDate, endDate]];
return [
[startDate, endDate]
];
}
function wholeInterval(start, end) {
return [new Date(start.getFullYear(), start.getMonth(), start.getDate()),
new Date(end.getFullYear(), end.getMonth(), end.getDate())];
}
function limitToInterval(intervals, start, end) {
var limit = wholeInterval(start, end);
intervals = andOperator(intervals, limit);
return intervals;
}
function orOperator(intervals) {
var result = [];
......@@ -169,6 +181,7 @@ function orOperator(intervals) {
result = result.concat(intervals[i]);
}
sortDateIntervals(result);
mergeOverlaps(result);
return result;
}
......@@ -258,4 +271,5 @@ module.exports.singleDay = singleDay;
module.exports.orOperator = orOperator;
module.exports.andOperator = andOperator;
module.exports.andNotOperator = andNotOperator;
module.exports.mergeOverlaps = mergeOverlaps;
\ No newline at end of file
module.exports.mergeOverlaps = mergeOverlaps;
module.exports.limitToInterval = limitToInterval;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment