Advertisement
Guest User

Untitled

a guest
Aug 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {simpleParser} from 'mailparser';
  2. import * as fs from 'fs';
  3. import * as path from 'path';
  4.  
  5. fs.readFile(path.join('S:\\parse-mail\\9o5stq4nkd4miv0usn7d5s8pqcp6neije2mju081'), (err, data) => {
  6.     if (err) {
  7.         return console.error(err);
  8.     }
  9.     simpleParser(data)
  10.         .then(parsedData => {
  11.             const pricingInfo = getPricingInfo(parsedData.text)
  12.             console.log(pricingInfo);
  13.         })
  14.         .catch(parseError => {
  15.             console.error(parseError);
  16.         });
  17. })
  18.  
  19.  
  20. /**
  21.  * Returns an object with the pricing info for the day for OGE
  22.  *
  23.  * @param textFromEmail Input text from an OGE Email
  24.  */
  25. function getPricingInfo(textFromEmail: string) {
  26.     const centsRegex = /([0-9]+) cents/
  27.     const dateRegex = /([a-zA-Z]+, [0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9])/
  28.     const timeRegex = /([0-9][0-9]:[0-9][0-9] [a-zA-Z][a-zA-Z] - [0-9][0-9]:[0-9][0-9] [a-zA-Z][a-zA-Z])/
  29.     // Extract the cents per hour
  30.     const centsPerHour = Number(centsRegex.exec(textFromEmail)[1])
  31.  
  32.     // The date is in the format 'Wednesday, 08/22/2018'
  33.     const date = dateRegex.exec(textFromEmail)[1]
  34.  
  35.     // The time is in the format '02:00 PM - 07:00 PM'
  36.     // we want to break this down into the start and end time
  37.     const timeRange = timeRegex.exec(textFromEmail)[1]
  38.     const startAndEndTimes = timeRange.split(' - ');
  39.     const [startTime, endTime] = startAndEndTimes;
  40.  
  41.     // Create actual date objects for the start and end time, so we can use them programmatically
  42.     const startTimeAndDate = new Date(`${date}, ${startTime}`)
  43.     const endTimeAndDate = new Date(`${date}, ${endTime}`)
  44.  
  45.     return {
  46.         centsPerHour,
  47.         startTimeAndDate,
  48.         endTimeAndDate
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement