graphql 15.4.0 → 15.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +10 -0
  2. package/error/GraphQLError.js +3 -3
  3. package/error/GraphQLError.js.flow +1 -1
  4. package/error/GraphQLError.mjs +1 -1
  5. package/execution/execute.js +8 -9
  6. package/execution/execute.js.flow +8 -10
  7. package/execution/execute.mjs +8 -8
  8. package/jsutils/isAsyncIterable.js +1 -7
  9. package/jsutils/isAsyncIterable.js.flow +1 -5
  10. package/jsutils/isAsyncIterable.mjs +1 -7
  11. package/jsutils/naturalCompare.js +69 -0
  12. package/jsutils/naturalCompare.js.flow +59 -0
  13. package/jsutils/naturalCompare.mjs +61 -0
  14. package/jsutils/safeArrayFrom.js +73 -0
  15. package/jsutils/safeArrayFrom.js.flow +59 -0
  16. package/jsutils/safeArrayFrom.mjs +66 -0
  17. package/jsutils/suggestionList.js +5 -1
  18. package/jsutils/suggestionList.js.flow +3 -1
  19. package/jsutils/suggestionList.mjs +3 -1
  20. package/language/blockString.js.flow +2 -2
  21. package/language/source.js.flow +1 -1
  22. package/package.json +3 -4
  23. package/type/definition.js.flow +57 -45
  24. package/type/directives.js.flow +9 -7
  25. package/type/schema.js.flow +1 -1
  26. package/utilities/TypeInfo.js.flow +1 -1
  27. package/utilities/astFromValue.js +6 -8
  28. package/utilities/astFromValue.js.flow +6 -6
  29. package/utilities/astFromValue.mjs +6 -7
  30. package/utilities/buildClientSchema.js +2 -1
  31. package/utilities/buildClientSchema.js.flow +1 -0
  32. package/utilities/buildClientSchema.mjs +2 -1
  33. package/utilities/coerceInputValue.js +7 -8
  34. package/utilities/coerceInputValue.js.flow +11 -8
  35. package/utilities/coerceInputValue.mjs +7 -7
  36. package/utilities/findBreakingChanges.js +6 -2
  37. package/utilities/findBreakingChanges.js.flow +6 -2
  38. package/utilities/findBreakingChanges.mjs +5 -2
  39. package/utilities/getIntrospectionQuery.d.ts +6 -0
  40. package/utilities/getIntrospectionQuery.js +8 -2
  41. package/utilities/getIntrospectionQuery.js.flow +16 -3
  42. package/utilities/getIntrospectionQuery.mjs +8 -2
  43. package/utilities/introspectionFromSchema.js +3 -1
  44. package/utilities/introspectionFromSchema.js.flow +2 -0
  45. package/utilities/introspectionFromSchema.mjs +3 -1
  46. package/utilities/lexicographicSortSchema.js +3 -1
  47. package/utilities/lexicographicSortSchema.js.flow +3 -2
  48. package/utilities/lexicographicSortSchema.mjs +2 -1
  49. package/utilities/separateOperations.js +44 -40
  50. package/utilities/separateOperations.js.flow +46 -36
  51. package/utilities/separateOperations.mjs +44 -40
  52. package/validation/ValidationContext.js.flow +3 -3
  53. package/validation/rules/FieldsOnCorrectTypeRule.js +3 -1
  54. package/validation/rules/FieldsOnCorrectTypeRule.js.flow +2 -1
  55. package/validation/rules/FieldsOnCorrectTypeRule.mjs +2 -1
  56. package/validation/rules/UniqueDirectiveNamesRule.js +1 -1
  57. package/validation/rules/UniqueDirectiveNamesRule.mjs +1 -1
  58. package/validation/rules/UniqueTypeNamesRule.js +1 -1
  59. package/validation/rules/UniqueTypeNamesRule.mjs +1 -1
  60. package/validation/validate.js.flow +4 -4
  61. package/version.js +2 -2
  62. package/version.js.flow +2 -2
  63. package/version.mjs +2 -2
  64. package/jsutils/isCollection.js +0 -47
  65. package/jsutils/isCollection.js.flow +0 -38
  66. package/jsutils/isCollection.mjs +0 -40
