-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
129 lines (110 loc) · 3.34 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
/*
Daefen
------
A library for encoding/decoding large numbers into/from a pronounceable high-density string constructed from a base of 3456 syllables.
*/
// Creates the syllable array
// Alternatively you can simply load the syllable arrays from syllables.js
var syllables = [];
var consonants = "bcdfghjklmnprstvwz"; // consonants that are unambiguous and easy to pronounce
var vowels = "aeiouy"; // vowels
// Vowel + Consonant
for (a = 0; a < vowels.length; a++) {
for (b = 0; b < consonants.length; b++) {
syllables.push(vowels[a] + consonants[b]);
}
}
// Consonant + Vowel
for (b = 0; b < consonants.length; b++) {
for (a = 0; a < vowels.length; a++) {
syllables.push(consonants[b] + vowels[a]);
}
}
// Consonant + Vowel + Vowel
for (b = 0; b < consonants.length; b++) {
for (a = 0; a < vowels.length; a++) {
for (e = 0; e < vowels.length; e++) {
syllables.push(consonants[b] + vowels[a] + vowels[e]);
}
}
}
// Consonant + Vowel + Consonant
for (b = 0; b < consonants.length; b++) {
for (a = 0; a < vowels.length; a++) {
for (c = 0; c < consonants.length; c++) {
syllables.push(consonants[b] + vowels[a] + consonants[c]);
}
}
}
// Vowel + Consonant + Vowel
for (a = 0; a < vowels.length; a++) {
for (b = 0; b < consonants.length; b++) {
for (e = 0; e < vowels.length; e++) {
syllables.push(vowels[a] + consonants[b] + vowels[e]);
}
}
}
// Quick function that converts a big Number object into an array of numbers for any chosen base
function fromBase10(bigNum, base) {
var result = [];
if (bigNum == 0) return [0];
for (var i = bigNum; i > 0; i = Math.floor(i / base)) {
result.unshift(i % base);
}
return result;
}
// Important to check some spacing issues
function isConsonant(letter) {
return consonants.indexOf(letter) >= 0;
}
// Converts an integer (passed as a string to avoid scientific notation issues)
function toWords(number) {
let numberArray = fromBase10(number, syllables.length);
let result = "";
let lastWord = "";
let n = 0;
for (i = 0; i < numberArray.length; i++) {
n = numberArray[i] || 0;
lastWord = result.split(" ").slice(-1)[0];
if (
result == "" ||
lastWord.length == syllables[n].length ||
(lastWord.length < 5 &&
isConsonant(lastWord.slice(-1)) &&
isConsonant(syllables[n].slice(0, 1)))
) {
result += syllables[n];
} else {
result += " " + syllables[n];
}
}
return result.replace(/\b[a-z]/g, function(f) {
return f.toUpperCase();
});
}
// Converts a valid phrase back into a string
function fromWords(words) {
let wordArray = words
.toLowerCase()
.replace(/[bcdfghjklmnprstvwz][bcdfghjklmnprstvwz]/gi, function(r) {
let n = Math.floor(r.length / 2);
return r.substr(0, n) + " " + r.substr(n, n);
})
.replace(/[a-z]{6}|[a-z]{4}/gi, function(r) {
let n = Math.floor(r.length / 2);
return r.substr(0, n) + " " + r.substr(n, n);
})
.split(" ");
let result = 0;
for (i = 0; i < wordArray.length; i++) {
if (syllables.indexOf(wordArray[i]) < 0) return;
result =
result +
syllables.indexOf(wordArray[i]) *
Math.pow(syllables.length, wordArray.length - i - 1);
}
return result;
}
// Be careful with numbers much larger than this in javascript
var random = Math.floor(Math.random() * Math.pow(2, 55));
toWords(random);