-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind.js
More file actions
68 lines (50 loc) · 1.03 KB
/
find.js
File metadata and controls
68 lines (50 loc) · 1.03 KB
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
/**
* Find attributes in Swagger specs.
*/
'use strict';
/**
* Module dependencies.
*/
var lodash = require('lodash');
var constants = require('./constants');
/**
* Find model.
*/
function model(obj) {
if (!obj) return;
if (obj.$ref) {
obj = obj.$ref;
} else if (obj.type === 'array' && obj.items && obj.items.$ref) {
obj = obj.items.$ref;
} else if (obj.type) {
obj = obj.type;
}
// ensure valid type
if (typeof obj !== 'string') return;
// ensure non-builtin type
if (constants.notModel.indexOf(obj) >= 0) return;
return obj;
}
/**
* Find models.
*/
function models(spec) {
var ids = {};
if (!spec) return ids;
var add = function(obj) {
var type = model(obj);
if (type) ids[type] = true;
};
add(spec);
if (spec.properties) {
lodash.forOwn(spec.properties, function(p) { add(p); });
} else if (spec.parameters) {
spec.parameters.forEach(function(p) { add(p); });
}
return Object.keys(ids);
}
/**
* Expose find.
*/
exports.model = model;
exports.models = models;