UNPKG

2.09 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.isRootType = exports.copyOwnProps = exports.copyOwnPropsIfNotPresent = exports.isObject = exports.makeRef = exports.takeRandom = exports.randomListLength = exports.uuidv4 = void 0;
4const utils_1 = require("@graphql-tools/utils");
5function uuidv4() {
6 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
7 const r = (Math.random() * 16) | 0;
8 // eslint-disable-next-line eqeqeq
9 const v = c == 'x' ? r : (r & 0x3) | 0x8;
10 return v.toString(16);
11 });
12}
13exports.uuidv4 = uuidv4;
14const randomListLength = () => {
15 // Mocking has always returned list of length 2 by default
16 // return 1 + Math.round(Math.random() * 10)
17 return 2;
18};
19exports.randomListLength = randomListLength;
20const takeRandom = (arr) => arr[Math.floor(Math.random() * arr.length)];
21exports.takeRandom = takeRandom;
22function makeRef(typeName, key) {
23 return { $ref: { key, typeName } };
24}
25exports.makeRef = makeRef;
26function isObject(thing) {
27 return thing === Object(thing) && !Array.isArray(thing);
28}
29exports.isObject = isObject;
30function copyOwnPropsIfNotPresent(target, source) {
31 for (const prop of Object.getOwnPropertyNames(source)) {
32 if (!Object.getOwnPropertyDescriptor(target, prop)) {
33 const propertyDescriptor = Object.getOwnPropertyDescriptor(source, prop);
34 Object.defineProperty(target, prop, propertyDescriptor == null ? {} : propertyDescriptor);
35 }
36 }
37}
38exports.copyOwnPropsIfNotPresent = copyOwnPropsIfNotPresent;
39function copyOwnProps(target, ...sources) {
40 for (const source of sources) {
41 let chain = source;
42 while (chain != null) {
43 copyOwnPropsIfNotPresent(target, chain);
44 chain = Object.getPrototypeOf(chain);
45 }
46 }
47 return target;
48}
49exports.copyOwnProps = copyOwnProps;
50const isRootType = (type, schema) => {
51 const rootTypeNames = (0, utils_1.getRootTypeNames)(schema);
52 return rootTypeNames.has(type.name);
53};
54exports.isRootType = isRootType;