UNPKG

2.29 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.assertSome = exports.isSome = exports.compareNodes = exports.nodeToString = exports.compareStrings = exports.isValidPath = exports.isDocumentString = exports.asArray = void 0;
4const graphql_1 = require("graphql");
5const asArray = (fns) => (Array.isArray(fns) ? fns : fns ? [fns] : []);
6exports.asArray = asArray;
7const invalidDocRegex = /\.[a-z0-9]+$/i;
8function 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}
27exports.isDocumentString = isDocumentString;
28const invalidPathRegex = /[‘“!%^<=>`]/;
29function isValidPath(str) {
30 return typeof str === 'string' && !invalidPathRegex.test(str);
31}
32exports.isValidPath = isValidPath;
33function 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}
42exports.compareStrings = compareStrings;
43function 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}
57exports.nodeToString = nodeToString;
58function 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}
66exports.compareNodes = compareNodes;
67function isSome(input) {
68 return input != null;
69}
70exports.isSome = isSome;
71function assertSome(input, message = 'Value should be something') {
72 if (input == null) {
73 throw new Error(message);
74 }
75}
76exports.assertSome = assertSome;