UNPKG

2.52 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 = /[‘“!%^<>`\n]/;
29/**
30 * Checkes whether the `str` contains any path illegal characters.
31 *
32 * A string may sometimes look like a path but is not (like an SDL of a simple
33 * GraphQL schema). To make sure we don't yield false-positives in such cases,
34 * we disallow new lines in paths (even though most Unix systems support new
35 * lines in file names).
36 */
37function isValidPath(str) {
38 return typeof str === 'string' && !invalidPathRegex.test(str);
39}
40exports.isValidPath = isValidPath;
41function compareStrings(a, b) {
42 if (String(a) < String(b)) {
43 return -1;
44 }
45 if (String(a) > String(b)) {
46 return 1;
47 }
48 return 0;
49}
50exports.compareStrings = compareStrings;
51function nodeToString(a) {
52 let name;
53 if ('alias' in a) {
54 name = a.alias?.value;
55 }
56 if (name == null && 'name' in a) {
57 name = a.name?.value;
58 }
59 if (name == null) {
60 name = a.kind;
61 }
62 return name;
63}
64exports.nodeToString = nodeToString;
65function compareNodes(a, b, customFn) {
66 const aStr = nodeToString(a);
67 const bStr = nodeToString(b);
68 if (typeof customFn === 'function') {
69 return customFn(aStr, bStr);
70 }
71 return compareStrings(aStr, bStr);
72}
73exports.compareNodes = compareNodes;
74function isSome(input) {
75 return input != null;
76}
77exports.isSome = isSome;
78function assertSome(input, message = 'Value should be something') {
79 if (input == null) {
80 throw new Error(message);
81 }
82}
83exports.assertSome = assertSome;