package/README.md CHANGED
@@ -115,6 +115,16 @@ directly on this branch:
115
115
  npm install graphql@git://github.com/graphql/graphql-js.git#npm
116
116
  ```
117
117
 
118
+ ### Experimental features
119
+
120
+ Each release of GraphQL.js will be accompanied by an experimental release containing support for the `@defer` and `@stream` directive proposal. We are hoping to get community feedback on these releases before the proposal is accepted into the GraphQL specification. You can use this experimental release of GraphQL.js by adding the following to your project's `package.json` file.
121
+
122
+ ```
123
+ "graphql": "experimental-stream-defer"
124
+ ```
125
+
126
+ Community feedback on this experimental release is much appreciated and can be provided on the [issue created for this purpose](https://github.com/graphql/graphql-js/issues/2848).
127
+
118
128
  ### Using in a Browser
119
129
 
120
130
  GraphQL.js is a general-purpose library and can be used both in a Node server
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
 
3
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
+
3
5
  Object.defineProperty(exports, "__esModule", {
4
6
  value: true
5
7
  });
@@ -16,8 +18,6 @@ var _printLocation = require("../language/printLocation.js");
16
18
 
17
19
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
20
 
19
- function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
20
-
21
21
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
22
22
 
23
23
  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
@@ -219,7 +219,7 @@ var GraphQLError = /*#__PURE__*/function (_Error) {
219
219
  }
220
220
  }); // Include (non-enumerable) stack trace.
221
221
 
222
- if (originalError === null || originalError === void 0 ? void 0 : originalError.stack) {
222
+ if (originalError !== null && originalError !== void 0 && originalError.stack) {
223
223
  Object.defineProperty(_assertThisInitialized(_this), 'stack', {
224
224
  value: originalError.stack,
225
225
  writable: true,
@@ -84,7 +84,7 @@ export class GraphQLError extends Error {
84
84
  path?: ?$ReadOnlyArray<string | number>,
85
85
  originalError?: ?(Error & { +extensions?: mixed, ... }),
86
86
  extensions?: ?{ [key: string]: mixed, ... },
87
- ): void {
87
+ ) {
88
88
  super(message);
89
89
 
90
90
  // Compute list of blame nodes.
@@ -208,7 +208,7 @@ export var GraphQLError = /*#__PURE__*/function (_Error) {
208
208
  }
209
209
  }); // Include (non-enumerable) stack trace.
210
210
 
211
- if (originalError === null || originalError === void 0 ? void 0 : originalError.stack) {
211
+ if (originalError !== null && originalError !== void 0 && originalError.stack) {
212
212
  Object.defineProperty(_assertThisInitialized(_this), 'stack', {
213
213
  value: originalError.stack,
214
214
  writable: true,
@@ -12,8 +12,6 @@ exports.buildResolveInfo = buildResolveInfo;
12
12
  exports.getFieldDef = getFieldDef;
13
13
  exports.defaultFieldResolver = exports.defaultTypeResolver = void 0;
14
14
 
15
- var _arrayFrom = _interopRequireDefault(require("../polyfills/arrayFrom.js"));
16
-
17
15
  var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
18
16
 
19
17
  var _memoize = _interopRequireDefault(require("../jsutils/memoize3.js"));
@@ -26,7 +24,7 @@ var _isPromise = _interopRequireDefault(require("../jsutils/isPromise.js"));
26
24
 
27
25
  var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike.js"));
28
26
 
29
- var _isCollection = _interopRequireDefault(require("../jsutils/isCollection.js"));
27
+ var _safeArrayFrom = _interopRequireDefault(require("../jsutils/safeArrayFrom.js"));
30
28
 
31
29
  var _promiseReduce = _interopRequireDefault(require("../jsutils/promiseReduce.js"));
32
30
 
@@ -602,15 +600,11 @@ function completeValue(exeContext, returnType, fieldNodes, info, path, result) {
602
600
 
603
601
 
604
602
  function completeListValue(exeContext, returnType, fieldNodes, info, path, result) {
605
- if (!(0, _isCollection.default)(result)) {
606
- throw new _GraphQLError.GraphQLError("Expected Iterable, but did not find one for field \"".concat(info.parentType.name, ".").concat(info.fieldName, "\"."));
607
- } // This is specified as a simple map, however we're optimizing the path
603
+ // This is specified as a simple map, however we're optimizing the path
608
604
  // where the list contains no Promises by avoiding creating another Promise.
609
-
610
-
611
605
  var itemType = returnType.ofType;
612
606
  var containsPromise = false;
613
- var completedResults = (0, _arrayFrom.default)(result, function (item, index) {
607
+ var completedResults = (0, _safeArrayFrom.default)(result, function (item, index) {
614
608
  // No need to modify the info object containing the path,
615
609
  // since from here on it is not ever accessed by resolver functions.
616
610
  var itemPath = (0, _Path.addPath)(path, index, undefined);
@@ -642,6 +636,11 @@ function completeListValue(exeContext, returnType, fieldNodes, info, path, resul
642
636
  return handleFieldError(error, itemType, exeContext);
643
637
  }
644
638
  });
639
+
640
+ if (completedResults == null) {
641
+ throw new _GraphQLError.GraphQLError("Expected Iterable, but did not find one for field \"".concat(info.parentType.name, ".").concat(info.fieldName, "\"."));
642
+ }
643
+
645
644
  return containsPromise ? Promise.all(completedResults) : completedResults;
646
645
  }
647
646
  /**
@@ -1,6 +1,4 @@
1
1
  // @flow strict
2
- import arrayFrom from '../polyfills/arrayFrom';
3
-
4
2
  import type { Path } from '../jsutils/Path';
5
3
  import type { ObjMap } from '../jsutils/ObjMap';
6
4
  import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
@@ -10,7 +8,7 @@ import invariant from '../jsutils/invariant';
10
8
  import devAssert from '../jsutils/devAssert';
11
9
  import isPromise from '../jsutils/isPromise';
12
10
  import isObjectLike from '../jsutils/isObjectLike';
13
- import isCollection from '../jsutils/isCollection';
11
+ import safeArrayFrom from '../jsutils/safeArrayFrom';
14
12
  import promiseReduce from '../jsutils/promiseReduce';
15
13
  import promiseForObject from '../jsutils/promiseForObject';
16
14
  import { addPath, pathToArray } from '../jsutils/Path';
@@ -868,17 +866,11 @@ function completeListValue(
868
866
  path: Path,
869
867
  result: mixed,
870
868
  ): PromiseOrValue<$ReadOnlyArray<mixed>> {
871
- if (!isCollection(result)) {
872
- throw new GraphQLError(
873
- `Expected Iterable, but did not find one for field "${info.parentType.name}.${info.fieldName}".`,
874
- );
875
- }
876
-
877
869
  // This is specified as a simple map, however we're optimizing the path
878
870
  // where the list contains no Promises by avoiding creating another Promise.
879
871
  const itemType = returnType.ofType;
880
872
  let containsPromise = false;
881
- const completedResults = arrayFrom(result, (item, index) => {
873
+ const completedResults = safeArrayFrom(result, (item, index) => {
882
874
  // No need to modify the info object containing the path,
883
875
  // since from here on it is not ever accessed by resolver functions.
884
876
  const itemPath = addPath(path, index, undefined);
@@ -926,6 +918,12 @@ function completeListValue(
926
918
  }
927
919
  });
928
920
 
921
+ if (completedResults == null) {
922
+ throw new GraphQLError(
923
+ `Expected Iterable, but did not find one for field "${info.parentType.name}.${info.fieldName}".`,
924
+ );
925
+ }
926
+
929
927
  return containsPromise ? Promise.all(completedResults) : completedResults;
930
928
  }
931
929
 
@@ -1,11 +1,10 @@
1
- import arrayFrom from "../polyfills/arrayFrom.mjs";
2
1
  import inspect from "../jsutils/inspect.mjs";
3
2
  import memoize3 from "../jsutils/memoize3.mjs";
4
3
  import invariant from "../jsutils/invariant.mjs";
5
4
  import devAssert from "../jsutils/devAssert.mjs";
6
5
  import isPromise from "../jsutils/isPromise.mjs";
7
6
  import isObjectLike from "../jsutils/isObjectLike.mjs";
8
- import isCollection from "../jsutils/isCollection.mjs";
7
+ import safeArrayFrom from "../jsutils/safeArrayFrom.mjs";
9
8
  import promiseReduce from "../jsutils/promiseReduce.mjs";
10
9
  import promiseForObject from "../jsutils/promiseForObject.mjs";
11
10
  import { addPath, pathToArray } from "../jsutils/Path.mjs";
@@ -588,15 +587,11 @@ function completeValue(exeContext, returnType, fieldNodes, info, path, result) {
588
587
 
589
588
 
590
589
  function completeListValue(exeContext, returnType, fieldNodes, info, path, result) {
591
- if (!isCollection(result)) {
592
- throw new GraphQLError("Expected Iterable, but did not find one for field \"".concat(info.parentType.name, ".").concat(info.fieldName, "\"."));
593
- } // This is specified as a simple map, however we're optimizing the path
590
+ // This is specified as a simple map, however we're optimizing the path
594
591
  // where the list contains no Promises by avoiding creating another Promise.
595
-
596
-
597
592
  var itemType = returnType.ofType;
598
593
  var containsPromise = false;
599
- var completedResults = arrayFrom(result, function (item, index) {
594
+ var completedResults = safeArrayFrom(result, function (item, index) {
600
595
  // No need to modify the info object containing the path,
601
596
  // since from here on it is not ever accessed by resolver functions.
602
597
  var itemPath = addPath(path, index, undefined);
@@ -628,6 +623,11 @@ function completeListValue(exeContext, returnType, fieldNodes, info, path, resul
628
623
  return handleFieldError(error, itemType, exeContext);
629
624
  }
630
625
  });
626
+
627
+ if (completedResults == null) {
628
+ throw new GraphQLError("Expected Iterable, but did not find one for field \"".concat(info.parentType.name, ".").concat(info.fieldName, "\"."));
629
+ }
630
+
631
631
  return containsPromise ? Promise.all(completedResults) : completedResults;
632
632
  }
633
633
  /**
@@ -7,13 +7,7 @@ exports.default = isAsyncIterable;
7
7
 
8
8
  var _symbols = require("../polyfills/symbols.js");
9
9
 
10
- function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
11
-
12
10
  // eslint-disable-next-line no-redeclare
13
11
  function isAsyncIterable(maybeAsyncIterable) {
14
- if (maybeAsyncIterable == null || _typeof(maybeAsyncIterable) !== 'object') {
15
- return false;
16
- }
17
-
18
- return typeof maybeAsyncIterable[_symbols.SYMBOL_ASYNC_ITERATOR] === 'function';
12
+ return typeof (maybeAsyncIterable === null || maybeAsyncIterable === void 0 ? void 0 : maybeAsyncIterable[_symbols.SYMBOL_ASYNC_ITERATOR]) === 'function';
19
13
  }
@@ -10,9 +10,5 @@ declare function isAsyncIterable(value: mixed): boolean %checks(value instanceof
10
10
 
11
11
  // eslint-disable-next-line no-redeclare
12
12
  export default function isAsyncIterable(maybeAsyncIterable) {
13
- if (maybeAsyncIterable == null || typeof maybeAsyncIterable !== 'object') {
14
- return false;
15
- }
16
-
17
- return typeof maybeAsyncIterable[SYMBOL_ASYNC_ITERATOR] === 'function';
13
+ return typeof maybeAsyncIterable?.[SYMBOL_ASYNC_ITERATOR] === 'function';
18
14
  }
@@ -1,5 +1,3 @@
1
- function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
2
-
3
1
  import { SYMBOL_ASYNC_ITERATOR } from "../polyfills/symbols.mjs";
4
2
  /**
5
3
  * Returns true if the provided object implements the AsyncIterator protocol via
@@ -8,9 +6,5 @@ import { SYMBOL_ASYNC_ITERATOR } from "../polyfills/symbols.mjs";
8
6
 
9
7
  // eslint-disable-next-line no-redeclare
10
8
  export default function isAsyncIterable(maybeAsyncIterable) {
11
- if (maybeAsyncIterable == null || _typeof(maybeAsyncIterable) !== 'object') {
12
- return false;
13
- }
14
-
15
- return typeof maybeAsyncIterable[SYMBOL_ASYNC_ITERATOR] === 'function';
9
+ return typeof (maybeAsyncIterable === null || maybeAsyncIterable === void 0 ? void 0 : maybeAsyncIterable[SYMBOL_ASYNC_ITERATOR]) === 'function';
16
10
  }
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = naturalCompare;
7
+
8
+ /**
9
+ * Returns a number indicating whether a reference string comes before, or after,
10
+ * or is the same as the given string in natural sort order.
11
+ *
12
+ * See: https://en.wikipedia.org/wiki/Natural_sort_order
13
+ *
14
+ */
15
+ function naturalCompare(aStr, bStr) {
16
+ var aIdx = 0;
17
+ var bIdx = 0;
18
+
19
+ while (aIdx < aStr.length && bIdx < bStr.length) {
20
+ var aChar = aStr.charCodeAt(aIdx);
21
+ var bChar = bStr.charCodeAt(bIdx);
22
+
23
+ if (isDigit(aChar) && isDigit(bChar)) {
24
+ var aNum = 0;
25
+
26
+ do {
27
+ ++aIdx;
28
+ aNum = aNum * 10 + aChar - DIGIT_0;
29
+ aChar = aStr.charCodeAt(aIdx);
30
+ } while (isDigit(aChar) && aNum > 0);
31
+
32
+ var bNum = 0;
33
+
34
+ do {
35
+ ++bIdx;
36
+ bNum = bNum * 10 + bChar - DIGIT_0;
37
+ bChar = bStr.charCodeAt(bIdx);
38
+ } while (isDigit(bChar) && bNum > 0);
39
+
40
+ if (aNum < bNum) {
41
+ return -1;
42
+ }
43
+
44
+ if (aNum > bNum) {
45
+ return 1;
46
+ }
47
+ } else {
48
+ if (aChar < bChar) {
49
+ return -1;
50
+ }
51
+
52
+ if (aChar > bChar) {
53
+ return 1;
54
+ }
55
+
56
+ ++aIdx;
57
+ ++bIdx;
58
+ }
59
+ }
60
+
61
+ return aStr.length - bStr.length;
62
+ }
63
+
64
+ var DIGIT_0 = 48;
65
+ var DIGIT_9 = 57;
66
+
67
+ function isDigit(code) {
68
+ return !isNaN(code) && DIGIT_0 <= code && code <= DIGIT_9;
69
+ }
@@ -0,0 +1,59 @@
1
+ // @flow strict
2
+ /**
3
+ * Returns a number indicating whether a reference string comes before, or after,
4
+ * or is the same as the given string in natural sort order.
5
+ *
6
+ * See: https://en.wikipedia.org/wiki/Natural_sort_order
7
+ *
8
+ */
9
+ export default function naturalCompare(aStr: string, bStr: string): number {
10
+ let aIdx = 0;
11
+ let bIdx = 0;
12
+
13
+ while (aIdx < aStr.length && bIdx < bStr.length) {
14
+ let aChar = aStr.charCodeAt(aIdx);
15
+ let bChar = bStr.charCodeAt(bIdx);
16
+
17
+ if (isDigit(aChar) && isDigit(bChar)) {
18
+ let aNum = 0;
19
+ do {
20
+ ++aIdx;
21
+ aNum = aNum * 10 + aChar - DIGIT_0;
22
+ aChar = aStr.charCodeAt(aIdx);
23
+ } while (isDigit(aChar) && aNum > 0);
24
+
25
+ let bNum = 0;
26
+ do {
27
+ ++bIdx;
28
+ bNum = bNum * 10 + bChar - DIGIT_0;
29
+ bChar = bStr.charCodeAt(bIdx);
30
+ } while (isDigit(bChar) && bNum > 0);
31
+
32
+ if (aNum < bNum) {
33
+ return -1;
34
+ }
35
+
36
+ if (aNum > bNum) {
37
+ return 1;
38
+ }
39
+ } else {
40
+ if (aChar < bChar) {
41
+ return -1;
42
+ }
43
+ if (aChar > bChar) {
44
+ return 1;
45
+ }
46
+ ++aIdx;
47
+ ++bIdx;
48
+ }
49
+ }
50
+
51
+ return aStr.length - bStr.length;
52
+ }
53
+
54
+ const DIGIT_0 = 48;
55
+ const DIGIT_9 = 57;
56
+
57
+ function isDigit(code: number): boolean {
58
+ return !isNaN(code) && DIGIT_0 <= code && code <= DIGIT_9;
59
+ }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Returns a number indicating whether a reference string comes before, or after,
3
+ * or is the same as the given string in natural sort order.
4
+ *
5
+ * See: https://en.wikipedia.org/wiki/Natural_sort_order
6
+ *
7
+ */
8
+ export default function naturalCompare(aStr, bStr) {
9
+ var aIdx = 0;
10
+ var bIdx = 0;
11
+
12
+ while (aIdx < aStr.length && bIdx < bStr.length) {
13
+ var aChar = aStr.charCodeAt(aIdx);
14
+ var bChar = bStr.charCodeAt(bIdx);
15
+
16
+ if (isDigit(aChar) && isDigit(bChar)) {
17
+ var aNum = 0;
18
+
19
+ do {
20
+ ++aIdx;
21
+ aNum = aNum * 10 + aChar - DIGIT_0;
22
+ aChar = aStr.charCodeAt(aIdx);
23
+ } while (isDigit(aChar) && aNum > 0);
24
+
25
+ var bNum = 0;
26
+
27
+ do {
28
+ ++bIdx;
29
+ bNum = bNum * 10 + bChar - DIGIT_0;
30
+ bChar = bStr.charCodeAt(bIdx);
31
+ } while (isDigit(bChar) && bNum > 0);
32
+
33
+ if (aNum < bNum) {
34
+ return -1;
35
+ }
36
+
37
+ if (aNum > bNum) {
38
+ return 1;
39
+ }
40
+ } else {
41
+ if (aChar < bChar) {
42
+ return -1;
43
+ }
44
+
45
+ if (aChar > bChar) {
46
+ return 1;
47
+ }
48
+
49
+ ++aIdx;
50
+ ++bIdx;
51
+ }
52
+ }
53
+
54
+ return aStr.length - bStr.length;
55
+ }
56
+ var DIGIT_0 = 48;
57
+ var DIGIT_9 = 57;
58
+
59
+ function isDigit(code) {
60
+ return !isNaN(code) && DIGIT_0 <= code && code <= DIGIT_9;
61
+ }
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = safeArrayFrom;
7
+
8
+ var _symbols = require("../polyfills/symbols.js");
9
+
10
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
11
+
12
+ /**
13
+ * Safer version of `Array.from` that return `null` if value isn't convertible to array.
14
+ * Also protects against Array-like objects without items.
15
+ *
16
+ * @example
17
+ *
18
+ * safeArrayFrom([ 1, 2, 3 ]) // [1, 2, 3]
19
+ * safeArrayFrom('ABC') // null
20
+ * safeArrayFrom({ length: 1 }) // null
21
+ * safeArrayFrom({ length: 1, 0: 'Alpha' }) // ['Alpha']
22
+ * safeArrayFrom({ key: 'value' }) // null
23
+ * safeArrayFrom(new Map()) // []
24
+ *
25
+ */
26
+ function safeArrayFrom(collection) {
27
+ var mapFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (item) {
28
+ return item;
29
+ };
30
+
31
+ if (collection == null || _typeof(collection) !== 'object') {
32
+ return null;
33
+ }
34
+
35
+ if (Array.isArray(collection)) {
36
+ return collection.map(mapFn);
37
+ } // Is Iterable?
38
+
39
+
40
+ var iteratorMethod = collection[_symbols.SYMBOL_ITERATOR];
41
+
42
+ if (typeof iteratorMethod === 'function') {
43
+ // $FlowFixMe[incompatible-use]
44
+ var iterator = iteratorMethod.call(collection);
45
+ var result = [];
46
+ var step;
47
+
48
+ for (var i = 0; !(step = iterator.next()).done; ++i) {
49
+ result.push(mapFn(step.value, i));
50
+ }
51
+
52
+ return result;
53
+ } // Is Array like?
54
+
55
+
56
+ var length = collection.length;
57
+
58
+ if (typeof length === 'number' && length >= 0 && length % 1 === 0) {
59
+ var _result = [];
60
+
61
+ for (var _i = 0; _i < length; ++_i) {
62
+ if (!Object.prototype.hasOwnProperty.call(collection, _i)) {
63
+ return null;
64
+ }
65
+
66
+ _result.push(mapFn(collection[String(_i)], _i));
67
+ }
68
+
69
+ return _result;
70
+ }
71
+
72
+ return null;
73
+ }
@@ -0,0 +1,59 @@
1
+ // @flow strict
2
+ import { SYMBOL_ITERATOR } from '../polyfills/symbols';
3
+
4
+ /**
5
+ * Safer version of `Array.from` that return `null` if value isn't convertible to array.
6
+ * Also protects against Array-like objects without items.
7
+ *
8
+ * @example
9
+ *
10
+ * safeArrayFrom([ 1, 2, 3 ]) // [1, 2, 3]
11
+ * safeArrayFrom('ABC') // null
12
+ * safeArrayFrom({ length: 1 }) // null
13
+ * safeArrayFrom({ length: 1, 0: 'Alpha' }) // ['Alpha']
14
+ * safeArrayFrom({ key: 'value' }) // null
15
+ * safeArrayFrom(new Map()) // []
16
+ *
17
+ */
18
+ export default function safeArrayFrom<T>(
19
+ collection: mixed,
20
+ mapFn: (elem: mixed, index: number) => T = (item) => ((item: any): T),
21
+ ): Array<T> | null {
22
+ if (collection == null || typeof collection !== 'object') {
23
+ return null;
24
+ }
25
+
26
+ if (Array.isArray(collection)) {
27
+ return collection.map(mapFn);
28
+ }
29
+
30
+ // Is Iterable?
31
+ const iteratorMethod = collection[SYMBOL_ITERATOR];
32
+ if (typeof iteratorMethod === 'function') {
33
+ // $FlowFixMe[incompatible-use]
34
+ const iterator = iteratorMethod.call(collection);
35
+ const result = [];
36
+ let step;
37
+
38
+ for (let i = 0; !(step = iterator.next()).done; ++i) {
39
+ result.push(mapFn(step.value, i));
40
+ }
41
+ return result;
42
+ }
43
+
44
+ // Is Array like?
45
+ const length = collection.length;
46
+ if (typeof length === 'number' && length >= 0 && length % 1 === 0) {
47
+ const result = [];
48
+ for (let i = 0; i < length; ++i) {
49
+ if (!Object.prototype.hasOwnProperty.call(collection, i)) {
50
+ return null;
51
+ }
52
+ result.push(mapFn(collection[String(i)], i));
53
+ }
54
+
55
+ return result;
56
+ }
57
+
58
+ return null;
59
+ }
@@ -0,0 +1,66 @@
1
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
2
+
3
+ import { SYMBOL_ITERATOR } from "../polyfills/symbols.mjs";
4
+ /**
5
+ * Safer version of `Array.from` that return `null` if value isn't convertible to array.
6
+ * Also protects against Array-like objects without items.
7
+ *
8
+ * @example
9
+ *
10
+ * safeArrayFrom([ 1, 2, 3 ]) // [1, 2, 3]
11
+ * safeArrayFrom('ABC') // null
12
+ * safeArrayFrom({ length: 1 }) // null
13
+ * safeArrayFrom({ length: 1, 0: 'Alpha' }) // ['Alpha']
14
+ * safeArrayFrom({ key: 'value' }) // null
15
+ * safeArrayFrom(new Map()) // []
16
+ *
17
+ */
18
+
19
+ export default function safeArrayFrom(collection) {
20
+ var mapFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (item) {
21
+ return item;
22
+ };
23
+
24
+ if (collection == null || _typeof(collection) !== 'object') {
25
+ return null;
26
+ }
27
+
28
+ if (Array.isArray(collection)) {
29
+ return collection.map(mapFn);
30
+ } // Is Iterable?
31
+
32
+
33
+ var iteratorMethod = collection[SYMBOL_ITERATOR];
34
+
35
+ if (typeof iteratorMethod === 'function') {
36
+ // $FlowFixMe[incompatible-use]
37
+ var iterator = iteratorMethod.call(collection);
38
+ var result = [];
39
+ var step;
40
+
41
+ for (var i = 0; !(step = iterator.next()).done; ++i) {
42
+ result.push(mapFn(step.value, i));
43
+ }
44
+
45
+ return result;
46
+ } // Is Array like?
47
+
48
+
49
+ var length = collection.length;
50
+
51
+ if (typeof length === 'number' && length >= 0 && length % 1 === 0) {
52
+ var _result = [];
53
+
54
+ for (var _i = 0; _i < length; ++_i) {
55
+ if (!Object.prototype.hasOwnProperty.call(collection, _i)) {
56
+ return null;
57
+ }
58
+
59
+ _result.push(mapFn(collection[String(_i)], _i));
60
+ }
61
+
62
+ return _result;
63
+ }
64
+
65
+ return null;
66
+ }
@@ -5,6 +5,10 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = suggestionList;
7
7
 
8
+ var _naturalCompare = _interopRequireDefault(require("./naturalCompare.js"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
8
12
  /**
9
13
  * Given an invalid input string and a list of valid options, returns a filtered
10
14
  * list of valid options sorted based on their similarity with the input.
@@ -25,7 +29,7 @@ function suggestionList(input, options) {
25
29
 
26
30
  return Object.keys(optionsByDistance).sort(function (a, b) {
27
31
  var distanceDiff = optionsByDistance[a] - optionsByDistance[b];
28
- return distanceDiff !== 0 ? distanceDiff : a.localeCompare(b);
32
+ return distanceDiff !== 0 ? distanceDiff : (0, _naturalCompare.default)(a, b);
29
33
  });
30
34
  }
31
35
  /**
@@ -1,4 +1,6 @@
1
1
  // @flow strict
2
+ import naturalCompare from './naturalCompare';
3
+
2
4
  /**
3
5
  * Given an invalid input string and a list of valid options, returns a filtered
4
6
  * list of valid options sorted based on their similarity with the input.
@@ -20,7 +22,7 @@ export default function suggestionList(
20
22
 
21
23
  return Object.keys(optionsByDistance).sort((a, b) => {
22
24
  const distanceDiff = optionsByDistance[a] - optionsByDistance[b];
23
- return distanceDiff !== 0 ? distanceDiff : a.localeCompare(b);
25
+ return distanceDiff !== 0 ? distanceDiff : naturalCompare(a, b);
24
26
  });
25
27
  }
26
28