Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cat): number output lines (#750) #775

Merged
merged 1 commit into from Sep 23, 2017
Merged

Conversation

gcca
Copy link
Contributor

@gcca gcca commented Sep 21, 2017

Add cat -n support to show number lines. Fix #750
Additionally this add two tests:

  • Empty file
  • File without EOF

Copy link
Member

@nfischer nfischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Much appreciated.

src/cat.js Outdated
}

function number(n) {
return (' ' + n).slice(-6);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment? I think this does the right thing (prepends 5 spaces for 1-digit numbers, 4 spaces for 2-digit, 3 spaces for 3-digit, etc.), but it's not super obvious. A one-line comment explaining this would be great.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, can we rename this function? "number" is vague. Maybe we can rename this to "prependPaddedNumber" and change its functionality to accept both a number and line (and it will insert all padding).

src/cat.js Outdated
}

lines = lines.map(function (line, i) {
return number(i + 1) + ' ' + line;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think GNU cat uses a tab, not a single space, between the number and line. It also makes for slightly easier parsing. I'm not sure if this is specified by POSIX, however.

test/cat.js Outdated
t.is(result.toString(), ' 1 test3');
});

test('multiple numbers without EOF', t => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test to check a file with 10+ lines (just to check that padding works as expected)?

test/cat.js Outdated
t.is(result.toString(), ' 1 test2\n 2 test1\n');
});

test('simple without EOF', t => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding! Could you move this test above the numbers section? It would be nice to clump the -n tests together.

src/cat.js Outdated
var lines = cat.split('\n');

if (lines[lines.length - 1] === '') {
addEOF = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm can we do something better here? It's kind of silly to strip the last array element simply to add it back at the end.

I'm not certain what the best approach is here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can read by chunks from file stream. But even 'readline' package split the buffer in lines.
https://github.com/nodejs/node/blob/master/lib/readline.js#L980

@codecov-io
Copy link

codecov-io commented Sep 22, 2017

Codecov Report

❗ No coverage uploaded for pull request base (master@dcead1b). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #775   +/-   ##
=========================================
  Coverage          ?   95.39%           
=========================================
  Files             ?       33           
  Lines             ?     1237           
  Branches          ?        0           
=========================================
  Hits              ?     1180           
  Misses            ?       57           
  Partials          ?        0
Impacted Files Coverage Δ
src/cat.js 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dcead1b...a267c2b. Read the comment docs.

Copy link
Member

@nfischer nfischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple more comments, otherwise LGTM. Thanks for the feature!

src/cat.js Outdated

function addNumbers(cat) {
var lines = cat.split('\n');
var lastLine = lines.splice(-1)[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think lines.pop() is a bit clearer?

t.is(result.toString(), 'test3');
});

test('empty', t => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you edit the PR description to mention that you're adding 2 additional tests for previous functionality? I assume this is code that worked before--please correct me if that's not so.

Copy link
Contributor Author

@gcca gcca Sep 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it has work fine. I add these tests as part of the -n implementation.
Thanks for the feedback. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants