-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrafity.common.js
executable file
·89 lines (76 loc) · 2.35 KB
/
crafity.common.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
/*jslint bitwise: true, unparam: true, maxerr: 50, white: true */
/*globals window*/
/*!
* crafity.common
* Copyright(c) 2011 Crafity
* Copyright(c) 2011 Bart Riemens
* Copyright(c) 2011 Galina Slavova
* MIT Licensed
*/
(function (crafity, $, window) {
"use strict";
if (!crafity.Exception) {
throw new Error("Missing dependency 'crafity.Exception'");
}
var Exception = crafity.Exception
, common = (crafity.common = {});
/**
* Validate arguments
* @param name The name of the argument
* @param value The value of the argument
* @param type The expected type (or types)
* @param required Value can not be null or undefined
*/
common.arg = common.args = function (name, value, type, required) {
if (!name) { throw new Exception("No options are passed to validateArguments"); }
var options = [];
if (name && typeof name === 'string' && value) {
options.push({
name: name,
value: value,
type: type,
required: required
});
} else if (name instanceof Array) {
options = name;
} else {
options = Array.apply(null, arguments);
}
options.forEach(function (option) {
var isEmpty = option.value === undefined
|| option.value === null;
if (option.required && isEmpty) {
throw new Exception("Argument '" + option.name + "' is required but was '" +
(option.value === null ? "null" : "undefined") + "'");
}
if (option.type && !isEmpty && typeof option.value === 'string'
&& option.type !== String) {
throw new Exception("Argument '" + option.name + "' must be of type '" +
option.type + "' but was '" + typeof option.value + "'");
}
if (option.type && !isEmpty && typeof option.value !== 'string'
&& !(option.value instanceof option.type)) {
throw new Exception("Argument '" + option.name + "' must be of type '" +
option.type + "' but was '" + typeof option.value + "'");
}
});
};
crafity.catchError = function (callback) {
return function (err) {
try {
if (err && err.stack && err.message) { throw err; }
if (callback) {
return callback.apply(this, arguments);
}
} catch (err) {
window.console.error(err.stack || err.message || err.toString());
return false;
}
};
};
crafity.ready = function (onready) {
$(function () {
onready.apply(this, arguments);
});
};
}(window.crafity = window.crafity || {}, window.jQuery, window));