helpers.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.assertSome = exports.isSome = exports.compareNodes = exports.nodeToString = exports.compareStrings = exports.isValidPath = exports.isDocumentString = exports.asArray = void 0;
  4. const graphql_1 = require("graphql");
  5. const asArray = (fns) => (Array.isArray(fns) ? fns : fns ? [fns] : []);
  6. exports.asArray = asArray;
  7. const invalidDocRegex = /\.[a-z0-9]+$/i;
  8. function isDocumentString(str) {
  9. if (typeof str !== 'string') {
  10. return false;
  11. }
  12. // XXX: is-valid-path or is-glob treat SDL as a valid path
  13. // (`scalar Date` for example)
  14. // this why checking the extension is fast enough
  15. // and prevent from parsing the string in order to find out
  16. // if the string is a SDL
  17. if (invalidDocRegex.test(str)) {
  18. return false;
  19. }
  20. try {
  21. (0, graphql_1.parse)(str);
  22. return true;
  23. }
  24. catch (e) { }
  25. return false;
  26. }
  27. exports.isDocumentString = isDocumentString;
  28. const invalidPathRegex = /[‘“!%^<=>`]/;
  29. function isValidPath(str) {
  30. return typeof str === 'string' && !invalidPathRegex.test(str);
  31. }
  32. exports.isValidPath = isValidPath;
  33. function compareStrings(a, b) {
  34. if (String(a) < String(b)) {
  35. return -1;
  36. }
  37. if (String(a) > String(b)) {
  38. return 1;
  39. }
  40. return 0;
  41. }
  42. exports.compareStrings = compareStrings;
  43. function nodeToString(a) {
  44. var _a, _b;
  45. let name;
  46. if ('alias' in a) {
  47. name = (_a = a.alias) === null || _a === void 0 ? void 0 : _a.value;
  48. }
  49. if (name == null && 'name' in a) {
  50. name = (_b = a.name) === null || _b === void 0 ? void 0 : _b.value;
  51. }
  52. if (name == null) {
  53. name = a.kind;
  54. }
  55. return name;
  56. }
  57. exports.nodeToString = nodeToString;
  58. function compareNodes(a, b, customFn) {
  59. const aStr = nodeToString(a);
  60. const bStr = nodeToString(b);
  61. if (typeof customFn === 'function') {
  62. return customFn(aStr, bStr);
  63. }
  64. return compareStrings(aStr, bStr);
  65. }
  66. exports.compareNodes = compareNodes;
  67. function isSome(input) {
  68. return input != null;
  69. }
  70. exports.isSome = isSome;
  71. function assertSome(input, message = 'Value should be something') {
  72. if (input == null) {
  73. throw new Error(message);
  74. }
  75. }
  76. exports.assertSome = assertSome;