Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions flow-typed/npm/jest_v22.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,24 +436,29 @@ type JestSpyType = {
calls: JestCallsType
};

type JestDoneFn = {
(): void,
fail: (error: Error) => void,
};

/** Runs this function after every test inside this context */
declare function afterEach(
fn: (done: () => void) => ?Promise<mixed>,
fn: (done: JestDoneFn) => ?Promise<mixed>,
timeout?: number
): void;
/** Runs this function before every test inside this context */
declare function beforeEach(
fn: (done: () => void) => ?Promise<mixed>,
fn: (done: JestDoneFn) => ?Promise<mixed>,
timeout?: number
): void;
/** Runs this function after all tests have finished inside this context */
declare function afterAll(
fn: (done: () => void) => ?Promise<mixed>,
fn: (done: JestDoneFn) => ?Promise<mixed>,
timeout?: number
): void;
/** Runs this function before any tests have started inside this context */
declare function beforeAll(
fn: (done: () => void) => ?Promise<mixed>,
fn: (done: JestDoneFn) => ?Promise<mixed>,
timeout?: number
): void;

Expand Down Expand Up @@ -486,7 +491,7 @@ declare var it: {
*/
(
name: string,
fn?: (done: () => void) => ?Promise<mixed>,
fn?: (done: JestDoneFn) => ?Promise<mixed>,
timeout?: number
): void,
/**
Expand All @@ -498,7 +503,7 @@ declare var it: {
*/
only(
name: string,
fn?: (done: () => void) => ?Promise<mixed>,
fn?: (done: JestDoneFn) => ?Promise<mixed>,
timeout?: number
): void,
/**
Expand All @@ -510,7 +515,7 @@ declare var it: {
*/
skip(
name: string,
fn?: (done: () => void) => ?Promise<mixed>,
fn?: (done: JestDoneFn) => ?Promise<mixed>,
timeout?: number
): void,
/**
Expand All @@ -522,13 +527,13 @@ declare var it: {
*/
concurrent(
name: string,
fn?: (done: () => void) => ?Promise<mixed>,
fn?: (done: JestDoneFn) => ?Promise<mixed>,
timeout?: number
): void
};
declare function fit(
name: string,
fn: (done: () => void) => ?Promise<mixed>,
fn: (done: JestDoneFn) => ?Promise<mixed>,
timeout?: number
): void;
/** An individual test unit */
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"README.md"
],
"peerDependencies": {
"graphql": "^0.5.0 || ^0.6.0 || ^0.7.0 || ^0.8.0-b || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0"
"graphql": "^0.5.0 || ^0.6.0 || ^0.7.0 || ^0.8.0-b || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^14.2.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
Expand All @@ -51,8 +51,8 @@
"babel-preset-stage-2": "^6.24.1",
"codecov": "^3.0.0",
"eslint-plugin-flowtype": "^2.50.0",
"flow-bin": "^0.81.0",
"graphql": "^14.0.2",
"flow-bin": "^0.107.0",
"graphql": "^14.2.1",
"jest": "^23.6.0",
"mockdate": "^2.0.2",
"standard": "^12.0.1"
Expand Down
2 changes: 2 additions & 0 deletions src/date/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ exports[`GraphQLDate value parsing throws an error when parsing 4566 1`] = `"Dat
exports[`GraphQLDate value parsing throws an error when parsing null 1`] = `"Date cannot represent non string type null"`;

exports[`GraphQLDate value parsing throws an error when parsing true 1`] = `"Date cannot represent non string type true"`;

exports[`GraphQLDate value parsing throws an error when parsing undefined 1`] = `"Date cannot represent non string type undefined"`;
12 changes: 7 additions & 5 deletions src/date/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,32 @@ const config: GraphQLScalarTypeConfig<Date, string> = {
throw new TypeError('Date cannot represent an invalid Date instance')
} else if (typeof value === 'string' || value instanceof String) {
if (validateDate(value)) {
return value
return value.toString()
}
throw new TypeError(
`Date cannot represent an invalid date-string ${value}.`
`Date cannot represent an invalid date-string ${value.toString()}.`
)
} else {
let parsedValue: string = JSON.stringify(value) || 'undefined'
throw new TypeError(
'Date cannot represent a non string, or non Date type ' +
JSON.stringify(value)
parsedValue
)
}
},
parseValue (value) {
if (!(typeof value === 'string' || value instanceof String)) {
let parsedValue: string = JSON.stringify(value) || 'undefined'
throw new TypeError(
`Date cannot represent non string type ${JSON.stringify(value)}`
`Date cannot represent non string type ${parsedValue}`
)
}

if (validateDate(value)) {
return parseDate(value)
}
throw new TypeError(
`Date cannot represent an invalid date-string ${value}.`
`Date cannot represent an invalid date-string ${value.toString()}.`
)
},
parseLiteral (ast) {
Expand Down
3 changes: 2 additions & 1 deletion src/date/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ describe('GraphQLDate', () => {
{},
[],
true,
null
null,
undefined
].forEach(invalidInput => {
it(`throws an error when parsing ${stringify(invalidInput)}`, () => {
expect(() =>
Expand Down
2 changes: 2 additions & 0 deletions src/dateTime/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,5 @@ exports[`GraphQLDateTime value parsing throws an error when parsing 4566 1`] = `
exports[`GraphQLDateTime value parsing throws an error when parsing null 1`] = `"DateTime cannot represent non string type null"`;

exports[`GraphQLDateTime value parsing throws an error when parsing true 1`] = `"DateTime cannot represent non string type true"`;

exports[`GraphQLDateTime value parsing throws an error when parsing undefined 1`] = `"DateTime cannot represent non string type undefined"`;
12 changes: 7 additions & 5 deletions src/dateTime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,36 @@ const config: GraphQLScalarTypeConfig<Date, string> = {
return serializeDateTimeString(value)
}
throw new TypeError(
`DateTime cannot represent an invalid date-time-string ${value}.`
`DateTime cannot represent an invalid date-time-string ${value.toString()}.`
)
} else if (typeof value === 'number' || value instanceof Number) {
if (validateUnixTimestamp(value)) {
return serializeUnixTimestamp(value)
}
throw new TypeError(
'DateTime cannot represent an invalid Unix timestamp ' + value
'DateTime cannot represent an invalid Unix timestamp ' + value.valueOf()
)
} else {
let parsedValue: string = JSON.stringify(value) || 'undefined'
throw new TypeError(
'DateTime cannot be serialized from a non string, ' +
'non numeric or non Date type ' + JSON.stringify(value)
'non numeric or non Date type ' + parsedValue
)
}
},
parseValue (value) {
if (!(typeof value === 'string' || value instanceof String)) {
let parsedValue: string = JSON.stringify(value) || 'undefined'
throw new TypeError(
`DateTime cannot represent non string type ${JSON.stringify(value)}`
`DateTime cannot represent non string type ${parsedValue}`
)
}

if (validateDateTime(value)) {
return parseDateTime(value)
}
throw new TypeError(
`DateTime cannot represent an invalid date-time-string ${value}.`
`DateTime cannot represent an invalid date-time-string ${value.toString()}.`
)
},
parseLiteral (ast) {
Expand Down
3 changes: 2 additions & 1 deletion src/dateTime/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ describe('GraphQLDateTime', () => {
{},
[],
true,
null
null,
undefined
].forEach(invalidInput => {
it(`throws an error when parsing ${stringify(invalidInput)}`, () => {
expect(() =>
Expand Down
2 changes: 2 additions & 0 deletions src/time/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ exports[`GraphQLTime value parsing throws an error when parsing 4566 1`] = `"Tim
exports[`GraphQLTime value parsing throws an error when parsing null 1`] = `"Time cannot represent non string type null"`;

exports[`GraphQLTime value parsing throws an error when parsing true 1`] = `"Time cannot represent non string type true"`;

exports[`GraphQLTime value parsing throws an error when parsing undefined 1`] = `"Time cannot represent non string type undefined"`;
10 changes: 6 additions & 4 deletions src/time/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,29 @@ const config: GraphQLScalarTypeConfig<Date, string> = {
return serializeTimeString(value)
}
throw new TypeError(
`Time cannot represent an invalid time-string ${value}.`
`Time cannot represent an invalid time-string ${value.toString()}.`
)
} else {
let parsedValue: string = JSON.stringify(value) || 'undefined'
throw new TypeError(
'Time cannot be serialized from a non string, ' +
'or non Date type ' + JSON.stringify(value)
'or non Date type ' + parsedValue
)
}
},
parseValue (value: mixed): Date {
if (!(typeof value === 'string' || value instanceof String)) {
let parsedValue: string = JSON.stringify(value) || 'undefined'
throw new TypeError(
`Time cannot represent non string type ${JSON.stringify(value)}`
`Time cannot represent non string type ${parsedValue}`
)
}

if (validateTime(value)) {
return parseTime(value)
}
throw new TypeError(
`Time cannot represent an invalid time-string ${value}.`
`Time cannot represent an invalid time-string ${value.toString()}.`
)
},
parseLiteral (ast): ?Date {
Expand Down
3 changes: 2 additions & 1 deletion src/time/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ describe('GraphQLTime', () => {
{},
[],
true,
null
null,
undefined
].forEach(invalidInput => {
it(`throws an error when parsing ${stringify(invalidInput)}`, () => {
expect(() =>
Expand Down
23 changes: 12 additions & 11 deletions src/utils/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
// Suppose the current date is 2016-01-01, then
// parseTime('11:00:12Z') parses to a Date corresponding to
// 2016-01-01T11:00:12Z.
export const parseTime = (time: string): Date => {
export const parseTime = (time: string | String): Date => {
const currentDateString = new Date().toISOString()
return new Date(currentDateString.substr(0, currentDateString.indexOf('T') + 1) + time)
return new Date(currentDateString.substr(0, currentDateString.indexOf('T') + 1) + time.toString())
}

// Serializes a Date into an RFC 3339 compliant time-string in the
Expand All @@ -30,10 +30,10 @@ export const serializeTime = (date: Date): string => {

// Serializes an RFC 3339 compliant time-string by shifting
// it to UTC.
export const serializeTimeString = (time: string): string => {
export const serializeTimeString = (time: string | String): string => {
// If already formatted to UTC then return the time string
if (time.indexOf('Z') !== -1) {
return time
return time.toString()
} else {
// These are time-strings with timezone information,
// these need to be shifted to UTC.
Expand Down Expand Up @@ -72,8 +72,8 @@ export const serializeTimeString = (time: string): string => {
// Example:
// parseDate('2016-01-01') parses to a Date corresponding to
// 2016-01-01T00:00:00.000Z.
export const parseDate = (date: string): Date => {
return new Date(date)
export const parseDate = (date: string | String): Date => {
return new Date(date.toString())
}

// Serializes a Date into a RFC 3339 compliant date-string
Expand All @@ -83,8 +83,8 @@ export const serializeDate = (date: Date): string => {
}

// Parses an RFC 3339 compliant date-time-string into a Date.
export const parseDateTime = (dateTime: string): Date => {
return new Date(dateTime)
export const parseDateTime = (dateTime: string | String): Date => {
return new Date(dateTime.toString())
}

// Serializes a Date into an RFC 3339 compliant date-time-string
Expand All @@ -95,7 +95,8 @@ export const serializeDateTime = (dateTime: Date): string => {

// Serializes an RFC 3339 compliant date-time-string by shifting
// it to UTC.
export const serializeDateTimeString = (dateTime: string): string => {
export const serializeDateTimeString = (dateTime: string | String): string => {
dateTime = dateTime.toString()
// If already formatted to UTC then return the time string
if (dateTime.indexOf('Z') !== -1) {
return dateTime
Expand Down Expand Up @@ -132,6 +133,6 @@ export const serializeDateTimeString = (dateTime: string): string => {

// Serializes a Unix timestamp to an RFC 3339 compliant date-time-string
// in the format YYYY-MM-DDThh:mm:ss.sssZ
export const serializeUnixTimestamp = (timestamp: number): string => {
return new Date(timestamp * 1000).toISOString()
export const serializeUnixTimestamp = (timestamp: number | Number): string => {
return new Date(timestamp.valueOf() * 1000).toISOString()
}
12 changes: 8 additions & 4 deletions src/utils/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ const leapYear = (year: number): boolean => {
// equals NaN.
// - Leap seconds cannot be known in advance.
//
export const validateTime = (time: string): boolean => {
export const validateTime = (time: string | String): boolean => {
const TIME_REGEX = /^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(\.\d{1,})?(([Z])|([+|-]([01][0-9]|2[0-3]):[0-5][0-9]))$/
time = time.toString()
return TIME_REGEX.test(time)
}

Expand All @@ -72,8 +73,9 @@ export const validateTime = (time: string): boolean => {
// 11 November 30
// 12 December 31
//
export const validateDate = (datestring: string): boolean => {
export const validateDate = (datestring: string | String): boolean => {
const RFC_3339_REGEX = /^(\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))$/
datestring = datestring.toString()

if (!RFC_3339_REGEX.test(datestring)) {
return false
Expand Down Expand Up @@ -117,8 +119,9 @@ export const validateDate = (datestring: string): boolean => {
//
// Where *s is a fraction of seconds with at least 1 digit.
//
export const validateDateTime = (dateTimeString: string): boolean => {
export const validateDateTime = (dateTimeString: string | String): boolean => {
const RFC_3339_REGEX = /^(\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60))(\.\d{1,})?(([Z])|([+|-]([01][0-9]|2[0-3]):[0-5][0-9]))$/
dateTimeString = dateTimeString.toString()

// Validate the structure of the date-string
if (!RFC_3339_REGEX.test(dateTimeString)) {
Expand All @@ -144,7 +147,8 @@ export const validateDateTime = (dateTimeString: string): boolean => {
// Unix timestamps are signed 32-bit integers. They are interpreted
// as the number of seconds since 00:00:00 UTC on 1 January 1970.
//
export const validateUnixTimestamp = (timestamp: number): boolean => {
export const validateUnixTimestamp = (timestamp: number | Number): boolean => {
timestamp = timestamp.valueOf()
const MAX_INT = 2147483647
const MIN_INT = -2147483648
return (timestamp === timestamp && timestamp <= MAX_INT && timestamp >= MIN_INT) // eslint-disable-line
Expand Down
14 changes: 8 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1889,9 +1889,10 @@ flat-cache@^1.2.1:
graceful-fs "^4.1.2"
write "^0.2.1"

flow-bin@^0.81.0:
version "0.81.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.81.0.tgz#7f0a733dce1dad3cb1447c692639292dc3d60bf5"
flow-bin@^0.107.0:
version "0.107.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.107.0.tgz#b37bfcce51204d35d58f8eb93b3a76b52291e4cc"
integrity sha512-hsmwO5Q0+XUXaO2kIKLpleUNNBSFcsGEQGBOTEC/KR/4Ez695I1fweX/ioSjbU4RWhPZhkIqnpbF9opVAauCHg==

for-in@^0.1.5:
version "0.1.6"
Expand Down Expand Up @@ -2108,9 +2109,10 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4:
version "1.0.1"
resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725"

graphql@^14.0.2:
version "14.0.2"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.0.2.tgz#7dded337a4c3fd2d075692323384034b357f5650"
graphql@^14.2.1:
version "14.2.1"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.2.1.tgz#779529bf9a01e7207b977a54c20670b48ca6e95c"
integrity sha512-2PL1UbvKeSjy/lUeJqHk+eR9CvuErXoCNwJI4jm3oNFEeY+9ELqHNKO1ZuSxAkasPkpWbmT/iMRMFxd3cEL3tQ==
dependencies:
iterall "^1.2.2"

Expand Down