-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomName.js
98 lines (88 loc) · 2.29 KB
/
randomName.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
//
// randomName.js
// - Generate a random name that sounds halfway ok for the specified language.
//
// John D. Allen
// July 2015
//
//----------------------------------------------------------------------------------
// Version:
// 0.3 -- Modifed to support an REST API to get name.
// 0.2 -- Modifed for better name generation
// 0.1 -- Initial code.
//
//----------------------------------------------------------------------------------
var express = require('express');
var app = express();
var constants = ["b","c","c","d","f","g","h","j","k","k","l","m","n","p","q","r","s","s","t","t","v","w","x","y","z"];
var vowels = ["a","i","e","o","u"];
var endings = ["ith", "ton", "on", "fled", "man", "ar","ge","dile", "in", "ax", "ing", "port"];
var patterns = [
"CVCE",
"CVCVCE",
"CVCVE",
"CVCVCVCE"
];
//-------------------------------------------------
// Function: rand()
//-------------------------------------------------
function rand(n) {
return Math.floor(Math.random()*n);
}
var conlen = constants.length;
var volen = vowels.length;
var endlen = endings.length;
var patlen = patterns.length;
//-------------------------------------------------
// Export: name()
//-------------------------------------------------
function nameEN() {
var out = "";
// pick a pattern
var pat = patterns[rand(patlen)];
// process pattern
for (var i = 0; i < pat.length; i++) {
switch(pat[i]) {
case "C":
out += constants[rand(conlen)];
break;
case "V":
out += vowels[rand(volen)];
break;
case "E":
out += endings[rand(endlen)];
break;
}
if (i === pat.length - 1) {
return out;
}
}
};
// //-----------[ Test Code ]-----------------
// function Main() {
// console.log("12345678901234>");
// for (var i = 1; i < 30; i++) {
// exports.name(function(err, data) {
// console.log(data);
// });
// }
// }
// if (require.main === module) {
// Main();
// }
app.get("/name", (req, res, next) => {
var lang = req.query.lang;
if (lang == undefined) { lang = "EN"; }
switch(lang) {
case "EN":
var out = '{"name": "' + nameEN() + '"}';
break;
default:
var out = '{"err": "Unsupported Language"}'
}
res.setHeader('Content-Type', 'application/json')
res.send(out);
});
app.listen(80, () => {
//console.log("Server running on port 80")
});