This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
forked from APIs-guru/openapi-directory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortParameters.js
58 lines (52 loc) · 1.72 KB
/
sortParameters.js
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
var util = require('util');
var _ = require('lodash');
function sortPFunc(a,b) {
if (a.$ref && b.$ref) {
if (a.$ref < b.$ref) return -1;
if (a.$ref > b.$ref) return +1;
return 0;
}
if (a.name < b.name) return -1;
if (a.name > b.name) return +1;
if (a.in < b.in) return -1;
if (a.in > b.in) return +1;
return 0;
}
function sortParameters(swagger, comparison, source) {
for (var p in swagger.paths) {
var pathItem = swagger.paths[p];
for (var o in pathItem) {
if (o === 'parameters') {
pathItem.parameters = pathItem.parameters.sort(sortPFunc);
if (comparison && comparison.paths[p] && comparison.paths[p].parameters &&
_.isEqual(pathItem.parameters, comparison.paths[p].parameters)) {
pathItem.parameters = source.paths[p].parameters;
}
}
else {
var op = pathItem[o];
if (op.parameters) {
op.parameters = op.parameters.sort(sortPFunc);
if (comparison && comparison.paths[p] && comparison.paths[p][o] && comparison.paths[p][o].parameters &&
_.isEqual(op.parameters, comparison.paths[p][o].parameters)) {
op.parameters = source.paths[p][o].parameters;
}
}
}
}
}
}
function sortTFunc(a,b) {
if (a.name < b.name) return -1;
if (a.name > b.name) return +1;
return 0;
}
function sortTags(swagger) {
if (swagger.tags) {
swagger.tags = swagger.tags.sort(sortTFunc);
}
}
module.exports = {
sortParameters : sortParameters,
sortTags : sortTags
};