mirror of
https://github.com/jsbin/jsbin.git
synced 2026-01-18 15:18:04 +00:00
50 lines
1.2 KiB
JavaScript
50 lines
1.2 KiB
JavaScript
module.exports = {
|
|
extract: function (obj /* keys */) {
|
|
var keys = [].slice.call(arguments, 1),
|
|
collected = {};
|
|
|
|
keys.forEach(function (key) {
|
|
if (obj[key]) {
|
|
collected[key] = obj[key];
|
|
}
|
|
});
|
|
|
|
return collected;
|
|
},
|
|
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;
|
|
},
|
|
titleForBin: function (bin) {
|
|
// Try and get title from HTML first, unlike the PHP version we're
|
|
// returning the HTML title first as that seems to make most sense.
|
|
// TODO: Use HTML parser or JSDOM
|
|
var html = bin.html || '',
|
|
javascript = (bin.javascript || '').trim(),
|
|
matches = html.match(/<title>([^>]*)<\/title>/);
|
|
|
|
if (matches) {
|
|
return matches[1];
|
|
}
|
|
|
|
// No title return JavaScript.
|
|
if (javascript) {
|
|
return javascript.replace(/\s+/g, ' ');
|
|
}
|
|
|
|
matches = html.match(/<body.*>([^\s]*)/);
|
|
if (matches) {
|
|
return matches[1];
|
|
}
|
|
return '';
|
|
}
|
|
};
|