-
Notifications
You must be signed in to change notification settings - Fork 20
/
urban.js
129 lines (111 loc) · 2.82 KB
/
urban.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
var https = require('https'),
util = require('util'),
EventEmitter = require('events').EventEmitter,
randomWord;
function urban() {
var word = new Dictionary(null); // instantiate first
return word.constructor.apply(word, arguments); // then re-instantiate it with the arguments of `urban`
}
function random() {
randomWord = true;
return urban();
}
urban.version = "0.3.4";
function Dictionary(words) {
if (randomWord === true && !arguments.length){
this.getData();
randomWord = false;
return this;
}
if (!arguments.length) {
throw new Error('missing argument(s)');
}
else if (arguments.length > 1) {
for (var i = 1; i < arguments.length; i++) words += ' ' +arguments[i];
}
else {
if (Array.isArray(words)) {
words = words.join(' ');
}
}
this._ended = false;
this.json = '';
if (words !== null) {
this.words = encodeURI(words.trim());
this.getData();
}
else {
return false;
}
return this;
}
util.inherits(Dictionary, EventEmitter);
Dictionary.fn = Dictionary.prototype;
Dictionary.fn._noop = function() {};
Dictionary.fn._end = function(fn) {
if (this._ended) {
fn(this.json);
}
else {
this.once('end', function(json) {
fn(json);
});
}
return this;
};
Dictionary.fn.getData = function(page) {
this.page = page || 1;
var self = this;
var _path = (!randomWord) ? '/v0/define?page=' + self.page + '&term=' + self.words : '/v0/random';
if (this._req && this._ended === 0) this._req.abort(); // abort if there's already one request happening
this._ended = false;
this._req = https.get({
host: 'api.urbandictionary.com',
path: _path
}, function(res) {
self._res = res; // reference
self.json = '';
res.on('data', function(data) {
if (self._ended !== 0) self._ended = 0; // in progress
self.json += data;
}).on('end', function() {
self.json = JSON.parse(self.json.trim());
self.emit('end', self.json);
self._ended = true;
});
});
return this;
};
Dictionary.fn.images = function(fn) {
return this._end(function(json) {
if (fn) fn(json.images);
});
};
Dictionary.fn.total = function(fn) {
return this._end(function(json) {
if (fn) fn(json.total);
});
};
Dictionary.fn.pages = function(fn) {
return this._end(function(json) {
if (fn) fn(json.pages);
});
};
Dictionary.fn.more = Dictionary.fn.next = function() {
return this.getData(++this.page);
};
Dictionary.fn.prev = function() {
return this.getData(this.page < 2 ? 1 : --this.page);
};
Dictionary.fn.res = Dictionary.fn.results = function(fn) {
return this._end(function(json) {
if (fn) fn(json.list);
});
};
Dictionary.fn.first = function(fn) {
return this._end(function(json) {
if (fn && json.list) fn(json.list[0]);
});
};
module.exports = urban;
module.exports.random = random;