UNPKG

calc-transporte

Version:
2,329 lines (2,223 loc) 242 kB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.calcTransporte = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ module.exports={ "sube": [ { "date": "2018-02-01", "values": [ { "trip": 1, "value": 1 }, { "trip": 2, "value": 0.5 }, { "trip": 3, "value": 0.25 } ] } ], "subway": [ { "date": "2016-10-01", "values": [ { "trip": 21, "value": 0.8 }, { "trip": 31, "value": 0.7 }, { "trip": 41, "value": 0.6 } ] } ], "benefit1": [ { "date": "1999-01-01", "value": 0.45 } ], "benefit2": [ { "date": "2016-05-01", "value": 4 }, { "date": "2018-05-01", "value": 6 }, { "date": "2018-07-01", "value": 7 } ] } },{}],2:[function(require,module,exports){ module.exports={ "bus": [ { "date": "2010-01-01", "values": [ { "id": "0", "price": 1.1 }, { "id": "4", "price": 1.75 } ] }, { "date": "2011-12-01", "values": [ { "id": "0", "price": 1.5 }, { "id": "4", "price": 2.2 } ] }, { "date": "2014-01-01", "values": [ { "id": "0", "price": 2.5 }, { "id": "1", "price": 2.7 }, { "id": "2", "price": 2.85 }, { "id": "3", "price": 3.9 }, { "id": "4", "price": 4.5 } ] }, { "date": "2014-07-01", "values": [ { "id": "0", "price": 3.0 }, { "id": "1", "price": 3.25 }, { "id": "2", "price": 3.5 }, { "id": "3", "price": 4.0 }, { "id": "4", "price": 4.7 } ] }, { "date": "2016-04-01", "values": [ { "id": "0", "price": 6.0 }, { "id": "1", "price": 6.25 }, { "id": "2", "price": 6.5 }, { "id": "3", "price": 6.75 }, { "id": "4", "price": 7.0 } ] }, { "date": "2018-02-01", "values": [ { "id": "0", "price": 8.0 }, { "id": "1", "price": 8.25 }, { "id": "2", "price": 8.5 }, { "id": "3", "price": 9.0 }, { "id": "4", "price": 9.5 } ] }, { "date": "2018-04-01", "values": [ { "id": "0", "price": 9.0 }, { "id": "1", "price": 9.25 }, { "id": "2", "price": 9.5 }, { "id": "3", "price": 10.25 }, { "id": "4", "price": 10.75 } ] }, { "date": "2018-06-01", "values": [ { "id": "0", "price": 10.0 }, { "id": "1", "price": 10.5 }, { "id": "2", "price": 10.75 }, { "id": "3", "price": 11.25 }, { "id": "4", "price": 11.75 } ] }, { "date": "2018-08-15", "values": [ { "id": "0", "price": 11.0 }, { "id": "1", "price": 11.75 }, { "id": "2", "price": 12.25 }, { "id": "3", "price": 13.0 }, { "id": "4", "price": 13.75 } ] }, { "date": "2018-09-15", "values": [ { "id": "0", "price": 12.0 }, { "id": "1", "price": 13.0 }, { "id": "2", "price": 13.75 }, { "id": "3", "price": 14.5 }, { "id": "4", "price": 15.25 } ] }, { "date": "2018-10-15", "values": [ { "id": "0", "price": 13.0 }, { "id": "1", "price": 14.25 }, { "id": "2", "price": 15.0 }, { "id": "3", "price": 15.75 }, { "id": "4", "price": 16.5 } ] }, { "date": "2019-01-12", "values": [ { "id": "0", "price": 15.0 }, { "id": "1", "price": 16.5 }, { "id": "2", "price": 17.5 }, { "id": "3", "price": 18.5 }, { "id": "4", "price": 19 } ] }, { "date": "2019-02-15", "values": [ { "id": "0", "price": 16.5 }, { "id": "1", "price": 18 }, { "id": "2", "price": 19 }, { "id": "3", "price": 20 }, { "id": "4", "price": 21 } ] }, { "date": "2019-03-15", "values": [ { "id": "0", "price": 18 }, { "id": "1", "price": 20 }, { "id": "2", "price": 21 }, { "id": "3", "price": 22 }, { "id": "4", "price": 23 } ] } ], "train": { "sanmartin": [ { "date": "2016-04-01", "values": [ { "id": "0", "price": 4.0 }, { "id": "1", "price": 5.0 }, { "id": "2", "price": 6.0 } ] }, { "date": "2018-02-01", "values": [ { "id": "0", "price": 5.5 }, { "id": "1", "price": 6.75 }, { "id": "2", "price": 8.0 } ] }, { "date": "2018-04-01", "values": [ { "id": "0", "price": 6.25 }, { "id": "1", "price": 7.5 }, { "id": "2", "price": 9.0 } ] }, { "date": "2018-06-01", "values": [ { "id": "0", "price": 6.75 }, { "id": "1", "price": 8.25 }, { "id": "2", "price": 10.0 } ] }, { "date": "2018-08-15", "values": [ { "id": "0", "price": 7.5 }, { "id": "1", "price": 9.5 }, { "id": "2", "price": 11.25 } ] }, { "date": "2018-09-15", "values": [ { "id": "0", "price": 8.25 }, { "id": "1", "price": 10.25 }, { "id": "2", "price": 12.5 } ] }, { "date": "2018-10-15", "values": [ { "id": "0", "price": 8.75 }, { "id": "1", "price": 11.0 }, { "id": "2", "price": 13.25 } ] }, { "date": "2019-01-12", "values": [ { "id": "0", "price": 10 }, { "id": "1", "price": 12.25 }, { "id": "2", "price": 15 } ] }, { "date": "2019-02-15", "values": [ { "id": "0", "price": 11.25 }, { "id": "1", "price": 14 }, { "id": "2", "price": 16.75 } ] }, { "date": "2019-03-15", "values": [ { "id": "0", "price": 12.25 }, { "id": "1", "price": 15.5 }, { "id": "2", "price": 18.5 } ] } ], "mitre": [ { "date": "2016-04-01", "values": [ { "id": "0", "price": 4.0 }, { "id": "1", "price": 5.0 }, { "id": "2", "price": 6.0 } ] }, { "date": "2018-02-01", "values": [ { "id": "0", "price": 5.5 }, { "id": "1", "price": 6.75 }, { "id": "2", "price": 8.0 } ] }, { "date": "2018-04-01", "values": [ { "id": "0", "price": 6.25 }, { "id": "1", "price": 7.5 }, { "id": "2", "price": 9.0 } ] }, { "date": "2018-06-01", "values": [ { "id": "0", "price": 6.75 }, { "id": "1", "price": 8.25 }, { "id": "2", "price": 10.0 } ] }, { "date": "2018-08-15", "values": [ { "id": "0", "price": 7.5 }, { "id": "1", "price": 9.5 }, { "id": "2", "price": 11.25 } ] }, { "date": "2018-09-15", "values": [ { "id": "0", "price": 8.25 }, { "id": "1", "price": 10.25 }, { "id": "2", "price": 12.5 } ] }, { "date": "2018-10-15", "values": [ { "id": "0", "price": 8.75 }, { "id": "1", "price": 11.0 }, { "id": "2", "price": 13.25 } ] }, { "date": "2019-01-12", "values": [ { "id": "0", "price": 10 }, { "id": "1", "price": 12.25 }, { "id": "2", "price": 15 } ] }, { "date": "2019-02-15", "values": [ { "id": "0", "price": 11.25 }, { "id": "1", "price": 14 }, { "id": "2", "price": 16.75 } ] }, { "date": "2019-03-15", "values": [ { "id": "0", "price": 12.25 }, { "id": "1", "price": 15.5 }, { "id": "2", "price": 18.5 } ] } ], "sarmiento": [ { "date": "2016-04-01", "values": [ { "id": "0", "price": 4.0 }, { "id": "1", "price": 5.0 }, { "id": "2", "price": 6.0 } ] }, { "date": "2018-02-01", "values": [ { "id": "0", "price": 5.5 }, { "id": "1", "price": 6.75 }, { "id": "2", "price": 8.0 } ] }, { "date": "2018-04-01", "values": [ { "id": "0", "price": 6.25 }, { "id": "1", "price": 7.5 }, { "id": "2", "price": 9.0 } ] }, { "date": "2018-06-01", "values": [ { "id": "0", "price": 6.75 }, { "id": "1", "price": 8.25 }, { "id": "2", "price": 10.0 } ] }, { "date": "2018-08-15", "values": [ { "id": "0", "price": 7.5 }, { "id": "1", "price": 9.5 }, { "id": "2", "price": 11.25 } ] }, { "date": "2018-09-15", "values": [ { "id": "0", "price": 8.25 }, { "id": "1", "price": 10.25 }, { "id": "2", "price": 12.5 } ] }, { "date": "2018-10-15", "values": [ { "id": "0", "price": 8.75 }, { "id": "1", "price": 11.0 }, { "id": "2", "price": 13.25 } ] }, { "date": "2019-01-12", "values": [ { "id": "0", "price": 10 }, { "id": "1", "price": 12.25 }, { "id": "2", "price": 15 } ] }, { "date": "2019-02-15", "values": [ { "id": "0", "price": 11.25 }, { "id": "1", "price": 14 }, { "id": "2", "price": 16.75 } ] }, { "date": "2019-03-15", "values": [ { "id": "0", "price": 12.25 }, { "id": "1", "price": 15.5 }, { "id": "2", "price": 18.5 } ] } ], "belgranosur": [ { "date": "2016-04-01", "values": [ { "id": "0", "price": 2.0 }, { "id": "1", "price": 2.4 }, { "id": "2", "price": 3.4 } ] }, { "date": "2018-02-01", "values": [ { "id": "0", "price": 2.75 }, { "id": "1", "price": 3.25 }, { "id": "2", "price": 4.5 } ] }, { "date": "2018-04-01", "values": [ { "id": "0", "price": 3.0 }, { "id": "1", "price": 3.75 }, { "id": "2", "price": 5.0 } ] }, { "date": "2018-06-01", "values": [ { "id": "0", "price": 3.25 }, { "id": "1", "price": 4.0 }, { "id": "2", "price": 5.5 } ] }, { "date": "2018-08-15", "values": [ { "id": "0", "price": 4.0 }, { "id": "1", "price": 5.0 }, { "id": "2", "price": 6.75 } ] }, { "date": "2018-09-15", "values": [ { "id": "0", "price": 4.75 }, { "id": "1", "price": 5.75 }, { "id": "2", "price": 7.75 } ] }, { "date": "2018-10-15", "values": [ { "id": "0", "price": 5.5 }, { "id": "1", "price": 6.5 }, { "id": "2", "price": 8.5 } ] }, { "date": "2018-12-01", "values": [ { "id": "0", "price": 5.5 }, { "id": "1", "price": 7.25 }, { "id": "2", "price": 8.75 } ] }, { "date": "2019-01-12", "values": [ { "id": "0", "price": 6.25 }, { "id": "1", "price": 8.25 }, { "id": "2", "price": 10 } ] }, { "date": "2019-02-15", "values": [ { "id": "0", "price": 7 }, { "id": "1", "price": 9.25 }, { "id": "2", "price": 11 } ] }, { "date": "2019-03-15", "values": [ { "id": "0", "price": 7.75 }, { "id": "1", "price": 10.25 }, { "id": "2", "price": 12.25 } ] } ], "urquiza": [ { "date": "2016-04-01", "values": [ { "id": "0", "price": 2.0 }, { "id": "1", "price": 3.0 }, { "id": "2", "price": 3.6 } ] }, { "date": "2018-02-01", "values": [ { "id": "0", "price": 3.0 }, { "id": "1", "price": 4.0 }, { "id": "2", "price": 5.0 } ] }, { "date": "2018-04-01", "values": [ { "id": "0", "price": 3.25 }, { "id": "1", "price": 4.5 }, { "id": "2", "price": 5.5 } ] }, { "date": "2018-06-01", "values": [ { "id": "0", "price": 3.5 }, { "id": "1", "price": 5.0 }, { "id": "2", "price": 6.0 } ] }, { "date": "2018-08-15", "values": [ { "id": "0", "price": 4.25 }, { "id": "1", "price": 6.0 }, { "id": "2", "price": 7.25 } ] }, { "date": "2018-09-15", "values": [ { "id": "0", "price": 5.0 }, { "id": "1", "price": 7.0 }, { "id": "2", "price": 8.5 } ] }, { "date": "2018-10-15", "values": [ { "id": "0", "price": 5.75 }, { "id": "1", "price": 8.0 }, { "id": "2", "price": 9.5 } ] } ], "belgranonorte": [ { "date": "2016-04-01", "values": [ { "id": "0", "price": 2.0 }, { "id": "1", "price": 2.4 }, { "id": "2", "price": 3.4 }, { "id": "3", "price": 3.5 }, { "id": "4", "price": 4.0 }, { "id": "5", "price": 4.8 } ] }, { "date": "2018-02-01", "values": [ { "id": "0", "price": 2.75 }, { "id": "1", "price": 3.25 }, { "id": "2", "price": 4.5 }, { "id": "3", "price": 4.75 }, { "id": "4", "price": 5.5 }, { "id": "5", "price": 6.5 } ] }, { "date": "2018-04-01", "values": [ { "id": "0", "price": 3.0 }, { "id": "1", "price": 3.75 }, { "id": "2", "price": 5.0 }, { "id": "3", "price": 5.25 }, { "id": "4", "price": 6.25 }, { "id": "5", "price": 7.25 } ] }, { "date": "2018-06-01", "values": [ { "id": "0", "price": 3.25 }, { "id": "1", "price": 4.0 }, { "id": "2", "price": 5.5 }, { "id": "3", "price": 5.75 }, { "id": "4", "price": 6.75 }, { "id": "5", "price": 8.0 } ] }, { "date": "2018-08-15", "values": [ { "id": "0", "price": 3.75 }, { "id": "1", "price": 4.5 }, { "id": "2", "price": 6.25 }, { "id": "3", "price": 6.5 }, { "id": "4", "price": 7.75 }, { "id": "5", "price": 9.25 } ] }, { "date": "2018-09-15", "values": [ { "id": "0", "price": 4.25 }, { "id": "1", "price": 5.0 }, { "id": "2", "price": 6.75 }, { "id": "3", "price": 7.25 }, { "id": "4", "price": 8.5 }, { "id": "5", "price": 10.0 } ] }, { "date": "2018-10-15", "values": [ { "id": "0", "price": 4.75 }, { "id": "1", "price": 5.5 }, { "id": "2", "price": 7.5 }, { "id": "3", "price": 8.0 }, { "id": "4", "price": 9.25 }, { "id": "5", "price": 11.0 } ] } ], "roca": [ { "date": "2016-04-01", "values": [ { "id": "0", "price": 2.0 }, { "id": "1", "price": 3.0 }, { "id": "2", "price": 4.0 } ] }, { "date": "2018-02-01", "values": [ { "id": "0", "price": 2.75 }, { "id": "1", "price": 4.0 }, { "id": "2", "price": 5.5 } ] }, { "date": "2018-04-01", "values": [ { "id": "0", "price": 3.0 }, { "id": "1", "price": 4.5 }, { "id": "2", "price": 6.25 } ] }, { "date": "2018-06-01", "values": [ { "id": "0", "price": 3.25 }, { "id": "1", "price": 5.0 }, { "id": "2", "price": 6.0 } ] }, { "date": "2018-08-15", "values": [ { "id": "0", "price": 4.0 }, { "id": "1", "price": 5.0 }, { "id": "2", "price": 6.75 } ] }, { "date": "2018-09-15", "values": [ { "id": "0", "price": 4.75 }, { "id": "1", "price": 5.75 }, { "id": "2", "price": 7.75 } ] }, { "date": "2018-10-15", "values": [ { "id": "0", "price": 5.5 }, { "id": "1", "price": 6.5 }, { "id": "2", "price": 8.5 } ] }, { "date": "2018-12-01", "values": [ { "id": "0", "price": 5.5 }, { "id": "1", "price": 7.25 }, { "id": "2", "price": 8.75 } ] }, { "date": "2019-01-12", "values": [ { "id": "0", "price": 6.25 }, { "id": "1", "price": 8.25 }, { "id": "2", "price": 10 } ] }, { "date": "2019-02-15", "values": [ { "id": "0", "price": 7 }, { "id": "1", "price": 9.25 }, { "id": "2", "price": 11 } ] }, { "date": "2019-03-15", "values": [ { "id": "0", "price": 7.75 }, { "id": "1", "price": 10.25 }, { "id": "2", "price": 12.25 } ] } ] }, "subway": [ { "date": "2016-10-01", "values": [ { "id": "0", "price": 7.5 } ] }, { "date": "2018-08-01", "values": [ { "id": "0", "price": 12.5 } ] }, { "date": "2018-11-20", "values": [ { "id": "0", "price": 13.5 } ] }, { "date": "2018-12-20", "values": [ { "id": "0", "price": 14.5 } ] }, { "date": "2019-01-20", "values": [ { "id": "0", "price": 15.5 } ] }, { "date": "2019-02-20", "values": [ { "id": "0", "price": 16.5 } ] } ], "trolley": [ { "date": "2016-10-01", "values": [ { "id": "0", "price": 2.5 } ] }, { "date": "2018-08-01", "values": [ { "id": "0", "price": 4.0 } ] } ] } },{}],3:[function(require,module,exports){ "use strict"; module.exports = require("./lib/calc_transporte"); },{"./lib/calc_transporte":5}],4:[function(require,module,exports){ 'use strict'; var utils = require('./utils'); var defaults = require('lodash.defaults'); var flowRight = require('lodash.flowright'); var defaultBenefitValues = require('../data/benefit-values.json'); var clone = flowRight(JSON.parse, JSON.stringify); function Benefit(values) { var self = this; values = defaults(clone(values || {}), defaultBenefitValues); self.values = values } Benefit.prototype.getSubeBenefit = function getSubeBenefit(trip, date) { var self = this; return getBenefit(self.values.sube, trip, date); } Benefit.prototype.getSubwayBenefit = function getSubwayBenefit(trip, date) { var self = this; return getBenefit(self.values.subway, trip, date); } Benefit.prototype.getSocialBenefit = function getSocialBenefit(transport, date) { var self = this; var result; var data = self.values.benefit1; if (transport == 'subway') { data = self.values.benefit2 } var _date = utils.getClosestDate(date, data); if (_date > date) { return -1; } else { return data.find(function (element) { return element.date === _date; }).value; } } var getBenefit = function (values, index, date) { var result = 1; var _date = utils.getClosestDate(date, values); if (_date > date) { return result; } else { var dicounts = values.find(function (element) { return element.date === _date; }).values; dicounts.forEach(function (item) { if (index >= item.trip) { result = item.value; } }); } return result; } module.exports = Benefit; },{"../data/benefit-values.json":1,"./utils":7,"lodash.defaults":8,"lodash.flowright":9}],5:[function(require,module,exports){ 'use strict'; var defaults = require('lodash.defaults'); var flowRight = require('lodash.flowright'); var forEach = require('lodash.foreach'); var utils = require('./utils'); var Transport = require('./transport'); var Benefit = require('./benefit'); var defaultOptions = { date: null, roundtrip: true, benefit: false, weekTimes: 5, trips: [] }; var maximumSube = 8.00; var clone = flowRight(JSON.parse, JSON.stringify); function CalcTransporte(values, benefitValues) { var self = this; self.transport = new Transport(values); self.benefit = new Benefit(benefitValues); } CalcTransporte.prototype.getTransports = function getTransports() { var self = this; return self.transport.getTransports(); } CalcTransporte.prototype.getTrainLines = function getTrainLines() { var self = this; return self.transport.getTrainLines(); } CalcTransporte.prototype.getPricesByDate = function getPricesByDate(date) { var self = this; return self.transport.getPricesByDate(date); } CalcTransporte.prototype.calculate = function calculate(options) { var self = this; // add missing parameters. options.date = utils.formatDate(options.date); options = defaults(clone(options || {}), defaultOptions); // prevent non desired values forEach(options, function (n, key) { var differentType = defaultOptions[key] != null && typeof n !== typeof defaultOptions[key] if (differentType) { n = defaultOptions[key]; options[key] = defaultOptions[key]; } }); //variables var times = options.weekTimes * 4; var trips = createTrips(self, options); var result = { going: { cost: 0.00, costDaily: 0.00, savingDaily: 0.00, trip: [] }, returning: { cost: 0.00, costDaily: 0.00, savingDaily: 0.00, trip: [] }, discounts: { sube: 0.00, social: 0.00, subway: 0.00 } }; var indexTimes; var subwayTimes = 0; for (indexTimes = 0; indexTimes < times; indexTimes++) { trips.forEach(function (trip) { var price = trip.price; //Subway Discount if (trip.transport == 'subway') { subwayTimes++; //No benefit in subway /* if (options.benefit) { var subwayBenefit = trip.socialBenefit; if (subwayBenefit > 0) { price = subwayBenefit; result.discounts.social += trip.price - price; } } */ var priceWithDiscountSubway = price * self.benefit.getSubwayBenefit(subwayTimes, trip.date); result.discounts.subway += price - priceWithDiscountSubway; price = priceWithDiscountSubway; } else { var price = price * trip.socialBenefit; result.discounts.social += trip.price - price; } //Sube Discount var priceWithDiscountSube = price * trip.subeBenefit; var _priceTrip = 0.00; var _discountSube = 0.00; if (price - priceWithDiscountSube < maximumSube) { _priceTrip = priceWithDiscountSube; _discountSube = price - priceWithDiscountSube; } else { _priceTrip = price - maximumSube; _discountSube = maximumSube; } result[trip.type].cost += _priceTrip; result.discounts.sube += _discountSube; if (indexTimes == 0) { result[trip.type].trip[trip.index - 1] = { transport: trip.transport, price: trip.price, priceWithDiscount: _priceTrip }; result[trip.type].costDaily += _priceTrip; result[trip.type].savingDaily += trip.price - _priceTrip; } }); } var final = {}; final.goingDailyTotal = result.going.costDaily; final.goingDailySaving = result.going.savingDaily; final.returningDailyTotal = result.returning.costDaily; final.returningDailySaving = result.returning.savingDaily; final.savingMonthly = result.discounts.sube + result.discounts.social + result.discounts.subway; final.costMonthly = result.going.cost + result.returning.cost; final.goingTrip = result.going.trip; final.returningTrip = result.returning.trip; return final; } var createTrips = function (self, options) { var trips = []; var getPrices = false; var date = utils.formatDate(new Date()); if (options.date != null) { date = options.date; getPrices = true; } trips = trips.concat(createTrip(self, options, options.trips, 'going', date, getPrices)); if(options.roundtrip) { trips = trips.concat(createTrip(self, options, options.trips.reverse(), 'returning', date, getPrices)); } return trips; } var createTrip = function (self, options, trips, type, date, getPrices) { var _trips = []; var index = 1; var subeNetworkIndex = 1; trips.forEach(function (trip) { var _trip = { transport: null, index: null, type: type, price: null, subeBenefit: 1, socialBenefit: 1, date: date } _trip.transport = trip.transport _trip.index = index; if (getPrices) { _trip.price = self.transport.getPriceByTransportLineDateAndId(trip.transport, trip.line, date, trip.id); } else { _trip.price = trip.price; } //No benefit in subway if (options.benefit && trip.transport != 'subway') { _trip.socialBenefit = self.benefit.getSocialBenefit(trip.transport, date); } var dontApplySubeBenefit = (trip.transport == 'subway' && index > 1 && options.trips[index - 2].transport == 'subway') || subeNetworkIndex > 6; if (!dontApplySubeBenefit) { _trip.subeBenefit = self.benefit.getSubeBenefit(subeNetworkIndex, date); subeNetworkIndex++; } _trips[index - 1] = _trip; index++; }); return _trips; } module.exports = CalcTransporte; },{"./benefit":4,"./transport":6,"./utils":7,"lodash.defaults":8,"lodash.flowright":9,"lodash.foreach":10}],6:[function(require,module,exports){ 'use strict'; var defaultsValues = require('../data/default-values.json'); var defaults = require('lodash.defaults'); var flowRight = require('lodash.flowright'); var utils = require('./utils'); var clone = flowRight(JSON.parse, JSON.stringify); function Transport(values) { var self = this; values = defaults(clone(values || {}), defaultsValues); self.values = values } Transport.prototype.getTransports = function getTransports() { var self = this; return Object.keys(self.values); } Transport.prototype.getTrainLines = function getTrainLines() { var self = this; return Object.keys(self.values.train); } Transport.prototype.getPricesByDate = function getPricesByDate(date) { var self = this; var transports = self.getTransports(); var _result = {}; transports.forEach(function (transport) { self.getPricesByTransportAndDate(transport, date, _result); }); return _result; } Transport.prototype.getPricesByTransportAndDate = function getPricesByTransportAndDate(transport, date, _result) { var self = this; if (transport == 'train') { _result[transport] = {}; var lines = self.getTrainLines(); lines.forEach(function (line) { var _date = date; if (date === undefined || date == null) { _date = utils.formatDate(new Date()); } _date = utils.getClosestDate(_date, self.values[transport][line]); _result[transport][line] = { date: _date, values: null }; _result[transport][line].values = self.values[transport][line].find(function (element) { return element.date === _date; }).values; }); } else { var _date = date; if (date === undefined || date == null) { _date = utils.formatDate(new Date()); } _date = utils.getClosestDate(_date, self.values[transport]); _result[transport] = { date: _date, values: null }; _result[transport].values = self.values[transport].find(function (element) { return element.date === _date; }).values; } return _result; }; Transport.prototype.getPriceByTransportLineDateAndId = function getPriceByTransportLineDateAndId(transport, line, date, id) { var self = this; var transports = self.getTransports(); var _result = {}; self.getPricesByTransportAndDate(transport, date, _result); transports.forEach(function (transport) { self.getPricesByTransportAndDate(transport, date, _result); }); var data; if (transport == 'train') { data = _result[transport][line].values; } else { data = _result[transport].values; } var ids = data.map(function (o) { return o.id; }) ids.sort(utils.Comparator); var idToFind = utils.getNearest(ids, id); return data.find(function (element) { return element.id === idToFind; }).price; } module.exports = Transport; },{"../data/default-values.json":2,"./utils":7,"lodash.defaults":8,"lodash.flowright":9}],7:[function(require,module,exports){ "use strict"; var formatDate = function(date) { if (date == null) return null; var year = date.getFullYear(); var month = date.getMonth() + 1; if (month < 10) { month = "0" + month; } var day = date.getDate(); if (day < 10) { day = "0" + day; } return year + "-" + month + "-" + day; }; var Comparator = function(a, b) { if (a < b) return -1; if (a > b) return 1; return 0; }; var getClosestDate = function(date, data) { var dates = data.map(function(o) { return o.date; }); dates.sort(Comparator); return getNearest(dates, date); }; var getNearest = function(array, item) { var nearest; var BreakException = {}; try { array.forEach(function(x) { if (!nearest || item >= x) { nearest = x; } else { throw BreakException; } }); } catch (e) { if (e !== BreakException) throw e; } return nearest; }; module.exports = { formatDate: formatDate, Comparator: Comparator, getClosestDate: getClosestDate, getNearest: getNearest }; },{}],8:[function(require,module,exports){ /** * lodash (Custom Build) <https://lodash.com/> * Build: `lodash modularize exports="npm" -o ./` * Copyright jQuery Foundation and other contributors <https://jquery.org/> * Released under MIT license <https://lodash.com/license> * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE> * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors */ /** Used as references for various `Number` constants. */ var MAX_SAFE_INTEGER = 9007199254740991; /** `Object#toString` result references. */ var argsTag = '[object Arguments]', funcTag = '[object Function]', genTag = '[object GeneratorFunction]'; /** Used to detect unsigned integer values. */ var reIsUint = /^(?:0|[1-9]\d*)$/; /** * A faster alternative to `Function#apply`, this function invokes `func` * with the `this` binding of `thisArg` and the arguments of `args`. * * @private * @param {Function} func The function to invoke. * @param {*} thisArg The `this` binding of `func`. * @param {Array} args The arguments to invoke `func` with. * @returns {*} Returns the result of `func`. */ function apply(func, thisArg, args) { switch (args.length) { case 0: return func.call(thisArg); case 1: return func.call(thisArg, args[0]); case 2: return func.call(thisArg, args[0], args[1]); case 3: return func.call(thisArg, args[0], args[1], args[2]); } return func.apply(thisArg, args); } /** * The base implementation of `_.times` without support for iteratee shorthands * or max array length checks. * * @private * @param {number} n The number of times to invoke `iteratee`. * @param {Function} iteratee The function invoked per iteration. * @returns {Array} Returns the array of results. */ function baseTimes(n, iteratee) { var index = -1, result = Array(n); while (++index < n) { result[index] = iteratee(index); } return result; } /** Used for built-in method references. */ var objectProto = Object.prototype; /** Used to check objects for own properties. */ var hasOwnProperty = objectProto.hasOwnProperty; /** * Used to resolve the * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring) * of values. */ var objectToString = objectProto.toString; /** Built-in value references. */ var propertyIsEnumerable = objectProto.propertyIsEnumerable; /* Built-in method references for those with the same name as other `lodash` methods. */ var nativeMax = Math.max; /** * Creates an array of the enumerable property names of the array-like `value`. * * @private * @param {*} value The value to query. * @param {boolean} inherited Specify returning inherited property names. * @returns {Array} Returns the array of property names. */ function arrayLikeKeys(value, inherited) { // Safari 8.1 makes `arguments.callee` enumerable in strict mode. // Safari 9 makes `arguments.length` enumerable in strict mode. var result = (isArray(value) || isArguments(value)) ? baseTimes(value.length, String) : []; var length = result.length, skipIndexes = !!length; for (var key in value) { if ((inherited || hasOwnProperty.call(value, key)) && !(skipIndexes && (key == 'length' || isIndex(key, length)))) { result.push(key); } } return result; } /** * Used by `_.defaults` to customize its `_.assignIn` use. * * @private * @param {*} objValue The destination value. * @param {*} srcValue The source value. * @param {string} key The key of the property to assign. * @param {Object} object The parent object of `objValue`. * @returns {*} Returns the value to assign. */ function assignInDefaults(objValue, srcValue, key, object) { if (objValue === undefined || (eq(objValue, objectProto[key]) && !hasOwnProperty.call(object, key))) { return srcValue; } return objValue; } /** * Assigns `value` to `key` of `object` if the existing value is not equivalent * using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) * for equality comparisons. * * @private * @param {Object} object The object to modify. * @param {string} key The key of the property to assign. * @param {*} value The value to assign. */ function assignValue(object, key, value) { var objValue = object[key]; if (!(hasOwnProperty.call(object, key) && eq(objValue, value)) || (value === undefined && !(key in object))) { object[key] = value; } } /** * The base implementation of `_.keysIn` which doesn't treat sparse arrays as dense. * * @private * @param {Object} object The object to query. * @returns {Array} Returns the array of property names. */ function baseKeysIn(object) { if (!isObject(object)) { return nativeKeysIn(object); } var isProto = isPrototype(object), result = []; for (var key in object) { if (!(key == 'constructor' && (isProto || !hasOwnProperty.call(object, key)))) { result.push(key); } } return result; } /** * The base implementation of `_.rest` which doesn't validate or coerce arguments. * * @private * @param {Function} func The function to apply a rest parameter to. * @param {number} [start=func.length-1] The start position of the rest parameter. * @returns {Function} Returns the new function. */ function baseRest(func, start) { start = nativeMax(start === undefined ? (func.length - 1) : start, 0); return function() { var args = arguments, index = -1, length = nativeMax(args.length - start, 0), array = Array(length); while (++index < length) { array[index] = args[start + index]; } index = -1; var otherArgs = Array(start + 1); while (++index < start) { otherArgs[index] = args[index]; } otherArgs[start] = array; return apply(func, this, otherArgs); }; } /** * Copies properties of `source` to `object`. * * @private * @param {Object}