From 918bc83fb6cf72a3b4a433cbf5781a6163237db7 Mon Sep 17 00:00:00 2001 From: Riyadh Al Nur Date: Sat, 17 Aug 2019 00:43:25 +0800 Subject: [PATCH] Add support for setting default scale --- cli.js | 6 +++--- index.js | 10 ++++++++-- tests/tests.js | 8 +++++++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/cli.js b/cli.js index 838cc5a..855afef 100755 --- a/cli.js +++ b/cli.js @@ -17,7 +17,7 @@ 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 @@ -25,8 +25,8 @@ Examples 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: { diff --git a/index.js b/index.js index a7a6623..0aa824c 100644 --- a/index.js +++ b/index.js @@ -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); @@ -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'}`); }); } }; diff --git a/tests/tests.js b/tests/tests.js index b93db31..41dcd59 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -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'); + }); + }); }); });