-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhelpers.js
107 lines (96 loc) · 2.84 KB
/
helpers.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
/* Show the currently selected language and hide all of the other
* languages.
*/
export function selectLanguage (language) {
$('[lang]').each(function () {
if ($(this).attr('lang') === language) {
$(this).show();
} else {
$(this).hide();
}
});
}
/* Due to how scoping works in JavaScript modules, the
* `selectLanguage()` function is only declared in the module but not
* the global scope. The following will declare this function also in
* the `window` (the HTML document) scope so we can call it from the
* HTML document.
*/
window.selectLanguage = selectLanguage;
function processForm () { // eslint-disable-line no-unused-vars
// Save some values from the form.
var formMessage = $('#inputtext')[0].value;
var formKey = $('#key')[0].value;
// Check if formKey is actually an integer.
if (!isStringInteger(formKey)) {
alert('The secret key must be an integer. You entered "' +
formKey + '".');
} else {
// Process the form.
alert('processing form (' + formMessage + ')');
alert('result: ' + shiftText(formMessage, Number(formKey)));
}
}
window.processForm = processForm;
/* Check if the string "test" is actually an integer. isNan is false
* if "test" is empty, so check if "test" is empty too.
*/
export function isStringInteger (test) {
if (!(isNaN(test) || test === '')) {
if (Math.round(Number(test)) === Number(test)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/* Takes a text and a shift as arguments and returns the shifted
* text.
*/
export function shiftText (text, shift) {
var code = '';
for (var i in text) {
code += shiftLetter(text[i], shift);
}
return code;
}
/* Takes a letter and a shift as arguments and returns the shifted
* letter.
*/
export function shiftLetter (letter, shift) {
// First change the letter into a number.
var code = letter.charCodeAt(0);
var Acode = 'A'.charCodeAt(0);
var Zcode = 'Z'.charCodeAt(0);
var acode = 'a'.charCodeAt(0);
var zcode = 'z'.charCodeAt(0);
// Change the shift into a positive number between 0 and 25.
shift = shift % 26;
if (shift < 0) {
shift += 26;
}
// Only shift if the letter really is a letter.
if (code >= Acode && code <= Zcode) {
// The letter is a capital letter. Shift it.
code += shift;
// If code is too big, wrap it so it isn't.
code = Acode + (code - Acode) % 26;
} else if (code >= acode && code <= zcode) {
// The letter is a lowercase letter. Shift it and wrap it.
code += shift;
code = acode + (code - acode) % 26;
}
return String.fromCharCode(code);
}
/* Initialize language setting on page load
*
* The `$(...` is a jQuery shorthand to do something once the page is
* fully loaded and ready.
*
* https://learn.jquery.com/using-jquery-core/document-ready/
*/
$(function () {
selectLanguage('en');
});