Last active
September 28, 2018 08:07
-
-
Save sandiprb/18d12c8c7c51cc2b15e29f56a34dbf05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const util = require('util'); | |
const readFile = util.promisify(fs.readFile); | |
async function getFileLines() { | |
try { | |
const args = process.argv.slice(2) | |
if (!args.length) { | |
console.warn('>> Please pass file name as argument <<') | |
return | |
} | |
const fileName = args[0] | |
const fileContent = await readFile(`${fileName}`, 'utf-8'); | |
const lines = fileContent.split(/\r?\n/) | |
return lines | |
} catch (error) { | |
console.log(error) | |
} | |
} | |
async function main() { | |
let emplyeesLeaveCounts = {} | |
const publicHolidays = ['20180321'] | |
const fileLines = await getFileLines() | |
fileLines.forEach(line => { | |
let row = line.split(' '); | |
row = row.filter(item => !!item); | |
const emplyeeName = row[0]; | |
if (Object.keys(emplyeesLeaveCounts).indexOf(emplyeeName) === -1) { | |
emplyeesLeaveCounts[emplyeeName] = 0 | |
} | |
const dates = row.slice(1) | |
dates.forEach((date) => { | |
const isPublicHoliday = publicHolidays.indexOf(date) > -1; | |
!isPublicHoliday && (emplyeesLeaveCounts[emplyeeName] += 1) | |
}) | |
}) | |
if (Object.keys(emplyeesLeaveCounts).length) { | |
const sortedObject = Object.keys(emplyeesLeaveCounts).sort().reduce((acc, item) => { | |
const eKey = item | |
const eValue = emplyeesLeaveCounts[item] | |
acc[eKey] = eValue | |
return acc | |
}, {}) | |
console.log(sortedObject) | |
return sortedObject | |
} | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment