Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2.  
  3. const merge = require('lodash/merge');
  4.  
  5. const regionFormatter = require('./region-formatter');
  6. const path = (file) => `./gulpfile.js/util/delivery-dates/${file}`;
  7.  
  8. const isOneWordRegion = (region) => (['Москва', 'Санкт-Петербург'].includes(region));
  9. const isFewWordsCity = (city) => (['Набережные Челны', 'Нижний Новгород'].includes(city));
  10.  
  11. const parsePage = (page, pageNumber) => {
  12.     const countFrom = page.length - 1;
  13.     const countTo = page[1].match(/(\d\s?)*$/)[0].split(' ').length;
  14.     const toRegions = [];
  15.     const toCities = [];
  16.  
  17.     const isRegionsNotFilled = () => toRegions.length < countTo;
  18.     const isCitiesNotFilled = () => toCities.length < countTo;
  19.  
  20.     const tos = page[0].split(' ');
  21.  
  22.     let isSkip = false;
  23.  
  24.     tos.forEach((to, index) => {
  25.         if (isSkip) {
  26.             isSkip = false;
  27.             return;
  28.         }
  29.  
  30.         if (isRegionsNotFilled()) {
  31.             if (isOneWordRegion(to)) {
  32.                 toRegions.push(to);
  33.             } else {
  34.                 toRegions.push(`${to} ${tos[index + 1]}`);
  35.                 isSkip = true;
  36.             }
  37.         } else if (isCitiesNotFilled()) {
  38.             const tryFewWordsCity = `${to} ${tos[index + 1]}`;
  39.  
  40.             if (isFewWordsCity(tryFewWordsCity)) {
  41.                 toCities.push(tryFewWordsCity);
  42.                 isSkip = true;
  43.             } else {
  44.                 toCities.push(to);
  45.             }
  46.         }
  47.     });
  48.  
  49.     const returnTest = [...toRegions, ...toCities].join(' ');
  50.     if (returnTest !== page[0]) {
  51.         console.warn(page[0]);
  52.         console.warn(returnTest);
  53.         console.warn('Regions');
  54.         console.warn(...toRegions);
  55.         console.warn('Cities');
  56.         console.warn(...toCities);
  57.  
  58.         throw new Error();
  59.     }
  60.  
  61.     if (toRegions.length !== countTo || toCities.length !== countTo) {
  62.         throw new Error();
  63.     }
  64.  
  65.     const table = {};
  66.  
  67.     for(let i = 1; i <= countFrom; i++) {
  68.         const matches = page[i].match(/^(?:\d+\s)([а-я0-9\-\s]*?)([\d\s]*)$/i);
  69.         if (!matches || !matches[1] || !matches[2]) {
  70.             console.warn('NO MATCHES', page[i]);
  71.             return;
  72.         }
  73.  
  74.         const [, from, daysStr] = matches;
  75.  
  76.         const days = daysStr.split(' ').filter(raw => raw !== '');
  77.  
  78.         let isSkip = false;
  79.  
  80.         const fromArr = from.split(' ');
  81.         let fromRegion = '';
  82.         let fromCity = '';
  83.  
  84.         fromArr.forEach((fromEntity, index) => {
  85.             if (isSkip) {
  86.                 isSkip = false;
  87.                 return;
  88.             }
  89.  
  90.             if (!fromRegion) {
  91.                 if (isOneWordRegion(fromEntity)) {
  92.                     fromRegion = fromEntity;
  93.                 } else {
  94.                     fromRegion = `${fromEntity} ${fromArr[index + 1]}`;
  95.                     isSkip = true;
  96.                 }
  97.             } else if (!fromCity) {
  98.                 const tryFewWordsCity = `${fromEntity} ${fromArr[index + 1]}`;
  99.  
  100.                 if (isFewWordsCity(tryFewWordsCity)) {
  101.                     fromCity = tryFewWordsCity;
  102.                     isSkip = true;
  103.                 } else {
  104.                     fromCity = fromEntity;
  105.                 }
  106.             }
  107.         });
  108.  
  109.         if (!(fromRegion in table)) {
  110.             table[fromRegion] = {};
  111.         }
  112.  
  113.         if (!(fromCity in table[fromRegion])) {
  114.             table[fromRegion][fromCity] = {};
  115.         }
  116.  
  117.         // from_region->from_city->to_region->to_city->[min, max]
  118.  
  119.         days.forEach((day, index) => {
  120.             const toRegion = toRegions[index];
  121.             const toCity = toCities[index];
  122.  
  123.             if (!(toRegion in table[fromRegion][fromCity])) {
  124.                 table[fromRegion][fromCity][toRegion] = {};
  125.             }
  126.  
  127.             if (!(toCity in table[fromRegion][fromCity][toRegion])) {
  128.                 table[fromRegion][fromCity][toRegion][toCity] = [];
  129.             }
  130.  
  131.             if (+day && !isNaN(+day)) {
  132.                 table[fromRegion][fromCity][toRegion][toCity].push(+day);
  133.                 table[fromRegion][fromCity][toRegion][toCity] = table[fromRegion][fromCity][toRegion][toCity].sort();
  134.             }
  135.         });
  136.     }
  137.  
  138.  
  139.     if (!table) {
  140.         throw new Error(pageNumber);
  141.     }
  142.  
  143.     return table;
  144. };
  145.  
  146. const parse = data => {
  147.     const rows = data.split('\n');
  148.     const pages = [];
  149.     let tempPage = [];
  150.     rows.forEach((row, index) => {
  151.         if (row.startsWith('Мин Макс')) {
  152.             tempPage.push(rows[index - 1]);
  153.         } else if (tempPage.length !== 0) {
  154.             if (/^([0-9]+)\s([а-я]+)/i.test(row)) {
  155.                 tempPage.push(row);
  156.             } else {
  157.                 pages.push(tempPage);
  158.                 tempPage = [];
  159.             }
  160.         }
  161.     });
  162.  
  163.     return pages.map(parsePage);
  164. };
  165.  
  166. const DeliveryDates = () => new Promise(resolve => {
  167.     fs.readFile(path(`pdf-content`), 'utf8', (_, data) => {
  168.         const parsed = parse(data);
  169.         const tableNamed = merge(...parsed);
  170.  
  171.         const table = {};
  172.         Object.entries(tableNamed).forEach(([fromRegionName, data]) => {
  173.             const fromRegionCode = regionFormatter(fromRegionName);
  174.  
  175.             if (!(fromRegionCode in table)) {
  176.                 table[fromRegionCode] = {};
  177.             }
  178.  
  179.             Object.entries(data).forEach(([fromCityName, _data]) => {
  180.                 if (!(fromCityName in table[fromRegionCode])) {
  181.                     table[fromRegionCode][fromCityName] = {};
  182.                 }
  183.  
  184.                 Object.entries(_data).forEach(([toRegionName, __data]) => {
  185.                     const toRegionCode = regionFormatter(toRegionName);
  186.  
  187.                     table[fromRegionCode][fromCityName][toRegionCode] = __data;
  188.                 });
  189.             });
  190.         });
  191.  
  192.         fs.writeFileSync('../lib/config/data/delivery_interval_handbook.json', JSON.stringify(table));
  193.  
  194.         resolve();
  195.     });
  196. });
  197.  
  198. module.exports = DeliveryDates;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement