sindresorhus / df

Get free disk space info from `df -kP`
MIT License
64 stars 12 forks source link

Fix output parsing #7

Closed medusalix closed 5 years ago

medusalix commented 5 years ago

Parses the table by calculating the boundaries of its columns. I added some tests for it, although I'm not quite sure how to use module.exports correctly to export the parseOutput function for testing only. The algorithm using the while loop can surely be improved too. Open for suggestions 😃.

Fixes #5

sindresorhus commented 5 years ago

I added some tests for it, although I'm not quite sure how to use module.exports correctly to export the parseOutput function for testing only.

if (process.env.NODE_ENV === 'test') {
    module.exports._parseOutput = parseOutput;
}

(AVA sets this environment variable)

sindresorhus commented 5 years ago

The algorithm using the while loop can surely be improved too. Open for suggestions 😃.

I don't really have any suggestions... I guess the while-loop approach is fine as long as you add a lot of tests to cover the behavior and edge-cases.

sindresorhus commented 5 years ago

Would be good to add some inline code comments to explain your algorithm/approach.

medusalix commented 5 years ago

Would be good to add some inline code comments to explain your algorithm/approach.

I simplified the algorithm quite a bit and added some comments to explain the regex.

I don't really have any suggestions... I guess the while-loop approach is fine as long as you add a lot of tests to cover the behavior and edge-cases.

I'm not sure what to test for, apart from the usual output that df produces. My algorithm relies on the columns being in a certain order (Not sure if that is guaranteed across implementations of df). Feeding malformed input to the regex obivously produces incorrect results.