Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

const greeting = require('./lib/greet.js');

greeting.greet(process.argv[2]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Glad to see you make use of process.argv[2] to solve the bonus!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You could have also stored process.argv[2] in a variable and pass it as an argument to a separate function, see below for further elaboration on this!

11 changes: 11 additions & 0 deletions lib/greet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = exports = {};

exports.greet = function(name) {
console.log(`hello ${name}`);
if (!arguments.length) throw new Error('name not provided');
return `hello ${name}!`;
};

console.log('hello', process.argv[2]);
Copy link
Copy Markdown

@kaylynyuh kaylynyuh Dec 6, 2016

Choose a reason for hiding this comment

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

Since you are just exporting a single function in this case, module.exports = { } would be more appropriate. You want to use module.exports = exports = { } when you are exporting multiple methods. For instance, if you had another method attached to the exports object in this file, then the way you are using module exports on line 3 would be just fine :)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

For instance, you could have attached another method to the exports object that handles the bonus and within that method, declared a var for process.argv[2] and then pass that as the argument. Then, in index.js, you just invoke that method and keep the original greet method that accepts a name as parameter.

19 changes: 19 additions & 0 deletions test/greet-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const greeting = require('../lib/greet.js');
const assert = require('assert');

describe('Greeting Module', function() {
describe('#greet', function() {
it('should return hello and name if input in command line', function() {
var inputName = process.argv[2];
let result = greeting.greet(inputName);
assert.ok(result === `hello ${inputName}!`, 'but not equal to hello and input name.');
});
it('should throw an undefined if name argument is not passed in command line', function() {
assert.throws(function() {
greeting.greet();
}, 'error not thrown');
});
});
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice work around for testing process.argv[2] As I'm sure you may have discovered, mocha doesn't accept command line arguments, so in this case, the better test would have been to check for a truthy value for inputName. Testing for a command line argument in mocha will always be undefined.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good to know!