UNPKG

1.1 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true,
5});
6exports.sortValueNode = sortValueNode;
7
8var _naturalCompare = require('../jsutils/naturalCompare.js');
9
10var _kinds = require('../language/kinds.js');
11
12/**
13 * Sort ValueNode.
14 *
15 * This function returns a sorted copy of the given ValueNode.
16 *
17 * @internal
18 */
19function sortValueNode(valueNode) {
20 switch (valueNode.kind) {
21 case _kinds.Kind.OBJECT:
22 return { ...valueNode, fields: sortFields(valueNode.fields) };
23
24 case _kinds.Kind.LIST:
25 return { ...valueNode, values: valueNode.values.map(sortValueNode) };
26
27 case _kinds.Kind.INT:
28 case _kinds.Kind.FLOAT:
29 case _kinds.Kind.STRING:
30 case _kinds.Kind.BOOLEAN:
31 case _kinds.Kind.NULL:
32 case _kinds.Kind.ENUM:
33 case _kinds.Kind.VARIABLE:
34 return valueNode;
35 }
36}
37
38function sortFields(fields) {
39 return fields
40 .map((fieldNode) => ({
41 ...fieldNode,
42 value: sortValueNode(fieldNode.value),
43 }))
44 .sort((fieldA, fieldB) =>
45 (0, _naturalCompare.naturalCompare)(fieldA.name.value, fieldB.name.value),
46 );
47}