forked from realm/github-gantt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.js
126 lines (112 loc) · 2.56 KB
/
utilities.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
/*
Utility Functions
Various functions to check the integrity of data.
*/
var isString = function(x) {
return x !== null && x !== undefined && x.constructor === String
}
var isNumber = function(x) {
return x !== null && x !== undefined && x.constructor === Number
}
var isBoolean = function(x) {
return x !== null && x !== undefined && x.constructor === Boolean
}
var isObject = function(x) {
return x !== null && x !== undefined && x.constructor === Object
}
var isArray = function(x) {
return x !== null && x !== undefined && x.constructor === Array
}
var isDate = function(d) {
if ( Object.prototype.toString.call(d) === "[object Date]" ) {
if ( isNaN( d.getTime() ) ) {
return false;
}
else {
return true;
}
}
else {
return false;
}
}
var isRealmObject = function(x) {
return x !== null && x !== undefined && x.constructor === Realm.Object
}
var isRealmList = function(x) {
return x !== null && x !== undefined && x.constructor === Realm.List
}
var sanitizeFloat = function(number) {
if (isNumber(number)) {
return number;
}
else if (isString(number)) {
let n = parseFloat(number);
if (isNaN(n)) {
return null;
}
else {
return n;
}
}
else {
return null;
}
}
var sanitizeInt = function(number) {
if (isNumber(number)) {
return number;
}
else if (isString(number)) {
return parseInt(number);
}
else {
return null;
}
}
var sanitizeString = function(string) {
if (isString(string)) {
return string;
}
else if (isNumber(string)) {
return string.toString();
}
else {
return null;
}
}
var sanitizeStringNonNull = function(string) {
if (isString(string)) {
return string;
}
else if (isNumber(string)) {
return string.toString();
}
else {
return "";
}
}
var sanitizeBool = function(bool) {
if (isBoolean(bool)) {
return bool;
}
else if (isNumber(bool)) {
return Boolean(bool);
}
else {
return null;
}
}
exports.isString = isString;
exports.isNumber = isNumber;
exports.isBoolean = isBoolean;
exports.isObject = isObject;
exports.isArray = isArray;
exports.isDate = isDate;
exports.isRealmObject = isRealmObject;
exports.isRealmList = isRealmList;
exports.sanitizeFloat = sanitizeFloat;
exports.sanitizeInt = sanitizeInt;
exports.sanitizeString = sanitizeString;
exports.sanitizeStringNonNull = sanitizeStringNonNull;
exports.sanitizeBool = sanitizeBool;