-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
401 lines (347 loc) · 8.86 KB
/
index.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
"use strict";
var utils = require("../lib/utils");
/**
* Position index
*
* @typedef {[ number, number ]} Position
* @package
*/
/**
* Cow values to interpolate
*
* @typedef {Object} CowTemplateArgs
* @property {ReadonlyArray<Position>} pos Position indexes
* @property {string} str String to interpolate
* @package
*/
/**
* Cow action
*
* @typedef {"o" | "\\"} CowAction
*/
/**
* Cow name
*
* @typedef {Object} CowName
* @property {string} name Cow name
* @package
*/
/**
* Cow without name
*
* @typedef {Object} CowBase
* @property {string} [defEyes] Default eyes
* @property {string} [defTongue] Default tongue
* @property {ReadonlyArray<string>} template Cow template
* @property {ReadonlyArray<Position>} [actionPos] Action position indexes
* @property {ReadonlyArray<Position>} [eyesPos] Eyes position indexes
* @property {ReadonlyArray<Position>} [tonguePos] Tongue position indexes
*/
/**
* Cow
*
* @typedef {CowBase & CowName} Cow
*/
/**
* Cows list
*
* The default cow is in the first position.
*
* @type {ReadonlyArray<Readonly<Cow>>}
* @constant
* @package
*/
var corral = [
require("./default.cow"),
require("./apt.cow"),
require("./beavis.zen.cow"),
require("./blowfish.cow"),
require("./bong.cow"),
require("./bud-frogs.cow"),
require("./bunny.cow"),
require("./calvin.cow"),
require("./cheese.cow"),
require("./cock.cow"),
require("./cower.cow"),
require("./daemon.cow"),
require("./dragon-and-cow.cow"),
require("./dragon.cow"),
require("./duck.cow"),
require("./elephant-in-snake.cow"),
require("./elephant.cow"),
require("./eyes.cow"),
require("./flaming-sheep.cow"),
require("./fox.cow"),
require("./ghostbusters.cow"),
require("./gnu.cow"),
require("./head-in.cow"),
require("./hellokitty.cow"),
require("./kangaroo.cow"),
require("./kiss.cow"),
require("./kitty.cow"),
require("./koala.cow"),
require("./kosh.cow"),
require("./luke-koala.cow"),
require("./mech-and-cow.cow"),
require("./meow.cow"),
require("./milk.cow"),
require("./moofasa.cow"),
require("./moose.cow"),
require("./mutilated.cow"),
require("./pony-smaller.cow"),
require("./pony.cow"),
require("./ren.cow"),
require("./satanic.cow"),
require("./sheep.cow"),
require("./skeleton.cow"),
require("./small.cow"),
require("./snowman.cow"),
require("./sodomized.cow"),
require("./stegosaurus.cow"),
require("./stimpy.cow"),
require("./supermilker.cow"),
require("./surgery.cow"),
require("./suse.cow"),
require("./telebears.cow"),
require("./three-eyes.cow"),
require("./turkey.cow"),
require("./turtle.cow"),
require("./tux.cow"),
require("./udder.cow"),
require("./unipony-smaller.cow"),
require("./unipony.cow"),
require("./vader-koala.cow"),
require("./vader.cow"),
require("./www.cow")
];
/**
* Custom cows list
*
* @type {Readonly<Cow>[]}
* @constant
* @package
*/
var customCorral = [];
/**
* Truncate string to the given length
*
* @param {string | undefined} str String to truncate
* @param {number} len Maximum length
* @returns {string} Truncated string
* @package
*/
function truncate(str, len) {
return typeof str === "string" ? str.slice(0, len) : "";
}
/**
* Force cow value to the given lenght at least
*
* @param {string | undefined} value Cow value
* @param {string | undefined} empty Default value for empty value
* @param {string | undefined} undef Default value for undefined value
* @param {number} len Maximum length
* @returns {string} Fixed value
* @package
*/
function fix(value, empty, undef, len) {
if (typeof value !== "string") {
return truncate(undef, len);
}
if (value.length === 0) {
return truncate(empty, len);
}
return truncate(value, len);
}
/**
* Check if the given position array is valid
*
* @param {ReadonlyArray<Position> | undefined} arr Position array
* @returns Whether the position array is valid
* @package
*/
function validatePositionArray(arr) {
// Undefined arrays are allowed
if (arr === undefined) {
return true;
}
// Not arrays are not allowed
if (!Array.isArray(arr)) {
return false;
}
// Validate format of every position
return arr.every(function(pos) {
return Array.isArray(pos) && pos.length === 2 && typeof pos[0] === "number" && typeof pos[1] === "number";
});
}
/**
* Create a full deep copy of the given cow
*
* @param {Cow} cow Cow to copy
* @returns {Cow} A copy of the given cow
* @package
*/
function copyCow(cow) {
/**
* Copy the given position indexes
*
* @param {Readonly<Position>} pos Position indexes
* @returns {Position} A copy of the position indexes
*/
var copier = function(pos) {
return [ pos[0], pos[1] ];
};
return {
name: cow.name,
defEyes: cow.defEyes,
defTongue: cow.defTongue,
template: cow.template.slice(),
actionPos: cow.actionPos ? cow.actionPos.map(copier) : undefined,
eyesPos: cow.eyesPos ? cow.eyesPos.map(copier) : undefined,
tonguePos: cow.tonguePos ? cow.tonguePos.map(copier) : undefined,
};
}
/**
* Validate the given custom cow
*
* @param {Cow & CowBase} cow Custom cow to validata
* @param {boolean} [name=false] Validate name
* @returns {boolean} Whether the custom cow is valid
*/
function validateCow(cow, name) {
var valid = true;
valid = valid && typeof cow === "object" && cow !== null && !Array.isArray(cow);
valid = valid && Array.isArray(cow.template);
valid = valid && cow.template.every(function(line) { return typeof line === "string"; });
valid = valid && (cow.defEyes === undefined || typeof cow.defEyes === "string");
valid = valid && (cow.defTongue === undefined || typeof cow.defTongue === "string");
valid = valid && validatePositionArray(cow.actionPos);
valid = valid && validatePositionArray(cow.eyesPos);
valid = valid && validatePositionArray(cow.tonguePos);
if (name) {
valid = valid && typeof cow.name === "string" && cow.name.length > 0;
}
return valid;
}
/**
* Find a cow in the corral by name
*
* @param {string} name Cow name
* @returns {Cow} Matching cow
*/
function getCow(name) {
/** @type {Cow | undefined} */
var cow;
// Find cow
if (typeof name === "string") {
cow = utils.find(corral.concat(customCorral), function(cow) {
return cow.name === name;
});
}
// Get default cow if is not found
if (cow === undefined) {
cow = corral[0];
}
// Return a copy of the cow
return copyCow(cow);
}
/**
* Add a new cow to the custom corral
*
* @param {Cow} cow New cow to add
* @returns {boolean} Whether the cow could be added
*/
function addCow(cow) {
if (!validateCow(cow, true)) {
return false;
}
// Check if the cow already exists
if (getCow(cow.name).name === cow.name) {
return false;
}
// Add cow and sort corral
customCorral.push(cow);
customCorral.sort(function(a, b) {
return a.name.localeCompare(b.name);
});
return true;
}
/**
* Remove a cow from the custom corral
*
* @param {string} name Cow name
* @returns {Cow | undefined} Removed cow
*/
function removeCow(name) {
// Return undefined for not valid string
if (typeof name !== "string") {
return undefined;
}
// Get cow index
var ind = customCorral.findIndex(function(cow) {
return cow.name === name;
});
// Remove cow from corral and return it
if (ind !== -1) {
return customCorral.splice(ind, 1)[0];
}
// Not found cow
return undefined;
}
/**
* Cow renderer function
*
* @param {CowBase} cow Cow to render
* @param {CowAction} [action] Action
* @param {string} [eyes] Eyes
* @param {string} [tongue] Tongue
* @returns {string} Rendered cow
*/
function renderCow(cow, action, eyes, tongue) {
// Copy template
var lines = cow.template.slice();
// Get values to interpolate
/** @type {Readonly<CowTemplateArgs>[]} */
var values = [];
var act = -1;
if (cow.tonguePos) {
values.push({ pos: cow.tonguePos, str: fix(tongue, cow.defTongue, " ", 2) });
}
if (cow.eyesPos) {
values.push({ pos: cow.eyesPos, str: fix(eyes, cow.defEyes, "oo", 2) });
}
if (cow.actionPos) {
values.push({ pos: cow.actionPos, str: fix(action, undefined, undefined, 1) });
act = values.length - 1;
}
// Interpolate values
values.forEach(function(val, i) {
// Position indexes fixer and face position index flag
var fix = 0;
var f = i !== act;
val.pos.forEach(function(pos, j) {
var char = val.str[j] || (f && j === 1 ? "" : val.str.slice(-1));
var pos0 = pos[0];
var pos1 = pos[1] - fix;
var line = lines[pos0];
lines[pos0] = line.slice(0, pos1) + char + line.slice(pos1 + 1);
if (char.length === 0) {
++fix;
}
});
});
return lines.join("\n");
}
/**
* Cows collection
*
* @module cowsayjs/cows
*/
module.exports = {
corral: corral.map(copyCow),
customCorral: customCorral,
validateCow: validateCow,
getCow: getCow,
addCow: addCow,
removeCow: removeCow,
renderCow: renderCow
};