forked from apache/cordova-registry-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
208 lines (188 loc) · 6.49 KB
/
app.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var couchapp = require('couchapp')
, path = require('path')
ddoc =
{ _id:'_design/ui'
, rewrites :
[ {from:"/", to:'index.html'}
, {from:"/favicon.ico", to:'../../npm/favicon.ico'}
, {from:"/api", to:'../../'}
, {from:"/api/*", to:'../../*'}
, {from: "/downloads/*", to: '../../../downloads/*'}
, {from:"/*", to:'*'}
]
, lists : {}
}
function searcher(doc) {
//if(word) ? id = word : id=doc._id;
if (doc['dist-tags'] && doc['dist-tags'].latest) {
var dist = doc.versions[doc['dist-tags'].latest];
//We have latest version, now get the platforms available.
var version = doc['dist-tags'].latest;
var engines = dist && dist.engines ? dist.engines : '';
var platforms = dist && dist.platforms ? dist.platforms : '';
var obj = {
name: doc.name
, description: doc.description
, version: version
, platforms: platforms
, engines: engines
};
emit(doc._id, obj);
}
}
function packageSearch (doc) {
var descriptionBlacklist =
[ "for"
, "and"
, "in"
, "are"
, "is"
, "it"
, "do"
, "of"
, "on"
, "the"
, "to"
, "as"
]
if (doc['dist-tags'] && doc['dist-tags'].latest) {
var dist = doc.versions[doc['dist-tags'].latest];
//We have latest version, now get the platforms available.
var version = doc['dist-tags'].latest;
var engines = dist && dist.engines ? dist.engines : '';
var platforms = dist && dist.platforms ? dist.platforms : '';
var obj = {
name: doc.name
, description: doc.description
, version: version
, platforms: platforms
, engines: engines
};
}
if (doc.name) { // There aren't any better attributes for check if isPackage()
if (doc.name) {
var names = [doc.name];
if (doc.name.indexOf('-') !== -1) doc.name.split('-').forEach(function (n) {names.push(n)});
if (doc.name.indexOf('_') !== -1) doc.name.split('_').forEach(function (n) {names.push(n)});
names.forEach(function (n) {
if (n.length > 1) emit(n.toLowerCase(), obj);
});
}
if (doc._id) {
var ids = [doc._id];
if (doc._id.indexOf('.') !== -1) doc._id.split('.').forEach(function (n) {ids.push(n)});
ids.forEach(function (id) {
if (id.length > 1) emit(id.toLowerCase(), obj);
});
}
if (doc['dist-tags'] && doc['dist-tags'].latest && (
doc.versions[doc['dist-tags'].latest].keywords)) {
var keywords = (doc.versions[doc['dist-tags'].latest].keywords)
keywords.forEach(function (keyword) {
keyword.split(' ').forEach(function (k) {
if (k.length > 0) emit(k.toLowerCase(), obj);
});
})
}
if (doc.description) {
doc.description.split(' ').forEach(function (d) {
d = d.toLowerCase();
while (d.indexOf('.') !== -1) d = d.replace('.', '');
while (d.indexOf('\n') !== -1) d = d.replace('\n', '');
while (d.indexOf('\r') !== -1) d = d.replace('\r', '');
while (d.indexOf('`') !== -1) d = d.replace('`', '');
while (d.indexOf('_') !== -1) d = d.replace('_', '');
while (d.indexOf('"') !== -1) d = d.replace('"', '');
while (d.indexOf('\'') !== -1) d = d.replace('\'', '');
while (d.indexOf('(') !== -1) d = d.replace('(', '');
while (d.indexOf(')') !== -1) d = d.replace(')', '');
while (d.indexOf('[') !== -1) d = d.replace('[', '');
while (d.indexOf(']') !== -1) d = d.replace(']', '');
while (d.indexOf('{') !== -1) d = d.replace('{', '');
while (d.indexOf('}') !== -1) d = d.replace('}', '');
while (d.indexOf('*') !== -1) d = d.replace('*', '');
while (d.indexOf('%') !== -1) d = d.replace('%', '');
while (d.indexOf('+') !== -1) d = d.replace('+', '');
if (descriptionBlacklist.indexOf(d) !== -1) d = '';
if (d.length > 1) emit(d, obj);
})
}
}
}
function dependencies (doc) {
if (doc['dist-tags'] && doc['dist-tags'].latest) {
var dist = doc.versions[doc['dist-tags'].latest];
for (i in dist.dependencies) {
emit(i, dist.dependencies[i])
}
}
}
ddoc.views =
{ search: { map: packageSearch }
, searcher: { map: searcher}
, dependencies: {map: dependencies, reduce:"_count"}
, updated: {map: function (doc) {
var l = doc["dist-tags"].latest
, t = doc.time && doc.time[l]
if (t) emit(t, 1)
}}
, author:
{ map: function (doc) {
if (doc.author && doc.author.name) {
emit(doc.author.name, 1);
}
}
, reduce: "_sum"
}
, analytics:
{ map: function (doc) {
if (doc.time) {
if (doc.time.modified) {
emit(['latest', doc.time.modified], 1);
}
if (doc.time.created) {
emit(['created', doc.time.created], 1);
}
for (i in doc.time) {
emit(['update', doc.time[i]], 1);
}
}
}
, reduce: "_sum"
}
}
;
ddoc.lists.dependencies_limit = function(head, req) {
var deps = [];
while(row = getRow()) {
deps.push(row);
}
var sorted = deps.sort(function(a,b) { return req.query.descending !== "true" ? a.value - b.value : b.value - a.value; });
// using list_Limit rather than limit because limit appears to limit the initial view set
// assuming there's a supported convention but using this for now
var limit = req.query.list_limit && parseInt(req.query.list_limit);
send(JSON.stringify({ total_rows: deps.length, rows: limit ? sorted.splice(0, limit) : sorted}));
};
ddoc.lists.search = function(head, req) {
Object.keys = Object.keys
|| function (o) { var a = []
for (var i in o) a.push(i)
return a }
var set = {};
var rows = [];
while(row = getRow()) {
set[row.id] = { key: row.id, count: set[row.id] ? set[row.id].count + 1 : 1, value: row.value };
}
var keys = Object.keys(set);
for(var i=0; i<keys.length; i++) {
rows.push(set[keys[i]]);
}
send(JSON.stringify({ rows: rows} ));
};
// ddoc.validate_doc_update = function (newDoc, oldDoc, userCtx) {
// if (newDoc._deleted === true && userCtx.roles.indexOf('_admin') === -1) {
// throw "Only admin can delete documents on this database.";
// }
// }
couchapp.loadAttachments(ddoc, path.join(__dirname, 'attachments'));
module.exports = ddoc;