Skip to content

Commit

Permalink
Add support for setting default scale
Browse files Browse the repository at this point in the history
  • Loading branch information
riyadhalnur committed Aug 16, 2019
1 parent 291ac24 commit 918bc83
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ Options
--scale, -s Weather scale. Defaults to Celcius
--help Show this help message
--version Display version info and exit
config Set the default location
config Set the default location and scale
Examples
$ weather -c Dhaka -C Bangladesh
Dhaka, Bangladesh
Condition: Partly Cloudy
Temperature: 32°C
$ weather config -c Dhaka -C Bangladesh
Default location set to Dhaka, Bangladesh
$ weather config -c Dhaka -C Bangladesh -s F
Default location set to Dhaka, Bangladesh and scale to F
`, {
flags: {
city: {
Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ module.exports = {
.then(res => {
let result = Object.assign(res.data, { city: city, country: country, scale: flags.scale });

if (flags.scale === 'F') {
if (flags.scale === 'F' || config.get('scale') === 'F') {
result.temp = _toFahrenheit(res.data.temp);
result.scale = 'F';
}

resolve(result);
Expand All @@ -39,7 +40,12 @@ module.exports = {

config.set('city', opts.city);
config.set('country', opts.country);
resolve(`Default location set to ${opts.city}, ${opts.country}`);

if (opts.scale) {
config.set('scale', opts.scale.toUpperCase());
}

resolve(`Default location set to ${opts.city}, ${opts.country} and scale to ${opts.scale ? opts.scale : 'C'}`);
});
}
};
8 changes: 7 additions & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,18 @@ describe('Weather CLI', () => {
describe('Configure defaults', () => {
it('should print success message if config set successfully', () => {
return weather.setLocation({city: 'Dhaka', country: 'Bangladesh'}).should.be.fulfilled.then(res => {
res.should.equal('Default location set to Dhaka, Bangladesh');
res.should.equal('Default location set to Dhaka, Bangladesh and scale to C');
});
});

it('should return error if arguments are missing', () => {
return weather.setLocation({city: 'Dhaka'}).should.be.rejected;
});

it('should set custom location and scale', () => {
return weather.setLocation({city: 'Dhaka', country: 'Bangladesh', scale: 'F'}).should.be.fulfilled.then(res => {
res.should.equal('Default location set to Dhaka, Bangladesh and scale to F');
});
});
});
});

0 comments on commit 918bc83

Please # to comment.