-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.js
179 lines (166 loc) · 5.95 KB
/
utils.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
const mustache = require('mustache');
const nodemailer = require('nodemailer');
const template= require('./template');
const config=require('./config');
var ObjectId = require('mongodb').ObjectID;
var removeDiacritics=require('./diacritics');
let utilFunctions = {
//munge:function (val) {
//if (val) {
//val = val.trim().toLowerCase().replace(/\//g, " ").replace("["," ").replace("]"," ").replace("{"," ").replace("}"," ").replace("("," ").replace(")"," ").replace("|"," ").replace("&"," ").replace("*"," ").replace(":"," ").replace(";"," ").replace("~"," ").replace("@"," ").replace("#"," ").replace("="," ").replace("\r\n"," ").replace("\n"," ");
//// strip
//val = val.trim().replace(/[^0-9 'a-z]/gi, '');
//val = removeDiacritics(val);
//val = val.slice(0,139).trim();
//if (val.indexOf("mnemo's")==0) val = val.replace("mnemo's","nemo's");
//return val;
//} else {
//return ''
//}
//},
//mungeLong:function (val) {
//if (val) {
//val = val.trim().toLowerCase().replace(/\//g, " ").replace("["," ").replace("]"," ").replace("{"," ").replace("}"," ").replace("("," ").replace(")"," ").replace("|"," ").replace("&"," ").replace("*"," ").replace(":"," ").replace(";"," ").replace("~"," ").replace("@"," ").replace("#"," ").replace("="," ").replace("\r\n"," ").replace("\n"," ");
//// strip
//val = val.trim().replace(/[^0-9 'a-z]/gi, '');
//val = removeDiacritics(val);
//val = val.trim();
//return val;
//} else {
//return ''
//}
//},
//similarity : function(s1, s2) {
//var longer = s1;
//var shorter = s2;
//if (s1.length < s2.length) {
//longer = s2;
//shorter = s1;
//}
//var longerLength = longer.length;
//if (longerLength == 0) {
//return 1.0;
//}
//return (longerLength - utilFunctions.editDistance(longer, shorter)) / parseFloat(longerLength);
//},
//editDistance : function(s1, s2) {
//s1 = s1.toLowerCase();
//s2 = s2.toLowerCase();
//var costs = new Array();
//for (var i = 0; i <= s1.length; i++) {
//var lastValue = i;
//for (var j = 0; j <= s2.length; j++) {
//if (i == 0)
//costs[j] = j;
//else {
//if (j > 0) {
//var newValue = costs[j - 1];
//if (s1.charAt(i - 1) != s2.charAt(j - 1))
//newValue = Math.min(Math.min(newValue, lastValue),
//costs[j]) + 1;
//costs[j - 1] = lastValue;
//lastValue = newValue;
//}
//}
//}
//if (i > 0)
//costs[s2.length] = lastValue;
//}
//return costs[s2.length];
//},
//intersect : function() {
//var set = {};
//[].forEach.call(arguments, function(a,i){
//var tokens = a.match(/\w+/g);
//if (!i) {
//tokens.forEach(function(t){ set[t]=1 });
//} else {
//for (var k in set){
//if (tokens.indexOf(k)<0) delete set[k];
//}
//}
//});
//return Object.keys(set);
//},
sendMail : function(from,to,subject,html) {
return new Promise(function(resolve,reject) {
var transporter = nodemailer.createTransport(config.transport);
var mailOptions = {
from: from,
to: to,
subject: subject,
html: html
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
//console.log(error);
reject(error)
res.send('FAIL');
} else {
console.log('Email sent: ' + info.response);
resolve(info);
//res.send('OK');
}
});
})
},
renderLogin: function renderLogin(req,vars) {
let templateVars = Object.assign({},req.body);
templateVars = Object.assign(templateVars,vars);
templateVars.clientId = config.clientId;
templateVars.clientSecret = config.clientSecret;
//templateVars.successUrl = config.successUrl;
return mustache.render(template.login,templateVars);
},
sendTemplate: function sendTemplate(req,res,vars) {
let templateVars = Object.assign({},req.query);
templateVars = Object.assign(templateVars,vars);
res.send(mustache.render(template.layout,templateVars));
},
createIndexes: function (json) {
let tags = {};
// collate quizzes and tags
////console.log(json);
let questions = [];
for (var questionKey in json['questions']) {
////console.log('index q '+questionKey);
const question = json['questions'][questionKey]
if (question && question.question && question.question.length > 0) {
// //console.log(question);
//question.tags = question.tags ? question.tags.trim().toLowerCase() : '';
var id = question._id ? question._id : new ObjectId();
question._id = id;
var tagList = [];
if (question.tags) {
let splitList = question.tags.trim().toLowerCase().split(",");
splitList.forEach(function(tag) {
tagList.push(tag.trim());
});
question.tags = tagList;
}
// //console.log(question.tags);
////console.log(question.access);
// only generate tags for public questions
if (question.access==="public") {
// //console.log('public');
for (var tagKey in tagList) {
// //console.log(tagList[tagKey]);
// //console.log(['taglist each',tagKey,tagList[tagKey]]);
var tag = tagList[tagKey].trim().toLowerCase();
// //console.log(tag);
if (tag.length > 0) {
if (! (Array.isArray(tags[tag]))) {
tags[tag] = 1
}
}
}
}
questions.push(question);
}
}
////console.log('CREATED IINDEX');
////console.log(tags);
return {'questions':questions,'tags':tags};
},
}
module.exports = utilFunctions;