Skip to content

Commit

Permalink
Merge 50b159d into c71455c
Browse files Browse the repository at this point in the history
  • Loading branch information
VovanR committed Apr 26, 2018
2 parents c71455c + 50b159d commit 995735b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# http://editorconfig.org

root = true

[*]
Expand All @@ -10,3 +12,6 @@ insert_final_newline = true
[*.{json,yml,md}]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# hour-to-day-or-night

<img align="right" width="120" height="120"
src="./logo.svg" alt="Hour to Day or Night logo">

[![Commitizen friendly][commitizen-image]][commitizen-url]
[![XO code style][codestyle-image]][codestyle-url]

Expand Down Expand Up @@ -29,15 +32,6 @@ hourToDayOrNight(8);

hourToDayOrNight(19);
//=> 'night'

hourToDayOrNight(17.9);
//=> 'day'

hourToDayOrNight('5.9');
//=> 'night'

hourToDayOrNight();
//=> ''
```

## License
Expand All @@ -48,7 +42,7 @@ MIT © [Vladimir Rodkin](https://github.com/VovanR)
[commitizen-url]: https://commitizen.github.io/cz-cli/
[commitizen-image]: https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square

[codestyle-url]: https://github.com/sindresorhus/xo
[codestyle-url]: https://github.com/xojs/xo
[codestyle-image]: https://img.shields.io/badge/code_style-XO-5ed9c7.svg?style=flat-square

[npm-url]: https://npmjs.org/package/hour-to-day-or-night
Expand Down
10 changes: 5 additions & 5 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
max="23"
type="number"
placeholder="hour"
/>
/>
</div>

<div class="converter__output">
Expand All @@ -32,15 +32,15 @@
readonly
type="text"
placeholder="result"
/>
/>
</div>

<a
class="copyright"
href="https://github.com/vovanr/hour-to-day-or-night"
target="_blank"
rel="noopener noreferrer"
>
>
<span class="copyright__license">MIT ©</span> <span class="copyright__author">Vladimir Rodkin</span>
</a>

Expand All @@ -55,11 +55,11 @@
var resultControl = document.querySelector('.converter__control_name_result')

var previousTheme
inputControl.value = new Date().getHours()
inputControl.valueAsNumber = new Date().getHours()
inputControl.addEventListener('input', renderResult)

function renderResult() {
var val = hourToDayOrNight(inputControl.value)
var val = hourToDayOrNight(inputControl.valueAsNumber)
resultControl.value = val
if (!converterBlock.classList.contains(val)) {
converterBlock.classList.add(val)
Expand Down
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ var DAY = 'day';
var NIGHT = 'night';

/**
* @param {number|string} hour
* @return {string} day or night
* @param {number} hour from 0 to 24
* @return {string} 'day' or 'night'
* @throws {TypeError} 'hour' argument type must be a 'number'
* @throws {RangeError} 'hour' argument value must be a number from 0 to 23
*
* @example
* hourToDayOrNight(8);
Expand All @@ -13,8 +15,12 @@ var NIGHT = 'night';
* //=> 'night'
*/
function hourToDayOrNight(hour) {
if ((!hour && hour !== 0) || hour > 24) {
return '';
if (typeof hour !== 'number' || isNaN(hour)) {
throw new TypeError('Expected `hour` type to be a `number`');
}

if (hour < 0 || hour >= 24) {
throw new RangeError('Expected `hour` to be a number from 0 to 23');
}

if (hour >= 18) {
Expand Down
16 changes: 16 additions & 0 deletions logo.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 19 additions & 14 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,30 @@ test('should convert number', t => {
});
});

test('should convert string', t => {
Object.keys(spec).forEach(part => {
spec[part].forEach(hour => {
t.is(fn(String(hour)), part);
});
});
});

test('should convert float', t => {
t.is(fn(5.9), 'night');
t.is(fn(17.9), 'day');
t.is(fn('5.9'), 'night');
t.is(fn('17.9'), 'day');
});

test('should not convert empty value', t => {
[undefined, null, ''].forEach(arg => t.is(fn(arg), ''));
test('should throw `TypeError` with incorrect argument type', t => {
const error = t.throws(() => fn(null), TypeError);
t.is(error.message, 'Expected `hour` type to be a `number`');
});

test('should throw `TypeError` with empty argument value', t => {
[undefined, null, ''].forEach(arg => t.throws(() => fn(arg), TypeError));
});

test('should throw `TypeError` with wrong argument types', t => {
[NaN, [], {}, '3'].forEach(arg => t.throws(() => fn(arg), TypeError));
});

test('should throw `RangeError` with argument value > 24 hours', t => {
const error = t.throws(() => fn(25), RangeError);
t.is(error.message, 'Expected `hour` to be a number from 0 to 23');
});

test('should not convert value > 24 hours', t => {
t.is(fn(25), '');
test('should throw `RangeError` with argument value < 0 hours', t => {
const error = t.throws(() => fn(-1), RangeError);
t.is(error.message, 'Expected `hour` to be a number from 0 to 23');
});

0 comments on commit 995735b

Please sign in to comment.