1 | import { parse } from 'graphql';
|
2 | export const asArray = (fns) => (Array.isArray(fns) ? fns : fns ? [fns] : []);
|
3 | const invalidDocRegex = /\.[a-z0-9]+$/i;
|
4 | export function isDocumentString(str) {
|
5 | if (typeof str !== 'string') {
|
6 | return false;
|
7 | }
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | if (invalidDocRegex.test(str)) {
|
14 | return false;
|
15 | }
|
16 | try {
|
17 | parse(str);
|
18 | return true;
|
19 | }
|
20 | catch (e) { }
|
21 | return false;
|
22 | }
|
23 | const invalidPathRegex = /[‘“!%^<>`\n]/;
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | export function isValidPath(str) {
|
33 | return typeof str === 'string' && !invalidPathRegex.test(str);
|
34 | }
|
35 | export function compareStrings(a, b) {
|
36 | if (String(a) < String(b)) {
|
37 | return -1;
|
38 | }
|
39 | if (String(a) > String(b)) {
|
40 | return 1;
|
41 | }
|
42 | return 0;
|
43 | }
|
44 | export function nodeToString(a) {
|
45 | let name;
|
46 | if ('alias' in a) {
|
47 | name = a.alias?.value;
|
48 | }
|
49 | if (name == null && 'name' in a) {
|
50 | name = a.name?.value;
|
51 | }
|
52 | if (name == null) {
|
53 | name = a.kind;
|
54 | }
|
55 | return name;
|
56 | }
|
57 | export function compareNodes(a, b, customFn) {
|
58 | const aStr = nodeToString(a);
|
59 | const bStr = nodeToString(b);
|
60 | if (typeof customFn === 'function') {
|
61 | return customFn(aStr, bStr);
|
62 | }
|
63 | return compareStrings(aStr, bStr);
|
64 | }
|
65 | export function isSome(input) {
|
66 | return input != null;
|
67 | }
|
68 | export function assertSome(input, message = 'Value should be something') {
|
69 | if (input == null) {
|
70 | throw new Error(message);
|
71 | }
|
72 | }
|