-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreetr.js
156 lines (136 loc) · 3.5 KB
/
Greetr.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
(function (global, $) {
// 'new' an object
var Greetr = function (firstName, lastName, language) {
return new Greetr.init(firstName, lastName, language);
};
/**
* variables hidden within the scope of IIFE
*/
// supported Languages
const supportedLangs = ["en", "es"];
// informal greetings
const greetings = {
en: "Hello",
es: "Hola",
};
// formal greetings
const formGreetings = {
en: "Greetings",
es: "Saludos",
};
// logger messages
const logMessages = {
en: "Logged in",
es: "Inició sesión",
};
/**
* Greetr prototype methods
*/
Greetr.prototype = {
/**
* @returns string - full name
*/
fullName: function () {
return this.firstName + " " + this.lastName;
},
/**
* checks if language is supported in `supportedLangs` array
*/
validate: function () {
if (supportedLangs.indexOf(this.language) === -1) {
throw "Invalid Language!";
}
},
/**
*
* @returns string - joins greeting and first name
*/
greeting: function () {
return greetings[this.language] + " " + this.firstName + "!";
},
/**
*
* @returns string - Formal greeting with full name
*/
formalGreeting: function () {
return formGreetings[this.language] + ", " + this.fullName() + "!";
},
/**
*
* @param {Boolean} formal
* @returns greeting based on the boolean
*/
greet: function (formal) {
var msg;
if (formal) {
msg = this.formalGreeting();
} else {
msg = this.greeting();
}
if (console) {
console.log(msg);
}
return this;
},
log: function () {
if (global.console) {
global.console.log(logMessages[this.language] + ": " + this.fullName());
}
return this;
},
setLang: function (lang) {
this.language = lang;
this.validate();
return this;
},
/**
*
* @param {HTMLElementEventMap} domSelector
* @param {Boolean} formal
* @returns
*
* creates the greeting based on boolean and insert
* into the DOM selector
*/
generateHtmlGreeting: function (domSelector, formal) {
// throw an error if jQuery isn't present
if (!$) throw "jQuery not loaded!";
// throw an error if jQuery selector isn't present
if (!domSelector) throw "Missing jQuery Selector!";
/**
* create msg string based on formal boolean
* TODO: re-factor this to a private fn
*/
let msg;
if (formal) {
msg = this.formalGreeting();
} else {
msg = this.greeting();
}
// insert greeting into the dom selector
$(domSelector).html(msg);
return this;
},
};
/**
*
* @param {String} firstName
* @param {String} lastName
* @param {String} language
* creation of the actual object, allowing to 'new' an object
* without calling the 'new' keyword
*/
Greetr.init = function (firstName, lastName, language) {
var self = this;
self.firstName = firstName || "";
self.lastName = lastName || "";
self.language = language || "en";
// validate if the language passed in the arguments is supported
self.validate();
};
// trick borrowed from jQuery to avoid using the `new` keyword
Greetr.init.prototype = Greetr.prototype;
// attach Greetr to the global (window) & provide a shorthand
// `$G()` to ease our poor fingers
global.Greetr = global.G$ = Greetr;
})(window, jQuery);