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