Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract "didYouMean" util function #1944

Merged
merged 1 commit into from
Jun 2, 2019
Merged
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
2 changes: 1 addition & 1 deletion src/execution/__tests__/variables-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$value" got invalid value [1, 2, 3]; Expected type String; String cannot represent a non string value: [1, 2, 3]',
'Variable "$value" got invalid value [1, 2, 3]; Expected type String. String cannot represent a non string value: [1, 2, 3]',
locations: [{ line: 2, column: 16 }],
},
],
Expand Down
36 changes: 36 additions & 0 deletions src/jsutils/__tests__/didYouMean-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

import { expect } from 'chai';
import { describe, it } from 'mocha';
import didYouMean from '../didYouMean';

describe('didYouMean', () => {
it('Does accept an empty list', () => {
expect(didYouMean([])).to.equal('');
});

it('Handle single suggestion', () => {
expect(didYouMean(['A'])).to.equal(' Did you mean A?');
});

it('Handle two suggestions', () => {
expect(didYouMean(['A', 'B'])).to.equal(' Did you mean A or B?');
});

it('Handle multiple suggestions', () => {
expect(didYouMean(['A', 'B', 'C'])).to.equal(' Did you mean A, B, or C?');
});

it('Limits to five suggestions', () => {
expect(didYouMean(['A', 'B', 'C', 'D', 'E', 'F'])).to.equal(
' Did you mean A, B, C, D, or E?',
);
});
});
36 changes: 0 additions & 36 deletions src/jsutils/__tests__/quotedOrList-test.js

This file was deleted.

46 changes: 46 additions & 0 deletions src/jsutils/didYouMean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

const MAX_SUGGESTIONS = 5;

/**
* Given [ A, B, C ] return ' Did you mean A, B, or C?'.
*/
declare function didYouMean(suggestions: $ReadOnlyArray<string>): string;
// eslint-disable-next-line no-redeclare
declare function didYouMean(
subMessage: string,
suggestions: $ReadOnlyArray<string>,
): string;

// eslint-disable-next-line no-redeclare
export default function didYouMean(firstArg, secondArg) {
const [subMessage, suggestions] =
typeof firstArg === 'string'
? [firstArg, secondArg]
: [undefined, firstArg];

let message = ' Did you mean ';
if (subMessage) {
message += subMessage + ' ';
}

switch (suggestions.length) {
case 0:
return '';
case 1:
return message + suggestions[0] + '?';
case 2:
return message + suggestions[0] + ' or ' + suggestions[1] + '?';
}

const selected = suggestions.slice(0, MAX_SUGGESTIONS);
const lastItem = selected.pop();
return message + selected.join(', ') + ', or ' + lastItem + '?';
}
30 changes: 0 additions & 30 deletions src/jsutils/orList.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/jsutils/quotedOrList.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/subscription/__tests__/subscribe-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ describe('Subscription Initialization Phase', () => {
errors: [
{
message:
'Variable "$priority" got invalid value "meow"; Expected type Int; Int cannot represent non-integer value: "meow"',
'Variable "$priority" got invalid value "meow"; Expected type Int. Int cannot represent non-integer value: "meow"',
locations: [{ line: 2, column: 21 }],
},
],
Expand Down
6 changes: 3 additions & 3 deletions src/type/__tests__/enumType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('Type System: Enum Values', () => {
errors: [
{
message:
'Expected type Color, found "GREEN"; Did you mean the enum value GREEN?',
'Expected type Color, found "GREEN". Did you mean the enum value GREEN?',
locations: [{ line: 1, column: 23 }],
},
],
Expand All @@ -179,7 +179,7 @@ describe('Type System: Enum Values', () => {
errors: [
{
message:
'Expected type Color, found GREENISH; Did you mean the enum value GREEN?',
'Expected type Color, found GREENISH. Did you mean the enum value GREEN?',
locations: [{ line: 1, column: 23 }],
},
],
Expand All @@ -193,7 +193,7 @@ describe('Type System: Enum Values', () => {
errors: [
{
message:
'Expected type Color, found green; Did you mean the enum value GREEN?',
'Expected type Color, found green. Did you mean the enum value GREEN?',
locations: [{ line: 1, column: 23 }],
},
],
Expand Down
46 changes: 23 additions & 23 deletions src/utilities/__tests__/coerceValue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('coerceValue', () => {
it('returns error for array input as string', () => {
const result = coerceValue([1, 2, 3], GraphQLString);
expectErrors(result).to.deep.equal([
'Expected type String; String cannot represent a non string value: [1, 2, 3]',
'Expected type String. String cannot represent a non string value: [1, 2, 3]',
]);
});
});
Expand All @@ -46,7 +46,7 @@ describe('coerceValue', () => {
it('returns error for array input as ID', () => {
const result = coerceValue([1, 2, 3], GraphQLID);
expectErrors(result).to.deep.equal([
'Expected type ID; ID cannot represent value: [1, 2, 3]',
'Expected type ID. ID cannot represent value: [1, 2, 3]',
]);
});
});
Expand All @@ -60,7 +60,7 @@ describe('coerceValue', () => {
it('returns error for numeric looking string', () => {
const result = coerceValue('1', GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: "1"',
'Expected type Int. Int cannot represent non-integer value: "1"',
]);
});

Expand All @@ -82,49 +82,49 @@ describe('coerceValue', () => {
it('returns a single error for empty string as value', () => {
const result = coerceValue('', GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: ""',
'Expected type Int. Int cannot represent non-integer value: ""',
]);
});

it('returns a single error for 2^32 input as int', () => {
const result = coerceValue(Math.pow(2, 32), GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non 32-bit signed integer value: 4294967296',
'Expected type Int. Int cannot represent non 32-bit signed integer value: 4294967296',
]);
});

it('returns a single error for float input as int', () => {
const result = coerceValue(1.5, GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: 1.5',
'Expected type Int. Int cannot represent non-integer value: 1.5',
]);
});

it('returns a single error for NaN input as int', () => {
const result = coerceValue(NaN, GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: NaN',
'Expected type Int. Int cannot represent non-integer value: NaN',
]);
});

it('returns a single error for Infinity input as int', () => {
const result = coerceValue(Infinity, GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: Infinity',
'Expected type Int. Int cannot represent non-integer value: Infinity',
]);
});

it('returns a single error for char input', () => {
const result = coerceValue('a', GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: "a"',
'Expected type Int. Int cannot represent non-integer value: "a"',
]);
});

it('returns a single error for string input', () => {
const result = coerceValue('meow', GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: "meow"',
'Expected type Int. Int cannot represent non-integer value: "meow"',
]);
});
});
Expand All @@ -148,7 +148,7 @@ describe('coerceValue', () => {
it('returns error for numeric looking string', () => {
const result = coerceValue('1', GraphQLFloat);
expectErrors(result).to.deep.equal([
'Expected type Float; Float cannot represent non numeric value: "1"',
'Expected type Float. Float cannot represent non numeric value: "1"',
]);
});

Expand All @@ -160,35 +160,35 @@ describe('coerceValue', () => {
it('returns a single error for empty string input', () => {
const result = coerceValue('', GraphQLFloat);
expectErrors(result).to.deep.equal([
'Expected type Float; Float cannot represent non numeric value: ""',
'Expected type Float. Float cannot represent non numeric value: ""',
]);
});

it('returns a single error for NaN input', () => {
const result = coerceValue(NaN, GraphQLFloat);
expectErrors(result).to.deep.equal([
'Expected type Float; Float cannot represent non numeric value: NaN',
'Expected type Float. Float cannot represent non numeric value: NaN',
]);
});

it('returns a single error for Infinity input', () => {
const result = coerceValue(Infinity, GraphQLFloat);
expectErrors(result).to.deep.equal([
'Expected type Float; Float cannot represent non numeric value: Infinity',
'Expected type Float. Float cannot represent non numeric value: Infinity',
]);
});

it('returns a single error for char input', () => {
const result = coerceValue('a', GraphQLFloat);
expectErrors(result).to.deep.equal([
'Expected type Float; Float cannot represent non numeric value: "a"',
'Expected type Float. Float cannot represent non numeric value: "a"',
]);
});

it('returns a single error for char input', () => {
const result = coerceValue('meow', GraphQLFloat);
expectErrors(result).to.deep.equal([
'Expected type Float; Float cannot represent non numeric value: "meow"',
'Expected type Float. Float cannot represent non numeric value: "meow"',
]);
});
});
Expand All @@ -213,7 +213,7 @@ describe('coerceValue', () => {
it('results error for misspelled enum value', () => {
const result = coerceValue('foo', TestEnum);
expectErrors(result).to.deep.equal([
'Expected type TestEnum; did you mean FOO?',
'Expected type TestEnum. Did you mean FOO?',
]);
});

Expand Down Expand Up @@ -250,15 +250,15 @@ describe('coerceValue', () => {
it('returns an error for an invalid field', () => {
const result = coerceValue({ foo: 'abc' }, TestInputObject);
expectErrors(result).to.deep.equal([
'Expected type Int at value.foo; Int cannot represent non-integer value: "abc"',
'Expected type Int at value.foo. Int cannot represent non-integer value: "abc"',
]);
});

it('returns multiple errors for multiple invalid fields', () => {
const result = coerceValue({ foo: 'abc', bar: 'def' }, TestInputObject);
expectErrors(result).to.deep.equal([
'Expected type Int at value.foo; Int cannot represent non-integer value: "abc"',
'Expected type Int at value.bar; Int cannot represent non-integer value: "def"',
'Expected type Int at value.foo. Int cannot represent non-integer value: "abc"',
'Expected type Int at value.bar. Int cannot represent non-integer value: "def"',
]);
});

Expand All @@ -282,7 +282,7 @@ describe('coerceValue', () => {
it('returns error for a misspelled field', () => {
const result = coerceValue({ foo: 123, bart: 123 }, TestInputObject);
expectErrors(result).to.deep.equal([
'Field "bart" is not defined by type TestInputObject; did you mean bar?',
'Field "bart" is not defined by type TestInputObject. Did you mean bar?',
]);
});
});
Expand All @@ -298,8 +298,8 @@ describe('coerceValue', () => {
it('returns an error for an invalid input', () => {
const result = coerceValue([1, 'b', true], TestList);
expectErrors(result).to.deep.equal([
'Expected type Int at value[1]; Int cannot represent non-integer value: "b"',
'Expected type Int at value[2]; Int cannot represent non-integer value: true',
'Expected type Int at value[1]. Int cannot represent non-integer value: "b"',
'Expected type Int at value[2]. Int cannot represent non-integer value: true',
]);
});

Expand Down
Loading