forked from chuanxshi/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.html
116 lines (95 loc) · 2.51 KB
/
strategy.html
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
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Strategy
Description: allows one of a family of algorithms to be selected on-the-fly at runtime
*/
var validator = {
// all available checks
types:{},
// error messages in the current
// validation session
messages:[],
// current validation config
// name: validation type
config:{},
// the interface method
// 'data' is key => value pairs
validate:function (data) {
var i, msg, type, checker, result_ok;
// reset all messages
this.messages = [];
for (i in data) {
if (data.hasOwnProperty(i)) {
type = this.config[i];
checker = this.types[type];
if (!type) {
continue; // no need to validate
}
if (!checker) { // uh-oh
throw {
name:"ValidationError",
message:"No handler to validate type " + type
};
}
result_ok = checker.validate(data[i]);
if (!result_ok) {
msg = "Invalid value for *" + i + "*, " + checker.instructions;
this.messages.push(msg);
}
}
}
return this.hasErrors();
},
// helper
hasErrors:function () {
return this.messages.length !== 0;
}
};
// checks for non-empty values
validator.types.isNonEmpty = {
validate:function (value) {
return value !== "";
},
instructions:"the value cannot be empty"
};
// checks if a value is a number
validator.types.isNumber = {
validate:function (value) {
return !isNaN(value);
},
instructions:"the value can only be a valid number, e.g. 1, 3.14 or 2010"
};
// checks if the value contains only letters and numbers
validator.types.isAlphaNum = {
validate:function (value) {
return !/[^a-z0-9]/i.test(value);
},
instructions:"the value can only contain characters and numbers, no special symbols"
};
var data = {
first_name:"Super",
last_name:"Man",
age:"unknown",
username:"o_O"
};
validator.config = {
first_name:'isNonEmpty',
age:'isNumber',
username:'isAlphaNum'
};
validator.validate(data);
if (validator.hasErrors()) {
console.log(validator.messages.join("\n"));
}
// reference
// http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#strategypatternjquery
// http://shop.oreilly.com/product/9780596806767.do?sortby=publicationDate
</script>
</body>
</html>