-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversion.js
159 lines (145 loc) · 3.69 KB
/
conversion.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var moment = require("moment");
/**
*
* @param {*} string Required. A string expression. Cannot be an empty string!
*/
function Asc(string) {
return String.fromCharCode(string[0]);
}
/**
*
* @param {*} expression Required. Any valid expression. A nonzero value returns True, zero returns False. A run-time error occurs if the expression can not be interpreted as a numeric value
*/
function CBool(expression) {
if (typeof expression === "string") {
var asNumber = parseFloat(expression);
if (asNumber === NaN) {
throw "Type mismatch CBool";
}
}
return expression > 0;
}
/**
*
* @param {*} expression Required. Any valid expression
*/
function CByte(expression) {
if (typeof expression === "string") {
var asNumber = parseFloat(expression);
if (asNumber === NaN) {
throw "Type mismatch CByte";
}
}
return Math.round(expression % 255);
}
/**
*
* @param {*} expression Required. Any valid expression
*/
function CCur(expression) {
return parseFloat(expression.toFixed(4));
}
/**
*
* @param {*} date Required. Any valid date expression (like Date() or Now())
*/
function CDate(date) {
return moment(date).toDate();
}
/**
* The CDbl function converts an expression to type Double.
* The expression must be a numeric value.
* @param {*} expression Required. Any valid expression
*/
function CDbl(expression) {
return parseFloat(expression);
}
/**
*
* @param {*} charcode Required. A number that identifies a character
*/
function Chr(charcode) {
return String.fromCharCode(charcode);
}
/**
*
* @param {*} expression Required. Any valid expression
*/
function CInt(expression) {
var n = parseInt(expression);
if (isNaN(n)) {
throw "Type mismatch CInt";
}
if (n < -32768 || n > 32767) {
throw "Overflow CInt";
}
}
/**
*
* @param {*} expression Required. Any valid expression
*/
function CLng(expression) {
var n = parseInt(expression);
if (isNaN(n)) {
throw "Type mismatch CLng";
}
if (n < -2147483648 || n > 2147483647) {
throw "Overflow CLng";
}
}
/**
*
* @param {*} expression CSng(expression)
*/
function CSng(expression) {
var n = parseFloat(expression);
if (isNaN(n)) {
throw "Type mismatch CSng";
}
if (n < -3.402823E38 || n > -1.401298E-45) {
throw "Overflow CSng";
}
}
/**
*
* @param {*} expression Required. Any valid expression
* If expression is:
* Boolean - then the CStr function will return a string containing true or false.
* Date - then the CStr function will return a string that contains a date in the short-date format.
* Null - then a run-time error will occur.
* Empty - then the CStr function will return an empty string ("").
* Error - then the CStr function will return a string that contains the word "Error" followed by an error number.
* Other numeric - then the CStr function will return a string that contains the number.
*/
function CStr(expression) {
if (expression === null) {
throw "Error";
}
if (expression === undefined) {
return "";
}
if (expression instanceof Date) {
// Format to vbShortDate mm/dd/yyyy
var mm = expression.getMonth() + 1;
var dd = expression.getDate();
var yyyy = expression.getFullYear();
return mm + '/' + dd + '/' + yyyy;
}
if (expression instanceof Exception) {
return "Error " + expression.message;
}
return expression.toString();
}
module.exports = {
Asc: Asc,
CBool: CBool,
CByte: CByte,
CCur: CCur,
CDate: CDate,
CDbl: CDbl,
Chr: Chr,
CInt: CInt,
CLng: CLng,
CSng: CSng,
CStr: CStr
}