-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreetr.js
100 lines (85 loc) · 2.13 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
// If the other library injected before this didn't put semicolon at the end
;(function (global, $) {
var Greetr = function (firstName, lastName, language) {
return new Greetr.init({ firstName, lastName, language })
};
var supportedLanguages = ['en', 'es'];
var greetings = {
en:'Hello',
es:'Hola'
};
var formalGreetings = {
en:'Greetings',
es:'Saludos'
};
var logMessages = {
en:'LoggedIn',
es:'iniciar sesión'
}
Greetr.prototype = {
fullName: function(){
return `${this.firstName} ${this.lastName}`;
},
validate: function(){
if(!supportedLanguages.includes(this.language)){
throw new Error('unsupported language');
}
},
greeting: function(){
return `${greetings[this.language]} ${this.firstName} !`;
},
formalGreetings: function(){
return `${formalGreetings[this.language]} ${this.fullName()}`;
},
greet: function(formal){
var msg;
if(formal){
msg = this.formalGreetings()
}else{
msg = this.greeting()
}
if(console){
console.log(msg);
}
return this;
},
log: function(){
if(console){
console.log(`${logMessages[this.language]}: ${this.fullName()}`);
}
return this;
},
setLanguage : function(newlanguage){
this.language = newlanguage;
this.validate();
return this
},
HTMLGreeting: function(selector, formal){
if(!$){
throw new Error("jQuery not found");
}
if(!selector){
throw new Error('misssing jQuery Selector ')
}
var msg;
if(formal){
msg = this.formalGreetings()
}else{
msg = this.greeting()
}
$(selector).html(msg);
return this;
}
};
Greetr.init = function ({ firstName = '', lastName = '', language = 'en' }) {
var self = this;
self.firstName = firstName;
self.lastName = lastName;
self.language = language;
this.validate();
return self
};
Greetr.init.prototype = Greetr.prototype;
global.Greetr = global.G$ = Greetr;
return Greetr;
})(window, jQuery);