/
arc_core_graph_util.js
78 lines (71 loc) · 2.46 KB
/
arc_core_graph_util.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
/*
Copyright (C) 2014-2016 Christopher D. Russell
This library is published under the MIT License and is part of the
Encapsule Project System in Cloud (SiC) open service architecture.
Please follow https://twitter.com/Encapsule for news and updates
about jsgraph and other time saving libraries that do amazing things
with in-memory data on Node.js and HTML.
*/
var JSType = function(reference_) {
return Object.prototype.toString.call(reference_);
};
/*
request = {
ref: reference
types: string or array of strings (e.g. '[object Object]')
}
response = {
error: null (success) or string (error)
result: null (failure) or JSType of request.ref (success)
}
*/
var JSTypeInTypeSet = function(request_) {
var response = { error: null, result: null };
var errors = [];
var allowedTypeSet = {};
var jstype = JSType(request_.types);
switch (jstype) {
case '[object String]':
allowedTypeSet = [ request_.types ];
break;
case '[object Array]':
allowedTypeSet = request_.types;
break;
default:
errors.unshift("Invalid request.types value type '" + jstype + "'. Expected either '[object String]' or '[object Array]'.");
break;
}
jstype = JSType(request_.description);
if (jstype !== '[object String]') {
errors.unshift("Invalid request.description value type '" + jstype + "'. Expected '[object String]'.");
}
if (!errors.length) {
jsType = JSType(request_.ref);
response.result = (allowedTypeSet.indexOf(jsType) !== -1) && jsType || null;
}
if (!response.result) {
response.guidance = request_.description + " value type '" + jstype + "' is invalid. Expected one of [" + allowedTypeSet.join(',') + "].";
}
if (errors.length) {
errors.unshift("JSTypeInTypeSet failed:");
response.error = errors.join(' ');
}
return response;
};
var setPropertyValueIfUndefined = function(reference_, propertyName_, valueOrFunction_) {
var type = JSType(reference_[propertyName_]);
if (type === '[object Undefined]') {
type = JSType(valueOrFunction_);
if (type !== '[object Function]') {
reference_[propertyName_] = valueOrFunction_;
} else {
reference_[propertyName_] = valueOrFunction_();
}
return true;
}
return false;
};
module.exports = {
JSType: JSType,
setPropertyValueIfUndefined: setPropertyValueIfUndefined
};