Repository navigation
Expand file tree
/
Copy pathblah-code.js
More file actions
239 lines (213 loc) · 4.36 KB
/
Copy pathblah-code.js
File metadata and controls
239 lines (213 loc) · 4.36 KB
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
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], function () {
return (root.blahCode = factory());
});
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals
root.blahCode = factory();
}
})(this, function () {
// UMD Definition above, do not remove this line
// To get to know more about the Universal Module Definition
// visit: https://github.com/umdjs/umd
'use strict';
/**
* Chars which are encoded. Others will be simply ignored ᕕ( ᐛ )ᕗ
* @type {Array}
* @private
*/
var chars = [
' ',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'ä',
'ü',
'ö',
'!',
'?',
'.',
'-',
'@',
'ë',
'ï',
'é',
'à',
'ç'
];
/**
* Repeats strings.
*
* @param {String} string The string to repeat
* @param {Number} count how many times to repeat
* @return {String} Repeated Strings in one string
* @private
*/
var repeat = function (string, count) {
var result = '';
if (count < 1) {
return '';
}
while (count > 1) {
if (count & 1) {
result += string;
}
count >>= 1;
string += string;
}
return result + string;
};
/**
* Encodes normal text to blah code.
*
* @param {String} text Normal text.
* @return {String} Blah code
* @public
*/
var encode = function (text) {
var blah = [];
text.toLowerCase().split('').forEach(function (element) {
var position = chars.indexOf(element);
if (position !== -1) {
blah.push(repeat('blah ', position + 1).trim());
}
});
return blah.join(', ');
};
/**
* Decodes blah code to normal text.
*
* @param {String} code Blah code.
* @return {String} Normal text
* @public
*/
var decode = function (code) {
var text = [];
code.split(',').forEach(function (element) {
text.push(chars[element.trim().split(' ').length - 1]);
});
return text.join('');
};
/**
*creates Ooks out of a numerical value
*
* @param {Number} The value to ookify
* @return {String} an Ook-representation of the value
* @private
*/
var ookify = function (value) {
var ooks = '';
var leftValue = Math.floor(value / 3);
if (leftValue > 0) {
ooks = ookify(leftValue) + ' ';
}
var rightValue = value % 3;
if (rightValue === 0) {
ooks += 'Ook.';
} else if (rightValue === 1) {
ooks += 'Ook?';
} else if (rightValue === 2) {
ooks += 'Ook!';
} else {
ooks += rightValue + ' said Monkey!';
}
return ooks;
};
/**
*creates a numerical value out of a set of ooks
*
* @param {String} an Ook-representation of the value
* @return {Number} the numerical value
* @private
*/
var unookify = function (charTribbles) {
var idx = 0;
var tribblePattern = /Ook(.)/gi;
var match = null;
while ((match = tribblePattern.exec(charTribbles)) !== null) {
idx *= 3;
if (match[1] === '.') {
idx += 0;
} else if (match[1] === '?') {
idx += 1;
} else if (match[1] === '!') {
idx += 2;
} else {
console.log('Got Invalid value "' + match[0] + '"');
}
}
return idx;
};
/**
* Encodes human readable text to ape readable code.
* Be aware of the monkeys!
*
* @param {String} text Normal text.
* @return {String} ape code
* @public
*/
var encodeApe = function (text) {
var blah = [];
text.toLowerCase().split('').forEach(function (element) {
var position = chars.indexOf(element);
if (position !== -1) {
blah.push(ookify(position).trim());
}
});
return blah.join(', ');
};
/**
* Decodes ape readable code to human readable text.
*
* @param {String} code ape code.
* @return {String} Human text
* @public
*/
var decodeApe = function (code) {
var charPattern = /(\s*Ook[!?.]\s*)+,?/gi;
var result = '';
var characters = null;
while ((characters = charPattern.exec(code)) !== null) {
var idx = unookify(characters[0]);
result += chars[idx];
}
return result;
};
return {
encode: encode,
encodeApe: encodeApe,
decode: decode,
decodeApe: decodeApe
};
});