Skip to content

Commit

Permalink
Merge pull request #12 from okgrow/update-datetime-serialization
Browse files Browse the repository at this point in the history
fix: add support for serializing DateTime from ISO date string
  • Loading branch information
ccuilla committed Jul 27, 2018
2 parents c7b8fc4 + 9058bf0 commit 0b76be4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
44 changes: 25 additions & 19 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,53 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.4.1] - 2018-07-27

### Changed

- `DateTime` - Add support for serializing from a valid Date ISO string format

## [0.4.0] - 2018-04-27

### Added

* `RegularExpression` scalar type _generator_
* `UnsignedInt` alias for `NonNegativeInt`
* `UnsignedFloat` alias for `NonNegativeFloat`
- `RegularExpression` scalar type _generator_
- `UnsignedInt` alias for `NonNegativeInt`
- `UnsignedFloat` alias for `NonNegativeFloat`

## [0.3.0] - 2018-04-06

### Changed

* Updated package versions in `devDependencies`
* Updated `graphql` version dependency to `0.13.1`
* Added [Prettier](https://prettier.io/) configuration to `package.json`
* Linted (based on updates to eslint packages) and Prettier-ed all files
- Updated package versions in `devDependencies`
- Updated `graphql` version dependency to `0.13.1`
- Added [Prettier](https://prettier.io/) configuration to `package.json`
- Linted (based on updates to eslint packages) and Prettier-ed all files

### Added

* PhoneNumber
* PostalCode
* `package-lock.json`
- PhoneNumber
- PostalCode
- `package-lock.json`

## [0.2.0] - 2018-01-16

### Changed

* Implemented more strict numeric type checking
* Some type exception messages changed slightly
* Changed `graphql` from a dependency to a _peer_ dependency
- Implemented more strict numeric type checking
- Some type exception messages changed slightly
- Changed `graphql` from a dependency to a _peer_ dependency

### Added

* NegativeInt
* NegativeFloat
* NonPositiveInt
* NonPositiveFloat
* more tests
- NegativeInt
- NegativeFloat
- NonPositiveInt
- NonPositiveFloat
- more tests

## [0.1.0] - 2017-07-14

### Added

* Initial Release - released as [`@okgrow/graphql-scalars`](https://www.npmjs.com/package/@okgrow/graphql-scalars) on npm.
- Initial Release - released as [`@okgrow/graphql-scalars`](https://www.npmjs.com/package/@okgrow/graphql-scalars) on npm.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@okgrow/graphql-scalars",
"version": "0.4.0",
"version": "0.4.1",
"description": "A collection of scalar types not included in base GraphQL.",
"repository": {
"type": "git",
Expand Down
24 changes: 17 additions & 7 deletions src/DateTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,33 @@ export default new GraphQLScalarType({
description: 'Use JavaScript Date object for date/time fields.',

serialize(value) {
if (!(value instanceof Date)) {
throw new TypeError(`Value is not an instance of Date: ${value}`);
let v = value;

if (!(v instanceof Date) && typeof v !== 'string') {
throw new TypeError(
`Value is not an instance of Date or Date string: ${v}`,
);
}

if (typeof v === 'string') {
v = new Date();

v.setTime(Date.parse(value));
}

// eslint-disable-next-line no-restricted-globals
if (isNaN(value.getTime())) {
throw new TypeError(`Value is not a valid Date: ${value}`);
if (Number.isNaN(v.getTime())) {
throw new TypeError(`Value is not a valid Date: ${v}`);
}

return value.toJSON();
return v.toJSON();
},

parseValue(value) {
const date = new Date(value);

// eslint-disable-next-line no-restricted-globals
if (isNaN(date.getTime())) {
if (Number.isNaN(date.getTime())) {
throw new TypeError(`Value is not a valid Date: ${value}`);
}

Expand All @@ -41,7 +51,7 @@ export default new GraphQLScalarType({
const result = new Date(ast.value);

// eslint-disable-next-line no-restricted-globals
if (isNaN(result.getTime())) {
if (Number.isNaN(result.getTime())) {
throw new GraphQLError(`Value is not a valid Date: ${ast.value}`);
}

Expand Down
12 changes: 11 additions & 1 deletion src/__tests__/DateTime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ describe('DateTime', () => {
expect(DateTime.serialize(now)).toEqual(now.toJSON());
});

test('serialize (String)', () => {
const now = '2018-07-24T01:28:47.940Z';
const d1 = Date.parse(now);
const d2 = new Date();

d2.setTime(d1);

expect(DateTime.serialize(now)).toEqual(d2.toJSON());
});

test('parseValue', () => {
const now = new Date();
expect(DateTime.parseValue(now)).toEqual(now);
Expand All @@ -31,7 +41,7 @@ describe('DateTime', () => {
describe('not a valid date', () => {
test('serialize', () => {
expect(() => DateTime.serialize('this is not a date')).toThrow(
/Value is not an instance of Date/,
/Value is not a valid Date/,
);
});

Expand Down

0 comments on commit 0b76be4

Please sign in to comment.