WME Utils - HoursParser Beta

Parses a text string into hours, for use in Waze Map Editor scripts

目前为 2018-02-07 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/38249/249539/WME%20Utils%20-%20HoursParser%20Beta.js

  1. // ==UserScript==
  2. // @name WME Utils - HoursParser Beta
  3. // @namespace WazeDev
  4. // @version 2018.01.15.005
  5. // @description Parses a text string into hours, for use in Waze Map Editor scripts
  6. // @author MapOMatic (originally developed by bmtg)
  7. // @license GNU GPLv3
  8. // ==/UserScript==
  9.  
  10. class HoursParser {
  11. var I18nDate = I18n.translations[I18n.currentLocale()].date;
  12. this.DAYS_OF_THE_WEEK = {
  13. SS: ['saturdays', I18nDate.day_names[6].toLowerCase(), 'satur', I18nDate.abbr_day_names[6].toLowerCase(), 'sa'],
  14. UU: ['sundays', I18nDate.day_names[0].toLowerCase(), I18nDate.abbr_day_names[0].toLowerCase(), 'su'],
  15. MM: ['mondays', I18nDate.day_names[1].toLowerCase(), 'mondy', I18nDate.abbr_day_names[1].toLowerCase(), 'mo'],
  16. TT: ['tuesdays', I18nDate.day_names[2].toLowerCase(), 'tues', I18nDate.abbr_day_names[2].toLowerCase(), 'tu'],
  17. WW: ['wednesdays', I18nDate.day_names[3].toLowerCase(), 'weds', I18nDate.abbr_day_names[3].toLowerCase(), 'we'],
  18. RR: ['thursdays', I18nDate.day_names[4].toLowerCase(), 'thurs', 'thur', I18nDate.abbr_day_names[4].toLowerCase(), 'th'],
  19. FF: ['fridays', I18nDate.day_names[5].toLowerCase(), I18nDate.abbr_day_names[5].toLowerCase(), 'fr']
  20. };
  21. this.MONTHS_OF_THE_YEAR = {
  22. JAN: [I18nDate.month_names[1].toLowerCase(), I18nDate.abbr_month_names[1].toLowerCase()],
  23. FEB: [I18nDate.month_names[2].toLowerCase(), 'febr', I18nDate.abbr_month_names[2].toLowerCase()],
  24. MAR: [I18nDate.month_names[3].toLowerCase(), I18nDate.abbr_month_names[3].toLowerCase()],
  25. APR: [I18nDate.month_names[4].toLowerCase(), I18nDate.abbr_month_names[4].toLowerCase()],
  26. MAY: [I18nDate.month_names[5].toLowerCase(), I18nDate.abbr_month_names[5].toLowerCase()],
  27. JUN: [I18nDate.month_names[6].toLowerCase(), I18nDate.abbr_month_names[6].toLowerCase()],
  28. JUL: [I18nDate.month_names[7].toLowerCase(), I18nDate.abbr_month_names[7].toLowerCase()],
  29. AUG: [I18nDate.month_names[8].toLowerCase(), I18nDate.abbr_month_names[8].toLowerCase()],
  30. SEP: [I18nDate.month_names[9].toLowerCase(), 'sept', I18nDate.abbr_month_names[9].toLowerCase()],
  31. OCT: [I18nDate.month_names[10].toLowerCase(), I18nDate.abbr_month_names[10].toLowerCase()],
  32. NOV: [I18nDate.month_names[11].toLowerCase(), I18nDate.abbr_month_names[11].toLowerCase()],
  33. DEC: [I18nDate.month_names[12].toLowerCase(), I18nDate.abbr_month_names[12].toLowerCase()]
  34. };
  35. debugger;
  36. this.DAY_CODE_VECTOR = ['MM','TT','WW','RR','FF','SS','UU','MM','TT','WW','RR','FF','SS','UU','MM','TT','WW','RR','FF'];
  37. this.THRU_WORDS = ['through','thru','to','until','till','til','-','~'];
  38. }
  39.  
  40. parseHours(inputHours, locale) {
  41. let returnVal = {
  42. hours: [],
  43. parseError: false,
  44. overlappingHours: false,
  45. sameOpenAndCloseTimes: false
  46. };
  47.  
  48. let tfHourTemp, tfDaysTemp, newDayCodeVec = [];
  49. let tempRegex, twix, tsix;
  50. let inputHoursParse = inputHours.toLowerCase().trim();
  51. if (inputHoursParse.length === 0 || inputHoursParse === ',') {
  52. return returnVal;
  53. }
  54. let today = new Date();
  55. let tomorrow = new Date();
  56. tomorrow.setDate(tomorrow.getDate() + 1);
  57. inputHoursParse = inputHoursParse.replace(/\btoday\b/g, today.toLocaleDateString(locale, {weekday:'short'}).toLowerCase())
  58. .replace(/\btomorrow\b/g, tomorrow.toLocaleDateString(locale, {weekday:'short'}).toLowerCase())
  59. .replace(/\u2013|\u2014/g, "-") // long dash replacing
  60. .replace(/[^a-z0-9\:\-\. ~]/g, ' ') // replace unnecessary characters with spaces
  61. .replace(/\:{2,}/g, ':') // remove extra colons
  62. .replace(/closed|not open/g, '99:99-99:99') // parse 'closed'
  63. .replace(/by appointment( only)?/g, '99:99-99:99') // parse 'appointment only'
  64. .replace(/weekdays/g, 'mon-fri').replace(/weekends/g, 'sat-sun') // convert weekdays and weekends to days
  65. .replace(/(12(:00)?\W*)?noon/g, "12:00").replace(/(12(:00)?\W*)?mid(night|nite)/g, "00:00") // replace 'noon', 'midnight'
  66. .replace(/every\s*day|daily|(7|seven) days a week/g, "mon-sun") // replace 'seven days a week'
  67. .replace(/(open\s*)?(24|twenty\W*four)\W*h(ou)?rs?|all day/g, "00:00-00:00") // replace 'open 24 hour or similar'
  68. .replace(/(\D:)([^ ])/g, "$1 $2"); // space after colons after words
  69.  
  70. // replace thru type words with dashes
  71. this.THRU_WORDS.forEach(word => {
  72. inputHoursParse = inputHoursParse.replace( new RegExp(word, 'g'), '-');
  73. });
  74.  
  75. inputHoursParse = inputHoursParse.replace(/\-{2,}/g, "-"); // replace any duplicate dashes
  76.  
  77. // kill extra words
  78. let killWords = 'paste|here|business|operation|times|time|walk-ins|walk ins|welcome|dinner|lunch|brunch|breakfast|regular|weekday|weekend|opening|open|now|from|hours|hour|our|are|EST|and|&'.split("|");
  79. for (twix=0; twix<killWords.length; twix++) {
  80. tempRegex = new RegExp('\\b'+killWords[twix]+'\\b', "g");
  81. inputHoursParse = inputHoursParse.replace(tempRegex,'');
  82. }
  83.  
  84. // replace day terms with double caps
  85. for (let dayKey in this.DAYS_OF_THE_WEEK) {
  86. if (this.DAYS_OF_THE_WEEK.hasOwnProperty(dayKey)) {
  87. let tempDayList = this.DAYS_OF_THE_WEEK[dayKey];
  88. for (var tdix=0; tdix<tempDayList.length; tdix++) {
  89. tempRegex = new RegExp(tempDayList[tdix]+'(?!a-z)', "g");
  90. inputHoursParse = inputHoursParse.replace(tempRegex,dayKey);
  91. }
  92. }
  93. }
  94.  
  95. // Replace dates
  96. for (let monthKey in this.MONTHS_OF_THE_YEAR) {
  97. if (this.MONTHS_OF_THE_YEAR.hasOwnProperty(monthKey)) {
  98. let tempMonthList = this.MONTHS_OF_THE_YEAR[monthKey];
  99. for (var tmix=0; tmix<tempMonthList.length; tmix++) {
  100. tempRegex = new RegExp(tempMonthList[tmix]+'\\.? ?\\d{1,2}\\,? ?201\\d{1}', "g");
  101. inputHoursParse = inputHoursParse.replace(tempRegex,' ');
  102. tempRegex = new RegExp(tempMonthList[tmix]+'\\.? ?\\d{1,2}', "g");
  103. inputHoursParse = inputHoursParse.replace(tempRegex,' ');
  104. }
  105. }
  106. }
  107.  
  108. // replace any periods between hours with colons
  109. inputHoursParse = inputHoursParse.replace(/(\d{1,2})\.(\d{2})/g, '$1:$2');
  110. // remove remaining periods
  111. inputHoursParse = inputHoursParse.replace(/\./g, '');
  112. // remove any non-hour colons between letters and numbers and on string ends
  113. inputHoursParse = inputHoursParse.replace(/(\D+)\:(\D+)/g, '$1 $2').replace(/^ *\:/g, ' ').replace(/\: *$/g, ' ');
  114. // replace am/pm with AA/PP
  115. inputHoursParse = inputHoursParse.replace(/ *pm/g,'PP').replace(/ *am/g,'AA');
  116. inputHoursParse = inputHoursParse.replace(/ *p\.m\./g,'PP').replace(/ *a\.m\./g,'AA');
  117. inputHoursParse = inputHoursParse.replace(/ *p\.m/g,'PP').replace(/ *a\.m/g,'AA');
  118. inputHoursParse = inputHoursParse.replace(/ *p/g,'PP').replace(/ *a/g,'AA');
  119. // tighten up dashes
  120. inputHoursParse = inputHoursParse.replace(/\- {1,}/g,'-').replace(/ {1,}\-/g,'-');
  121. inputHoursParse = inputHoursParse.replace(/^(00:00-00:00)$/g,'MM-UU$1');
  122.  
  123. // Change all MTWRFSU to doubles, if any other letters return false
  124. if (inputHoursParse.match(/[bcdeghijklnoqvxyz]/g) !== null) {
  125. returnVal.parseError = true;
  126. return returnVal;
  127. } else {
  128. inputHoursParse = inputHoursParse.replace(/m/g,'MM').replace(/t/g,'TT').replace(/w/g,'WW').replace(/r/g,'RR');
  129. inputHoursParse = inputHoursParse.replace(/f/g,'FF').replace(/s/g,'SS').replace(/u/g,'UU');
  130. }
  131.  
  132. // tighten up spaces
  133. inputHoursParse = inputHoursParse.replace(/ {2,}/g,' ');
  134. inputHoursParse = inputHoursParse.replace(/ {1,}AA/g,'AA');
  135. inputHoursParse = inputHoursParse.replace(/ {1,}PP/g,'PP');
  136. // Expand hours into XX:XX format
  137. for (var asdf=0; asdf<5; asdf++) { // repeat a few times to catch any skipped regex matches
  138. inputHoursParse = inputHoursParse.replace(/([^0-9\:])(\d{1})([^0-9\:])/g, '$10$2:00$3');
  139. inputHoursParse = inputHoursParse.replace(/^(\d{1})([^0-9\:])/g, '0$1:00$2');
  140. inputHoursParse = inputHoursParse.replace(/([^0-9\:])(\d{1})$/g, '$10$2:00');
  141.  
  142. inputHoursParse = inputHoursParse.replace(/([^0-9\:])(\d{2})([^0-9\:])/g, '$1$2:00$3');
  143. inputHoursParse = inputHoursParse.replace(/^(\d{2})([^0-9\:])/g, '$1:00$2');
  144. inputHoursParse = inputHoursParse.replace(/([^0-9\:])(\d{2})$/g, '$1$2:00');
  145.  
  146. inputHoursParse = inputHoursParse.replace(/(\D)(\d{1})(\d{2}\D)/g, '$10$2:$3');
  147. inputHoursParse = inputHoursParse.replace(/^(\d{1})(\d{2}\D)/g, '0$1:$2');
  148. inputHoursParse = inputHoursParse.replace(/(\D)(\d{1})(\d{2})$/g, '$10$2:$3');
  149.  
  150. inputHoursParse = inputHoursParse.replace(/(\D\d{2})(\d{2}\D)/g, '$1:$2');
  151. inputHoursParse = inputHoursParse.replace(/^(\d{2})(\d{2}\D)/g, '$1:$2');
  152. inputHoursParse = inputHoursParse.replace(/(\D\d{2})(\d{2})$/g, '$1:$2');
  153.  
  154. inputHoursParse = inputHoursParse.replace(/(\D)(\d{1}\:)/g, '$10$2');
  155. inputHoursParse = inputHoursParse.replace(/^(\d{1}\:)/g, '0$1');
  156. }
  157.  
  158. // replace 12AM range with 00
  159. inputHoursParse = inputHoursParse.replace( /12(\:\d{2}AA)/g, '00$1');
  160. // Change PM hours to 24hr time
  161. while (inputHoursParse.match(/\d{2}\:\d{2}PP/) !== null) {
  162. tfHourTemp = inputHoursParse.match(/(\d{2})\:\d{2}PP/)[1];
  163. tfHourTemp = parseInt(tfHourTemp) % 12 + 12;
  164. inputHoursParse = inputHoursParse.replace(/\d{2}(\:\d{2})PP/,tfHourTemp.toString()+'$1');
  165. }
  166. // kill the AA
  167. inputHoursParse = inputHoursParse.replace( /AA/g, '');
  168.  
  169. // Side check for tabular input
  170. var inputHoursParseTab = inputHoursParse.replace( /[^A-Z0-9\:-]/g, ' ').replace( / {2,}/g, ' ');
  171. inputHoursParseTab = inputHoursParseTab.replace( /^ +/g, '').replace( / {1,}$/g, '');
  172. if (inputHoursParseTab.match(/[A-Z]{2}\:?\-? [A-Z]{2}\:?\-? [A-Z]{2}\:?\-? [A-Z]{2}\:?\-? [A-Z]{2}\:?\-?/g) !== null) {
  173. inputHoursParseTab = inputHoursParseTab.split(' ');
  174. var reorderThree = [0,7,14,1,8,15,2,9,16,3,10,17,4,11,18,5,12,19,6,13,20];
  175. var reorderTwo = [0,7,1,8,2,9,3,10,4,11,5,12,6,13];
  176. var inputHoursParseReorder = [], reix;
  177. if (inputHoursParseTab.length === 21) {
  178. for (reix=0; reix<21; reix++) {
  179. inputHoursParseReorder.push(inputHoursParseTab[reorderThree[reix]]);
  180. }
  181. } else if (inputHoursParseTab.length === 18) {
  182. for (reix=0; reix<18; reix++) {
  183. inputHoursParseReorder.push(inputHoursParseTab[reorderThree[reix]]);
  184. }
  185. } else if (inputHoursParseTab.length === 15) {
  186. for (reix=0; reix<15; reix++) {
  187. inputHoursParseReorder.push(inputHoursParseTab[reorderThree[reix]]);
  188. }
  189. } else if (inputHoursParseTab.length === 14) {
  190. for (reix=0; reix<14; reix++) {
  191. inputHoursParseReorder.push(inputHoursParseTab[reorderTwo[reix]]);
  192. }
  193. } else if (inputHoursParseTab.length === 12) {
  194. for (reix=0; reix<12; reix++) {
  195. inputHoursParseReorder.push(inputHoursParseTab[reorderTwo[reix]]);
  196. }
  197. } else if (inputHoursParseTab.length === 10) {
  198. for (reix=0; reix<10; reix++) {
  199. inputHoursParseReorder.push(inputHoursParseTab[reorderTwo[reix]]);
  200. }
  201. }
  202.  
  203. if (inputHoursParseReorder.length > 9) {
  204. inputHoursParseReorder = inputHoursParseReorder.join(' ');
  205. inputHoursParseReorder = inputHoursParseReorder.replace(/(\:\d{2}) (\d{2}\:)/g, '$1-$2');
  206. inputHoursParse = inputHoursParseReorder;
  207. }
  208.  
  209. }
  210.  
  211.  
  212. // remove colons after Days field
  213. inputHoursParse = inputHoursParse.replace(/(\D+)\:/g, '$1 ');
  214.  
  215. // Find any double sets
  216. inputHoursParse = inputHoursParse.replace(/([A-Z \-]{2,}) *(\d{2}\:\d{2} *\-{1} *\d{2}\:\d{2}) *(\d{2}\:\d{2} *\-{1} *\d{2}\:\d{2})/g, '$1$2$1$3');
  217. inputHoursParse = inputHoursParse.replace(/(\d{2}\:\d{2}) *(\d{2}\:\d{2})/g, '$1-$2');
  218.  
  219. // remove all spaces
  220. inputHoursParse = inputHoursParse.replace( / */g, '');
  221.  
  222. // Remove any dashes acting as Day separators for 3+ days ("M-W-F")
  223. inputHoursParse = inputHoursParse.replace( /([A-Z]{2})-([A-Z]{2})-([A-Z]{2})-([A-Z]{2})-([A-Z]{2})-([A-Z]{2})-([A-Z]{2})/g, '$1$2$3$4$5$6$7');
  224. inputHoursParse = inputHoursParse.replace( /([A-Z]{2})-([A-Z]{2})-([A-Z]{2})-([A-Z]{2})-([A-Z]{2})-([A-Z]{2})/g, '$1$2$3$4$5$6');
  225. inputHoursParse = inputHoursParse.replace( /([A-Z]{2})-([A-Z]{2})-([A-Z]{2})-([A-Z]{2})-([A-Z]{2})/g, '$1$2$3$4$5');
  226. inputHoursParse = inputHoursParse.replace( /([A-Z]{2})-([A-Z]{2})-([A-Z]{2})-([A-Z]{2})/g, '$1$2$3$4');
  227. inputHoursParse = inputHoursParse.replace( /([A-Z]{2})-([A-Z]{2})-([A-Z]{2})/g, '$1$2$3');
  228.  
  229. // parse any 'through' type terms on the day ranges (MM-RR --> MMTTWWRR)
  230. while (inputHoursParse.match(/[A-Z]{2}\-[A-Z]{2}/) !== null) {
  231. tfDaysTemp = inputHoursParse.match(/([A-Z]{2})\-([A-Z]{2})/);
  232. var startDayIX = this.DAY_CODE_VECTOR.indexOf(tfDaysTemp[1]);
  233. newDayCodeVec = [tfDaysTemp[1]];
  234. for (var dcvix=startDayIX+1; dcvix<startDayIX+7; dcvix++) {
  235. newDayCodeVec.push(this.DAY_CODE_VECTOR[dcvix]);
  236. if (tfDaysTemp[2] === this.DAY_CODE_VECTOR[dcvix]) {
  237. break;
  238. }
  239. }
  240. newDayCodeVec = newDayCodeVec.join('');
  241. inputHoursParse = inputHoursParse.replace(/[A-Z]{2}\-[A-Z]{2}/,newDayCodeVec);
  242. }
  243.  
  244. // split the string between numerical and letter characters
  245. inputHoursParse = inputHoursParse.replace(/([A-Z])\-?\:?([0-9])/g,'$1|$2');
  246. inputHoursParse = inputHoursParse.replace(/([0-9])\-?\:?([A-Z])/g,'$1|$2');
  247. inputHoursParse = inputHoursParse.replace(/(\d{2}\:\d{2})\:00/g,'$1'); // remove seconds
  248. inputHoursParse = inputHoursParse.split("|");
  249.  
  250. var daysVec = [], hoursVec = [];
  251. for (tsix=0; tsix<inputHoursParse.length; tsix++) {
  252. if (inputHoursParse[tsix][0].match(/[A-Z]/) !== null) {
  253. daysVec.push(inputHoursParse[tsix]);
  254. } else if (inputHoursParse[tsix][0].match(/[0-9]/) !== null) {
  255. hoursVec.push(inputHoursParse[tsix]);
  256. } else {
  257. returnVal.parseError = true;
  258. return returnVal;
  259. }
  260. }
  261.  
  262. // check that the dayArray and hourArray lengths correspond
  263. if ( daysVec.length !== hoursVec.length ) {
  264. returnVal.parseError = true;
  265. return returnVal;
  266. }
  267.  
  268. // Combine days with the same hours in the same vector
  269. var newDaysVec = [], newHoursVec = [], hrsIX;
  270. for (tsix=0; tsix<daysVec.length; tsix++) {
  271. if (hoursVec[tsix] !== '99:99-99:99') { // Don't add the closed days
  272. hrsIX = newHoursVec.indexOf(hoursVec[tsix]);
  273. if (hrsIX > -1) {
  274. newDaysVec[hrsIX] = newDaysVec[hrsIX] + daysVec[tsix];
  275. } else {
  276. newDaysVec.push(daysVec[tsix]);
  277. newHoursVec.push(hoursVec[tsix]);
  278. }
  279. }
  280. }
  281.  
  282. var hoursObjectArray = [], hoursObjectArrayMinDay = [], hoursObjectArraySorted = [], hoursObjectAdd, daysObjArray, toFromSplit;
  283. for (tsix=0; tsix<newDaysVec.length; tsix++) {
  284. hoursObjectAdd = {};
  285. daysObjArray = [];
  286. toFromSplit = newHoursVec[tsix].match(/(\d{2}\:\d{2})\-(\d{2}\:\d{2})/);
  287. if (toFromSplit === null) {
  288. returnVal.parseError = true;
  289. return returnVal;
  290. } else { // Check for hours outside of 0-23 and 0-59
  291. var hourCheck = toFromSplit[1].match(/(\d{2})\:/)[1];
  292. if (hourCheck>23 || hourCheck < 0) {
  293. returnVal.parseError = true;
  294. return returnVal;
  295. }
  296. hourCheck = toFromSplit[2].match(/(\d{2})\:/)[1];
  297. if (hourCheck>23 || hourCheck < 0) {
  298. returnVal.parseError = true;
  299. return returnVal;
  300. }
  301. hourCheck = toFromSplit[1].match(/\:(\d{2})/)[1];
  302. if (hourCheck>59 || hourCheck < 0) {
  303. returnVal.parseError = true;
  304. return returnVal;
  305. }
  306. hourCheck = toFromSplit[2].match(/\:(\d{2})/)[1];
  307. if (hourCheck>59 || hourCheck < 0) {
  308. returnVal.parseError = true;
  309. return returnVal;
  310. }
  311. }
  312. // Make the days object
  313. if ( newDaysVec[tsix].indexOf('MM') > -1 ) {
  314. daysObjArray.push(1);
  315. }
  316. if ( newDaysVec[tsix].indexOf('TT') > -1 ) {
  317. daysObjArray.push(2);
  318. }
  319. if ( newDaysVec[tsix].indexOf('WW') > -1 ) {
  320. daysObjArray.push(3);
  321. }
  322. if ( newDaysVec[tsix].indexOf('RR') > -1 ) {
  323. daysObjArray.push(4);
  324. }
  325. if ( newDaysVec[tsix].indexOf('FF') > -1 ) {
  326. daysObjArray.push(5);
  327. }
  328. if ( newDaysVec[tsix].indexOf('SS') > -1 ) {
  329. daysObjArray.push(6);
  330. }
  331. if ( newDaysVec[tsix].indexOf('UU') > -1 ) {
  332. daysObjArray.push(0);
  333. }
  334. // build the hours object
  335. hoursObjectAdd.fromHour = toFromSplit[1];
  336. hoursObjectAdd.toHour = toFromSplit[2];
  337. hoursObjectAdd.days = daysObjArray.sort();
  338. hoursObjectArray.push(hoursObjectAdd);
  339. // track the order
  340. if (hoursObjectAdd.days.length > 1 && hoursObjectAdd.days[0] === 0) {
  341. hoursObjectArrayMinDay.push( hoursObjectAdd.days[1] * 100 + parseInt(toFromSplit[1][0])*10 + parseInt(toFromSplit[1][1]) );
  342. } else {
  343. hoursObjectArrayMinDay.push( (((hoursObjectAdd.days[0]+6)%7)+1) * 100 + parseInt(toFromSplit[1][0])*10 + parseInt(toFromSplit[1][1]) );
  344. }
  345. }
  346. this._sortWithIndex(hoursObjectArrayMinDay);
  347. for (var hoaix=0; hoaix < hoursObjectArrayMinDay.length; hoaix++) {
  348. hoursObjectArraySorted.push(hoursObjectArray[hoursObjectArrayMinDay.sortIndices[hoaix]]);
  349. }
  350. if ( !this._checkHours(hoursObjectArraySorted) ) {
  351. returnVal.hours = hoursObjectArraySorted;
  352. returnVal.overlappingHours = true;
  353. return returnVal;
  354. } else if ( this._hasSameOpenCloseTimes(hoursObjectArraySorted) ) {
  355. returnVal.hours = hoursObjectArraySorted;
  356. returnVal.sameOpenAndCloseTimes = true;
  357. return returnVal;
  358. } else {
  359. for ( var ohix=0; ohix<hoursObjectArraySorted.length; ohix++ ) {
  360. if ( hoursObjectArraySorted[ohix].days.length === 2 && hoursObjectArraySorted[ohix].days[0] === 0 && hoursObjectArraySorted[ohix].days[1] === 1) {
  361. // separate hours
  362. hoursObjectArraySorted.push({days: [0], fromHour: hoursObjectArraySorted[ohix].fromHour, toHour: hoursObjectArraySorted[ohix].toHour});
  363. hoursObjectArraySorted[ohix].days = [1];
  364. }
  365. }
  366. }
  367. returnVal.hours = hoursObjectArray;
  368. return returnVal;
  369. }
  370.  
  371. // function to check overlapping hours
  372. _checkHours(hoursObj) {
  373. if (hoursObj.length === 1) {
  374. return true;
  375. }
  376. var daysObj, fromHourTemp, toHourTemp;
  377. for (var day2Ch=0; day2Ch<7; day2Ch++) { // Go thru each day of the week
  378. daysObj = [];
  379. for ( var hourSet = 0; hourSet < hoursObj.length; hourSet++ ) { // For each set of hours
  380. if (hoursObj[hourSet].days.indexOf(day2Ch) > -1) { // pull out hours that are for the current day, add 2400 if it goes past midnight, and store
  381. fromHourTemp = hoursObj[hourSet].fromHour.replace(/\:/g,'');
  382. toHourTemp = hoursObj[hourSet].toHour.replace(/\:/g,'');
  383. if (toHourTemp <= fromHourTemp) {
  384. toHourTemp = parseInt(toHourTemp) + 2400;
  385. }
  386. daysObj.push([fromHourTemp, toHourTemp]);
  387. }
  388. }
  389. if (daysObj.length > 1) { // If there's multiple hours for the day, check them for overlap
  390. for ( var hourSetCheck2 = 1; hourSetCheck2 < daysObj.length; hourSetCheck2++ ) {
  391. for ( var hourSetCheck1 = 0; hourSetCheck1 < hourSetCheck2; hourSetCheck1++ ) {
  392. if ( daysObj[hourSetCheck2][0] > daysObj[hourSetCheck1][0] && daysObj[hourSetCheck2][0] < daysObj[hourSetCheck1][1] ) {
  393. return false;
  394. }
  395. if ( daysObj[hourSetCheck2][1] > daysObj[hourSetCheck1][0] && daysObj[hourSetCheck2][1] < daysObj[hourSetCheck1][1] ) {
  396. return false;
  397. }
  398. }
  399. }
  400. }
  401. }
  402. return true;
  403. }
  404.  
  405. _hasSameOpenCloseTimes(hoursObj) {
  406. var fromHourTemp, toHourTemp;
  407. for ( var hourSet = 0; hourSet < hoursObj.length; hourSet++ ) { // For each set of hours
  408. fromHourTemp = hoursObj[hourSet].fromHour;
  409. toHourTemp = hoursObj[hourSet].toHour;
  410. if (fromHourTemp !== '00:00' && fromHourTemp === toHourTemp) {
  411. // If open and close times are the same, don't parse.
  412. return true;
  413. }
  414. }
  415. return false;
  416. }
  417.  
  418. _sortWithIndex(toSort) {
  419. for (var i = 0; i < toSort.length; i++) {
  420. toSort[i] = [toSort[i], i];
  421. }
  422. toSort.sort(function(left, right) {
  423. return left[0] < right[0] ? -1 : 1;
  424. });
  425. toSort.sortIndices = [];
  426. for (var j = 0; j < toSort.length; j++) {
  427. toSort.sortIndices.push(toSort[j][1]);
  428. toSort[j] = toSort[j][0];
  429. }
  430. return toSort;
  431. }
  432. // delete this later...
  433. _unused() {}
  434. }