Skip to content

Commit

Permalink
Refactor and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
nsmag committed Sep 3, 2019
1 parent 27468ec commit 35af76b
Show file tree
Hide file tree
Showing 11 changed files with 3,948 additions and 2,174 deletions.
11 changes: 10 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{
"plugins": ["transform-es2015-modules-commonjs"]
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
57 changes: 51 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
version: 2
version: 2.1

references:
node_image: &node_image
circleci/node:10
working_directory: &working_directory
~/repo
dependencies_cache_key: &dependencies_cache_key
v1-dependencies-{{ checksum "package.json" }}

jobs:
build:
working_directory: ~/graphql-date-type
install_dependencies:
install_dependencies:
docker:
- image: circleci/node:8.1.2
- image: *node_image
working_directory: *working_directory
steps:
- checkout
- attach_workspace:
at: *working_directory
- restore_cache:
key: *dependencies_cache_key
- run:
name: Install dependencies
command: yarn
name: Install node_modules
command: yarn install --ignore-engines
- save_cache:
key: *dependencies_cache_key
paths:
- node_modules
- persist_to_workspace:
root: .
paths: node_modules

lint_build_test:
docker:
- image: *node_image
working_directory: *working_directory
steps:
- checkout
- attach_workspace:
at: *working_directory
- run:
name: Lint
command: yarn lint
- run:
name: Build
command: yarn build:clean && yarn build
- run:
name: Test
command: yarn test

workflows:
version: 2
build_test:
jobs:
- install_dependencies
- lint_build_test:
requires:
- install_dependencies
3 changes: 0 additions & 3 deletions .eslintrc

This file was deleted.

37 changes: 37 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"env": {
"browser": true,
"es6": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
"comma-dangle": [
"error",
"always-multiline"
],
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
# build
/dist

# coverage report
/coverage

# misc
.DS_Store
.env
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# coverage report
/coverage
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# graphql-date-type
# GraphQL Date Type

Install

```
yarn add https://github.com/storylog/graphql-date-type.git
yarn add https://github.com/storylog/graphql-date-type
```

Usage
Expand Down
40 changes: 19 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
{
"name": "graphql-date-type",
"version": "1.0.3",
"version": "2.0.0",
"main": "dist/index.js",
"author": "Natthakit Susanthitanon <ns@storylog.co>",
"license": "MIT",
"scripts": {
"build": "babel src -d dist --plugins transform-es2015-modules-commonjs",
"build:clean": "rm -rf dist",
"build": "babel src -d dist --ignore '**/*.spec.js'",
"lint": "eslint .",
"install": "yarn run build",
"pretest": "yarn run lint",
"test": "jest --coverage --forceExit --runInBand",
"test": "jest",
"test:watch": "jest --watch"
},
"dependencies": {
"graphql": "^14.5.4"
},
"devDependencies": {
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"babel-jest": "^24.9.0",
"eslint": "^6.3.0",
"jest": "^24.9.0",
"jest-cli": "^24.9.0",
"jest-matcher-utils": "^24.9.0"
},
"jest": {
"testEnvironment": "node",
"coverageReporters": [
"html"
],
"roots": [
"<rootDir>/tests"
"<rootDir>/src"
],
"testMatch": [
"**/?(*.)(spec|test).js?(x)"
"**/*.spec.js"
]
},
"dependencies": {
"babel-cli": "^6.24.1",
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1",
"graphql": "^0.10.1"
},
"devDependencies": {
"eslint": "3.19.0",
"eslint-config-storylog": "https://github.com/storylog/eslint-config-storylog.git",
"jest": "^20.0.4",
"jest-cli": "^20.0.4"
}
}
51 changes: 25 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
import { GraphQLScalarType } from 'graphql';
import { Kind } from 'graphql/language';
import { GraphQLScalarType, Kind } from 'graphql';

function errorMessage(value) {
return `Cannot represent an invalid Date instance ${JSON.stringify(value)}`;
}

function parseDate(value) {
const date = new Date(value);

if (isNaN(date)) {
throw new TypeError(errorMessage(value));
}

return date;
}

export default new GraphQLScalarType({
name: 'Date',
description: 'Date custom scalar type',
parseValue(value) {
description: 'Date string in ISO format',
serialize(value) {
try {
return (new Date(value)).toISOString();
} catch (error) {
throw new TypeError(
`Cannot represent an invalid Date instance ${JSON.stringify(value)}`
);
throw new TypeError(errorMessage(value));
}
},
parseLiteral(ast) {
try {
if (ast.kind === _language.Kind.INT) {
return (new Date(parseInt(ast.value, 10))).toISOString();
}
return null;
} catch (error) {
throw new TypeError(
`Cannot represent an invalid Date instance ${JSON.stringify(ast)}`
);
}
parseValue(value) {
return parseDate(value);
},
serialize(value) {
try {
if (!value) return value;
return (new Date(value)).toISOString();
} catch (error) {
throw new TypeError(
`Cannot represent an invalid Date instance ${JSON.stringify(value)}`
);
parseLiteral(ast) {
if (ast.kind === Kind.STRING) {
return parseDate(ast.value);
}

throw new TypeError(errorMessage(ast));
},
});
75 changes: 75 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* eslint-env jest */

import { Kind } from 'graphql';
import { stringify } from 'jest-matcher-utils';
import GraphQLDate from './index';

describe('GraphQLDate', () => {
describe('serialize', () => {
[
[new Date(Date.UTC(2019, 0, 1)), '2019-01-01T00:00:00.000Z'],
[new Date(Date.UTC(2019, 0, 1, 11, 22, 33, 444)), '2019-01-01T11:22:33.444Z'],
].forEach(([value, expected]) => {
it(`serialize Date ${stringify(value)} to ${stringify(expected)}`, () => {
expect(GraphQLDate.serialize(value)).toEqual(expected);
});
});

it('throw error when serializing invalid date', () => {
expect(() => {
GraphQLDate.serialize(new Date('invalid date'));
}).toThrowError();
});
});

describe('parseValue', () => {
[
['2019-01-01T00:00:00Z', new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))],
['2019-01-01T00:00:00.000Z', new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))],
['2019-01-01T11:22:33.444Z', new Date(Date.UTC(2019, 0, 1, 11, 22, 33, 444))],
].forEach(([value, expected]) => {
it(`parse ${stringify(value)} to Date ${stringify(expected)}`, () => {
expect(GraphQLDate.parseValue(value)).toEqual(expected);
});
});

it('throw error when parsing invalid date string', () => {
expect(() => {
GraphQLDate.parseValue('invalid date');
}).toThrowError();
});
});

describe('parseLiteral', () => {
[
['2019-01-01T00:00:00Z', new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))],
['2019-01-01T00:00:00.000Z', new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))],
['2019-01-01T11:22:33.444Z', new Date(Date.UTC(2019, 0, 1, 11, 22, 33, 444))],
].forEach(([value, expected]) => {
const literal = {
kind: Kind.STRING,
value,
};

it(`parse literal ${stringify(literal)} to Date ${stringify(expected)}`, () => {
expect(GraphQLDate.parseLiteral(literal)).toEqual(expected);
});
});

[
[Kind.STRING, 'invalid date'],
[Kind.INT, 1],
].forEach(([kind, value]) => {
const literal = {
kind,
value,
};

it(`throw error when parsing invalid literal ${stringify(literal)}`, () => {
expect(() => {
GraphQLDate.parseLiteral(literal);
}).toThrowError();
});
});
});
});
Loading

0 comments on commit 35af76b

Please sign in to comment.