Skip to content

Commit

Permalink
New: date shorthand (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgwood authored and phated committed Jun 19, 2016
1 parent 444b78e commit 54caa30
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -54,6 +54,8 @@ var result = normalize.symbol(Symbol());
var result = normalize.boolean(true);
var result = normalize.function(function() {});
var result = normalize.undefined(undefined);
var result = normalize.date(new Date());
var result = normalize.date(1);
```

## API
Expand Down Expand Up @@ -100,6 +102,10 @@ Convenience method for `normalize('function', ...)`.

Convenience method for `normalize('undefined', ...)`.

#### `normalize.date(value[, ...appliedArguments])`

Convenience method for `normalize(dateOrTimestamp, ...)` where `dateOrTimestamp` accepts both numbers and instances of `Date`.

## License

MIT
Expand Down
8 changes: 8 additions & 0 deletions index.js
Expand Up @@ -50,4 +50,12 @@ types.forEach(function(type) {
normalize[type] = normalize.bind(null, type);
});

function dateOrTimestamp(value) {
return typeof value === 'number' ||
value instanceof Number ||
value instanceof Date;
}

normalize.date = normalize.bind(null, dateOrTimestamp);

module.exports = normalize;
24 changes: 24 additions & 0 deletions test/index.js
Expand Up @@ -241,3 +241,27 @@ describe('normalize.undefined', function() {
done();
});
});

describe('normalize.date', function() {

it('compares value to typeof number', function(done) {
var value = 1;
var result = normalize.date(value);
expect(result).toEqual(value);
done();
});

it('accepts object that are dates', function(done) {
var value = new Date();
var result = normalize.date(value);
expect(result).toEqual(value);
done();
});

it('rejects object that are not dates', function(done) {
var value = {};
var result = normalize.date(value);
expect(result).toEqual(null);
done();
});
});

0 comments on commit 54caa30

Please sign in to comment.