]>
git.r.bdr.sh - rbdr/generador-de-insultos/blob - lib/InsultGenerator.js
5c40651a7493921e5dd5c118102d39b8d8ae4903
4 var fs
= require('fs');
6 var Promise
= require('bluebird');
9 var Conjugator
= require('./Conjugator');
11 var InsultGenerator
= Class({}, "InsultGenerator")({
19 _verbRe: /^[0-9]+\-v
\s
+?lemma
\s
+?([^ ]+)/,
20 _nounRe: /^[0-9]+\-n
\s
+?lemma
\s
+?([^ ]+)/,
23 init : function init(config
) {
24 config
= config
|| {};
26 Object
.keys(config
).forEach(function (property
) {
27 this[property
] = config
[property
];
30 this._conjugator
= new Conjugator();
33 generate : function generate(config
) {
37 return this._getVerb();
38 }.bind(this)).then(function (verb
) {
39 return this._conjugateVerb(verb
);
40 }.bind(this)).then(function (conjugatedVerb
) {
41 selectedVerb
= conjugatedVerb
;
42 return this._getNoun();
43 }.bind(this)).then(function (noun
) {
44 return this._generateInsult(selectedVerb
, noun
);
48 _load : function _load() {
49 return this._loadFile()
51 return this._loadVerbs();
52 }.bind(this)).then(function () {
53 return this._loadNouns();
57 _loadFile : function _loadFile() {
58 return new Promise(function (resolve
, reject
) {
63 fs
.readFile(this.file
, {encoding: 'utf8'}, function (err
, contents
) {
68 this._contents
= contents
;
75 _loadVerbs : function _loadVerbs() {
76 return new Promise(function (resolve
, reject
) {
82 this._contents
.split('\n').forEach(function (line
) {
84 matches
= line
.match(this._verbRe
);
87 if (this.verbs
.indexOf(matches
[1]) === -1) {
88 this.verbs
.push(matches
[1])
97 _loadNouns : function _loadNouns() {
98 return new Promise(function (resolve
, reject
) {
104 this._contents
.split('\n').forEach(function (line
) {
106 matches
= line
.match(this._nounRe
);
109 if (this.nouns
.indexOf(matches
[1]) === -1) {
110 this.nouns
.push(matches
[1])
119 _getVerb : function _getVerb() {
121 index
= Math
.floor(Math
.random()*this.verbs
.length
);
122 return Promise
.resolve(this.verbs
[index
]);
125 _conjugateVerb : function _conjugateVerb(verb
) {
126 return new Promise(function (resolve
, reject
) {
131 var attemptConjugation
= function attemptConjugation(verb
, tries
) {
132 if (tries
> this._maxTries
) {
133 return reject(new Error("Couldn't find a proper verb"));
136 this._conjugator
.conjugate(verb
)
137 .then(function (conjugatedVerb
) {
138 resolve(conjugatedVerb
);
140 .catch(function (err
) {
141 this._getVerb().then(function (verb
) {
142 attemptConjugation
.bind(this)(verb
, tries
+ 1);
147 setTimeout(attemptConjugation
.bind(this, verb
, 0), 0);
151 _getNoun : function _getNoun() {
153 index
= Math
.floor(Math
.random()*this.nouns
.length
);
154 return Promise
.resolve(this.nouns
[index
].toLowerCase());
157 _generateInsult : function _generateInsult(verb
, noun
) {
158 return Promise
.resolve(verb
+ this._pluralize(noun
));
161 // Super dumb pluralizer
162 _pluralize : function _pluralize(noun
) {
165 lastLetter
= noun
[noun
.length
- 1];
167 if (lastLetter
=== "s") {
171 if (["a","e","i","o","u"].indexOf(lastLetter
) >= 0) {
180 module
.exports
= InsultGenerator
;