/
utils.js
211 lines (183 loc) · 5.99 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
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
209
210
211
var fs = require('fs'),
path = require('path'),
crypto = require('crypto'),
extend = require('soak').mixin,
inherit = require('soak').inherit,
EventEmitter = require('events').EventEmitter;
module.exports = {
// Create a base observable object that inherits from EventEmitter. This
// allows us to create new objects that can trigger events very easily.
//
// Example:
//
// var Store = Observable.extend({
// save: function () {}
// });
Observable: inherit(EventEmitter),
// Helper function for creating an index file in a directory. It loads all
// modules within the provided directory excluding the file provided as
// "indexpath". These will then be returned.
//
// Example:
//
// module.exports = utils.index(__dirname, __filename);
index: function (dirname, indexpath) {
var extensions = {js: 1},
modules = {};
indexpath = indexpath || path.join(dirname, 'index.js');
// Load all exported objects into a single module.
fs.readdirSync(dirname).forEach(function (filename) {
var fullpath = path.join(dirname, filename),
parts = filename.split('.'),
module, name;
if (fullpath !== indexpath && extensions[parts.pop()] && '.' !== filename[0]) {
name = parts.join('.');
module = require(fullpath);
// Grab the function name or module name if possible.
if (typeof module.name === 'string') {
name = module.name;
}
modules[name] = module;
}
});
return modules;
},
// Takes an object and a series of method names. Each method will be
// re-assigned to the object with the object bound as the methods scope. This
// is generally useful for defining callback methods.
//
// An array of method names can also be passed.
//
// Example:
//
// object = {onData: function () {}, onEnd: function () {});
// utils.bindAll(object, 'onData', 'onEnd');
//
// // Same as doing.
// object.onData = object.onData.bind(object);
// object.onEnd = object.onEnd.bind(object);
bindAll: function (object /* args */) {
var methods = [].slice.call(arguments, 1);
if (arguments.length === 2 && Array.isArray(arguments[1])) {
methods = arguments[1];
}
methods.forEach(function (method) {
if (typeof object[method] === 'function') {
object[method] = object[method].bind(object);
}
});
return object;
},
// Extends the first argument with the properties of the subsequent
// arguments.
extend: extend,
// Simplifies the process of extending existing objects.
inherit: inherit,
extract: function (obj /* keys */) {
var keys = [].slice.call(arguments, 1),
collected = {};
keys.forEach(function (key) {
if (obj[key]) {
collected[key] = obj[key];
}
});
return collected;
},
isAjax: function (req) {
return (req.get('X-Requested-With') || '').toLowerCase() === 'xmlhttprequest';
},
shortcode: function () {
var vowels = 'aeiou',
consonants = 'bcdfghjklmnpqrstvwxyz',
word = '', length = 6, index = 0, set;
for (; index < length; index += 1) {
set = (index % 2 === 0) ? vowels : consonants;
word += set[Math.floor(Math.random() * set.length)];
}
return word;
},
// Returns a gravatar url for the email address provided. An optional size
// parameter can be provided to specify the size of the avatar to generate.
gravatar: function (email, size) {
email = (email || '').trim().toLowerCase();
var hash = crypto.createHash('md5').update(email).digest('hex');
return 'http://www.gravatar.com/avatar/' + hash + '?s=' + (size || 26);
},
re: {
flatten: /\s+/g,
body: /<body.*?>\s*(.*)/m,
meta: /<meta name="description" content="(.*?)"/,
tags: /<[^>]+>/g,
title: /<title>(.*)<\/title>/
},
titleForBin: function (bin) {
var html = (bin.html || '').replace(this.re.flatten, ' '), // flatten
javascript = (bin.javascript || '').replace(this.re.flatten, ' ').trim(),
css = (bin.css || '').replace(this.re.flatten, ' ').trim(),
matches = (html.match(this.re.meta) || []);
// try to return the meta[description] first
if (matches.length === 2 && matches[1].trim()) {
return matches[1];
}
// then some of the body content with tags stripped
matches = (html.match(this.re.body) || ['', ''])[1].replace(this.re.tags, '').trim();
if (matches) {
return matches;
}
// No title return JavaScript.
if (javascript) {
return javascript;
}
if (css) {
return css;
}
matches = (html.match(this.re.title) || []);
if (matches.length === 2 && matches[1].trim()) {
return matches[1];
}
return html.replace(this.re.tags, '').trim();
},
since: function (date) {
var diff = (+new Date() - date) / 1000,
message = 'a long time',
timespan;
if (diff < 60) {
timespan = Math.floor(diff);
message = '{timespan} second';
}
else if (diff < 3600) {
timespan = Math.floor(diff / 60);
message = 'about {timespan} minute';
}
else if (diff < 86400) {
timespan = Math.floor(diff / 3600);
message = 'around {timespan} hour';
}
else if (diff < 604800) {
timespan = Math.floor(diff / 86400);
message = 'about {timespan} day';
}
else if (diff < 2419200) {
timespan = Math.ceil(diff / 604800);
message = 'nearly {timespan} week';
}
else if (diff < 29030400) {
timespan = Math.floor(diff / 2419200);
message = 'about {timespan} month';
}
if (timespan !== 1 && timespan) {
message += 's';
}
return message.replace('{timespan}', timespan) + ' ago';
},
queryStringForBin: function (bin, defaults) {
var params = [];
['html', 'javascript', 'css'].forEach(function (key) {
if (bin[key] !== defaults[key] && bin[key].trim()) {
params.push(key);
}
});
params.push('live');
return params.join(',');
}
};