Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {simpleParser} from 'mailparser';
- import * as fs from 'fs';
- import * as path from 'path';
- fs.readFile(path.join('S:\\parse-mail\\9o5stq4nkd4miv0usn7d5s8pqcp6neije2mju081'), (err, data) => {
- if (err) {
- return console.error(err);
- }
- simpleParser(data)
- .then(parsedData => {
- const pricingInfo = getPricingInfo(parsedData.text)
- console.log(pricingInfo);
- })
- .catch(parseError => {
- console.error(parseError);
- });
- })
- /**
- * Returns an object with the pricing info for the day for OGE
- *
- * @param textFromEmail Input text from an OGE Email
- */
- function getPricingInfo(textFromEmail: string) {
- const centsRegex = /([0-9]+) cents/
- const dateRegex = /([a-zA-Z]+, [0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9])/
- 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])/
- // Extract the cents per hour
- const centsPerHour = Number(centsRegex.exec(textFromEmail)[1])
- // The date is in the format 'Wednesday, 08/22/2018'
- const date = dateRegex.exec(textFromEmail)[1]
- // The time is in the format '02:00 PM - 07:00 PM'
- // we want to break this down into the start and end time
- const timeRange = timeRegex.exec(textFromEmail)[1]
- const startAndEndTimes = timeRange.split(' - ');
- const [startTime, endTime] = startAndEndTimes;
- // Create actual date objects for the start and end time, so we can use them programmatically
- const startTimeAndDate = new Date(`${date}, ${startTime}`)
- const endTimeAndDate = new Date(`${date}, ${endTime}`)
- return {
- centsPerHour,
- startTimeAndDate,
- endTimeAndDate
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement