Skip to content

Commit 6ca4079

Browse files
author
Travis CI
committed
Deploy 3828c20 to NPM branch
1 parent 3183313 commit 6ca4079

File tree

3 files changed

+146
-80
lines changed

3 files changed

+146
-80
lines changed

type/scalars.js

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ var MAX_INT = 2147483647;
3535
var MIN_INT = -2147483648;
3636

3737
function serializeInt(value) {
38-
if (Array.isArray(value)) {
39-
throw new TypeError("Int cannot represent an array value: ".concat((0, _inspect.default)(value)));
38+
if (typeof value === 'boolean') {
39+
return value ? 1 : 0;
4040
}
4141

42-
var num = Number(value);
42+
var num = value;
4343

44-
if (value === '' || !(0, _isInteger.default)(num)) {
44+
if (typeof value === 'string' && value !== '') {
45+
num = Number(value);
46+
}
47+
48+
if (!(0, _isInteger.default)(num)) {
4549
throw new TypeError("Int cannot represent non-integer value: ".concat((0, _inspect.default)(value)));
4650
}
4751

@@ -84,13 +88,17 @@ var GraphQLInt = new _definition.GraphQLScalarType({
8488
exports.GraphQLInt = GraphQLInt;
8589

8690
function serializeFloat(value) {
87-
if (Array.isArray(value)) {
88-
throw new TypeError("Float cannot represent an array value: ".concat((0, _inspect.default)(value)));
91+
if (typeof value === 'boolean') {
92+
return value ? 1 : 0;
8993
}
9094

91-
var num = Number(value);
95+
var num = value;
96+
97+
if (typeof value === 'string' && value !== '') {
98+
num = Number(value);
99+
}
92100

93-
if (value === '' || !(0, _isFinite.default)(num)) {
101+
if (!(0, _isFinite.default)(num)) {
94102
throw new TypeError("Float cannot represent non numeric value: ".concat((0, _inspect.default)(value)));
95103
}
96104

@@ -120,14 +128,22 @@ function serializeString(value) {
120128
// Support serializing objects with custom valueOf() functions - a common way
121129
// to represent an complex value which can be represented as a string
122130
// (ex: MongoDB id objects).
123-
var result = value && typeof value.valueOf === 'function' ? value.valueOf() : value; // Serialize string, number, and boolean values to a string, but do not
131+
var result = value && typeof value.valueOf === 'function' ? value.valueOf() : value; // Serialize string, boolean and number values to a string, but do not
124132
// attempt to coerce object, function, symbol, or other types as strings.
125133

126-
if (typeof result !== 'string' && typeof result !== 'number' && typeof result !== 'boolean') {
127-
throw new TypeError("String cannot represent value: ".concat((0, _inspect.default)(result)));
134+
if (typeof result === 'string') {
135+
return result;
136+
}
137+
138+
if (typeof result === 'boolean') {
139+
return result ? 'true' : 'false';
140+
}
141+
142+
if ((0, _isFinite.default)(result)) {
143+
return result.toString();
128144
}
129145

130-
return String(result);
146+
throw new TypeError("String cannot represent value: ".concat((0, _inspect.default)(value)));
131147
}
132148

133149
function coerceString(value) {
@@ -150,11 +166,15 @@ var GraphQLString = new _definition.GraphQLScalarType({
150166
exports.GraphQLString = GraphQLString;
151167

152168
function serializeBoolean(value) {
153-
if (Array.isArray(value)) {
154-
throw new TypeError("Boolean cannot represent an array value: ".concat((0, _inspect.default)(value)));
169+
if (typeof value === 'boolean') {
170+
return value;
171+
}
172+
173+
if ((0, _isFinite.default)(value)) {
174+
return value !== 0;
155175
}
156176

157-
return Boolean(value);
177+
throw new TypeError("Boolean cannot represent a non boolean value: ".concat((0, _inspect.default)(value)));
158178
}
159179

160180
function coerceBoolean(value) {
@@ -181,19 +201,27 @@ function serializeID(value) {
181201
// to represent an object identifier (ex. MongoDB).
182202
var result = value && typeof value.valueOf === 'function' ? value.valueOf() : value;
183203

184-
if (typeof result !== 'string' && (typeof result !== 'number' || !(0, _isInteger.default)(result))) {
185-
throw new TypeError("ID cannot represent value: ".concat((0, _inspect.default)(value)));
204+
if (typeof result === 'string') {
205+
return result;
186206
}
187207

188-
return String(result);
208+
if ((0, _isInteger.default)(result)) {
209+
return String(result);
210+
}
211+
212+
throw new TypeError("ID cannot represent value: ".concat((0, _inspect.default)(value)));
189213
}
190214

191215
function coerceID(value) {
192-
if (typeof value !== 'string' && (typeof value !== 'number' || !(0, _isInteger.default)(value))) {
193-
throw new TypeError("ID cannot represent value: ".concat((0, _inspect.default)(value)));
216+
if (typeof value === 'string') {
217+
return value;
218+
}
219+
220+
if ((0, _isInteger.default)(value)) {
221+
return value.toString();
194222
}
195223

196-
return String(value);
224+
throw new TypeError("ID cannot represent value: ".concat((0, _inspect.default)(value)));
197225
}
198226

199227
var GraphQLID = new _definition.GraphQLScalarType({

type/scalars.js.flow

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ const MAX_INT = 2147483647;
2222
const MIN_INT = -2147483648;
2323

2424
function serializeInt(value: mixed): number {
25-
if (Array.isArray(value)) {
26-
throw new TypeError(
27-
`Int cannot represent an array value: ${inspect(value)}`,
28-
);
25+
if (typeof value === 'boolean') {
26+
return value ? 1 : 0;
2927
}
30-
const num = Number(value);
31-
if (value === '' || !isInteger(num)) {
28+
29+
let num = value;
30+
if (typeof value === 'string' && value !== '') {
31+
num = Number(value);
32+
}
33+
34+
if (!isInteger(num)) {
3235
throw new TypeError(
3336
`Int cannot represent non-integer value: ${inspect(value)}`,
3437
);
@@ -74,13 +77,15 @@ export const GraphQLInt = new GraphQLScalarType({
7477
});
7578

7679
function serializeFloat(value: mixed): number {
77-
if (Array.isArray(value)) {
78-
throw new TypeError(
79-
`Float cannot represent an array value: ${inspect(value)}`,
80-
);
80+
if (typeof value === 'boolean') {
81+
return value ? 1 : 0;
82+
}
83+
84+
let num = value;
85+
if (typeof value === 'string' && value !== '') {
86+
num = Number(value);
8187
}
82-
const num = Number(value);
83-
if (value === '' || !isFinite(num)) {
88+
if (!isFinite(num)) {
8489
throw new TypeError(
8590
`Float cannot represent non numeric value: ${inspect(value)}`,
8691
);
@@ -118,16 +123,18 @@ function serializeString(value: mixed): string {
118123
// (ex: MongoDB id objects).
119124
const result =
120125
value && typeof value.valueOf === 'function' ? value.valueOf() : value;
121-
// Serialize string, number, and boolean values to a string, but do not
126+
// Serialize string, boolean and number values to a string, but do not
122127
// attempt to coerce object, function, symbol, or other types as strings.
123-
if (
124-
typeof result !== 'string' &&
125-
typeof result !== 'number' &&
126-
typeof result !== 'boolean'
127-
) {
128-
throw new TypeError(`String cannot represent value: ${inspect(result)}`);
129-
}
130-
return String(result);
128+
if (typeof result === 'string') {
129+
return result;
130+
}
131+
if (typeof result === 'boolean') {
132+
return result ? 'true' : 'false';
133+
}
134+
if (isFinite(result)) {
135+
return result.toString();
136+
}
137+
throw new TypeError(`String cannot represent value: ${inspect(value)}`);
131138
}
132139

133140
function coerceString(value: mixed): string {
@@ -153,12 +160,15 @@ export const GraphQLString = new GraphQLScalarType({
153160
});
154161

155162
function serializeBoolean(value: mixed): boolean {
156-
if (Array.isArray(value)) {
157-
throw new TypeError(
158-
`Boolean cannot represent an array value: ${inspect(value)}`,
159-
);
163+
if (typeof value === 'boolean') {
164+
return value;
160165
}
161-
return Boolean(value);
166+
if (isFinite(value)) {
167+
return value !== 0;
168+
}
169+
throw new TypeError(
170+
`Boolean cannot represent a non boolean value: ${inspect(value)}`,
171+
);
162172
}
163173

164174
function coerceBoolean(value: mixed): boolean {
@@ -185,23 +195,23 @@ function serializeID(value: mixed): string {
185195
// to represent an object identifier (ex. MongoDB).
186196
const result =
187197
value && typeof value.valueOf === 'function' ? value.valueOf() : value;
188-
if (
189-
typeof result !== 'string' &&
190-
(typeof result !== 'number' || !isInteger(result))
191-
) {
192-
throw new TypeError(`ID cannot represent value: ${inspect(value)}`);
198+
if (typeof result === 'string') {
199+
return result;
193200
}
194-
return String(result);
201+
if (isInteger(result)) {
202+
return String(result);
203+
}
204+
throw new TypeError(`ID cannot represent value: ${inspect(value)}`);
195205
}
196206

197207
function coerceID(value: mixed): string {
198-
if (
199-
typeof value !== 'string' &&
200-
(typeof value !== 'number' || !isInteger(value))
201-
) {
202-
throw new TypeError(`ID cannot represent value: ${inspect(value)}`);
208+
if (typeof value === 'string') {
209+
return value;
210+
}
211+
if (isInteger(value)) {
212+
return value.toString();
203213
}
204-
return String(value);
214+
throw new TypeError(`ID cannot represent value: ${inspect(value)}`);
205215
}
206216

207217
export const GraphQLID = new GraphQLScalarType({

type/scalars.mjs

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ var MAX_INT = 2147483647;
2020
var MIN_INT = -2147483648;
2121

2222
function serializeInt(value) {
23-
if (Array.isArray(value)) {
24-
throw new TypeError("Int cannot represent an array value: ".concat(inspect(value)));
23+
if (typeof value === 'boolean') {
24+
return value ? 1 : 0;
2525
}
2626

27-
var num = Number(value);
27+
var num = value;
2828

29-
if (value === '' || !isInteger(num)) {
29+
if (typeof value === 'string' && value !== '') {
30+
num = Number(value);
31+
}
32+
33+
if (!isInteger(num)) {
3034
throw new TypeError("Int cannot represent non-integer value: ".concat(inspect(value)));
3135
}
3236

@@ -68,13 +72,17 @@ export var GraphQLInt = new GraphQLScalarType({
6872
});
6973

7074
function serializeFloat(value) {
71-
if (Array.isArray(value)) {
72-
throw new TypeError("Float cannot represent an array value: ".concat(inspect(value)));
75+
if (typeof value === 'boolean') {
76+
return value ? 1 : 0;
7377
}
7478

75-
var num = Number(value);
79+
var num = value;
80+
81+
if (typeof value === 'string' && value !== '') {
82+
num = Number(value);
83+
}
7684

77-
if (value === '' || !isFinite(num)) {
85+
if (!isFinite(num)) {
7886
throw new TypeError("Float cannot represent non numeric value: ".concat(inspect(value)));
7987
}
8088

@@ -103,14 +111,22 @@ function serializeString(value) {
103111
// Support serializing objects with custom valueOf() functions - a common way
104112
// to represent an complex value which can be represented as a string
105113
// (ex: MongoDB id objects).
106-
var result = value && typeof value.valueOf === 'function' ? value.valueOf() : value; // Serialize string, number, and boolean values to a string, but do not
114+
var result = value && typeof value.valueOf === 'function' ? value.valueOf() : value; // Serialize string, boolean and number values to a string, but do not
107115
// attempt to coerce object, function, symbol, or other types as strings.
108116

109-
if (typeof result !== 'string' && typeof result !== 'number' && typeof result !== 'boolean') {
110-
throw new TypeError("String cannot represent value: ".concat(inspect(result)));
117+
if (typeof result === 'string') {
118+
return result;
119+
}
120+
121+
if (typeof result === 'boolean') {
122+
return result ? 'true' : 'false';
123+
}
124+
125+
if (isFinite(result)) {
126+
return result.toString();
111127
}
112128

113-
return String(result);
129+
throw new TypeError("String cannot represent value: ".concat(inspect(value)));
114130
}
115131

116132
function coerceString(value) {
@@ -132,11 +148,15 @@ export var GraphQLString = new GraphQLScalarType({
132148
});
133149

134150
function serializeBoolean(value) {
135-
if (Array.isArray(value)) {
136-
throw new TypeError("Boolean cannot represent an array value: ".concat(inspect(value)));
151+
if (typeof value === 'boolean') {
152+
return value;
153+
}
154+
155+
if (isFinite(value)) {
156+
return value !== 0;
137157
}
138158

139-
return Boolean(value);
159+
throw new TypeError("Boolean cannot represent a non boolean value: ".concat(inspect(value)));
140160
}
141161

142162
function coerceBoolean(value) {
@@ -162,19 +182,27 @@ function serializeID(value) {
162182
// to represent an object identifier (ex. MongoDB).
163183
var result = value && typeof value.valueOf === 'function' ? value.valueOf() : value;
164184

165-
if (typeof result !== 'string' && (typeof result !== 'number' || !isInteger(result))) {
166-
throw new TypeError("ID cannot represent value: ".concat(inspect(value)));
185+
if (typeof result === 'string') {
186+
return result;
167187
}
168188

169-
return String(result);
189+
if (isInteger(result)) {
190+
return String(result);
191+
}
192+
193+
throw new TypeError("ID cannot represent value: ".concat(inspect(value)));
170194
}
171195

172196
function coerceID(value) {
173-
if (typeof value !== 'string' && (typeof value !== 'number' || !isInteger(value))) {
174-
throw new TypeError("ID cannot represent value: ".concat(inspect(value)));
197+
if (typeof value === 'string') {
198+
return value;
199+
}
200+
201+
if (isInteger(value)) {
202+
return value.toString();
175203
}
176204

177-
return String(value);
205+
throw new TypeError("ID cannot represent value: ".concat(inspect(value)));
178206
}
179207

180208
export var GraphQLID = new GraphQLScalarType({

0 commit comments

Comments
 (0)