Skip to content

Commit

Permalink
danovak fixed, aprtial fix for ERNICommunity#121
Browse files Browse the repository at this point in the history
  • Loading branch information
yohny committed Mar 27, 2017
1 parent 25f0e21 commit f455f9a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 479 deletions.
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"nonew": true,
"undef": true,
"unused": true,
"esversion": 6,
//enviroments
"node": true,
"browser": true,
Expand Down
41 changes: 17 additions & 24 deletions parsers/danovak.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,29 @@ module.exports.parse = function(html, date, callback) {

var currentDay = $('div#privitanie>div').children().filter(function(index, item) { return $(item).text().indexOf(date.format("DD.MM.YYYY")) > -1; }).eq(0);
if (currentDay) {
var soupMenu = currentDay.next();
var mainMenu = soupMenu.next();
var soupItems = soupMenu.text().split(/\//g);
var mainItems = mainMenu.text().split(/\d\.\s/g).filter(function(item) { return item.trim() !== ''; });

dayMenu = soupItems.map(function(item, index) { return parseMenu(item, index, true); });
dayMenu = dayMenu.concat(mainItems.map(function(item, index) { return parseMenu(item, index, false); }));
}

function parseMenu(item, index, isSoup) {
if (item.trim() === '') {
return;
let menuHolder = currentDay.next();
let menuTextNodes = menuHolder.contents().filter(function() { return this.type === 'text'; });

menuTextNodes[0].data.trim().split(/\//g).forEach(function(itemTxt) { // on first line there are soups separated by '/'
dayMenu.push({ isSoup: true, text: itemTxt.trim(), price: NaN });
});
for (let i = 1; i < menuTextNodes.length; i++) {
let text = menuTextNodes[i].data.trim();
let price = NaN;
/* jshint -W083 */
text = text.replace(/\d+,\d+\s?/, (match) => {
price = parseFloat(match.replace(',', '.'));
});
/* jshint +W083 */
dayMenu.push({ isSoup: false, text: normalize(text), price: price });
}

var text = item.split(/\d+\,\d+/)[0];
var tmp = { isSoup: false, text: normalize(text), price: NaN };
if (isSoup) {
tmp.isSoup = true;
} else {
var priceString = item.match(/\d+\,\d+/g)[0];
tmp.price = parseFloat(priceString.replace(",", "."));
}

return tmp;
}

callback(dayMenu);

function normalize(str) {
return str.normalizeWhitespace()
return str.removeItemNumbering()
.normalizeWhitespace()
.removeMetrics()
.correctCommaSpacing();
}
Expand Down
63 changes: 1 addition & 62 deletions test/danovak.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,65 +185,4 @@ describe('danovak_zomato_parser', function () {
assert.equal(menu[6].price, 5.50);
});
});
});

describe('danovak_own_parser', function () {
describe('parsing sample 2017-01-10', function () {

var html = fs.readFileSync(__dirname + '/samples/Danovak.2017-01-10.html', { encoding: "utf-8" });
var menu;

before(function (done) {
parser_danovak.parse(html, moment("2017-01-10"), function (menuItems) {
menu = menuItems;
done();
});
});

it("should return 7 items", function () {
assert.equal(menu.length, 7);
});

it("1st item correct", function () {
assert.equal(menu[0].isSoup, true);
assert.equal(menu[0].text.trim(), "Hrášková s pečeňovými haluškami");
assert(isNaN(menu[0].price));
});

it("2nd item correct", function () {
assert.equal(menu[1].isSoup, true);
assert.equal(menu[1].text.trim(), "Zemiaková na kyslo");
assert(isNaN(menu[1].price));
});

it("3rd item correct", function () {
assert.equal(menu[2].isSoup, false);
assert.equal(menu[2].text.trim(), "Kurací steak s anglickou zeleninou, varené zemiaky");
assert.equal(menu[2].price, 3.99);
});

it("4th item correct", function () {
assert.equal(menu[3].isSoup, false);
assert.equal(menu[3].text.trim(), "Kôprová omáčka s hovädzím mäskom a domácou parenou knedľou");
assert.equal(menu[3].price, 3.99);
});

it("5th item correct", function () {
assert.equal(menu[4].isSoup, false);
assert.equal(menu[4].text.trim(), "Ryžový nákyp s bielkovým snehom a ovocným prelivom");
assert.equal(menu[4].price, 3.99);
});

it("6th item correct", function () {
assert.equal(menu[5].isSoup, false);
assert.equal(menu[5].text.trim(), "Cestoviny Carbonara");
assert.equal(menu[5].price, 3.99);
});

it("7th item correct", function () {
assert.equal(menu[6].isSoup, false);
assert.equal(menu[6].text.trim(), "Daňovákov burger(mleté mäso, šalátové listy, slaninka, syr, paradajka)BBQ om. hranolky, šalát");
assert.equal(menu[6].price, 5.50);
});
});
});
});
Loading

0 comments on commit f455f9a

Please sign in to comment.