UNPKG

9.06 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.12.7
2(function() {
3 var Date, DateTime, Exception, MAX_DATETIME_VALUE, MAX_DATE_VALUE, MAX_FLOAT_VALUE, MAX_INT_VALUE, MAX_TIME_VALUE, MIN_DATETIME_VALUE, MIN_DATE_VALUE, MIN_FLOAT_PRECISION_VALUE, MIN_FLOAT_VALUE, MIN_INT_VALUE, MIN_TIME_VALUE, OverFlowException, Uncertainty, isValidDecimal, isValidInteger, predecessor, ref, successor,
4 extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
5 hasProp = {}.hasOwnProperty;
6
7 Exception = require('../datatypes/exception').Exception;
8
9 ref = require('../datatypes/datetime'), DateTime = ref.DateTime, Date = ref.Date;
10
11 Uncertainty = require('../datatypes/uncertainty').Uncertainty;
12
13 module.exports.MAX_INT_VALUE = MAX_INT_VALUE = Math.pow(2, 31) - 1;
14
15 module.exports.MIN_INT_VALUE = MIN_INT_VALUE = Math.pow(-2, 31);
16
17 module.exports.MAX_FLOAT_VALUE = MAX_FLOAT_VALUE = 99999999999999999999999999999.99999999;
18
19 module.exports.MIN_FLOAT_VALUE = MIN_FLOAT_VALUE = -99999999999999999999999999999.99999999;
20
21 module.exports.MIN_FLOAT_PRECISION_VALUE = MIN_FLOAT_PRECISION_VALUE = Math.pow(10, -8);
22
23 module.exports.MIN_DATETIME_VALUE = MIN_DATETIME_VALUE = DateTime.parse("0001-01-01T00:00:00.000");
24
25 module.exports.MAX_DATETIME_VALUE = MAX_DATETIME_VALUE = DateTime.parse("9999-12-31T23:59:59.999");
26
27 module.exports.MIN_DATE_VALUE = MIN_DATE_VALUE = Date.parse("0001-01-01");
28
29 module.exports.MAX_DATE_VALUE = MAX_DATE_VALUE = Date.parse("9999-12-31");
30
31 module.exports.MIN_TIME_VALUE = MIN_TIME_VALUE = DateTime.parse("0000-01-01T00:00:00.000");
32
33 module.exports.MAX_TIME_VALUE = MAX_TIME_VALUE = DateTime.parse("0000-01-01T23:59:59.999");
34
35 module.exports.overflowsOrUnderflows = function(value) {
36 if (value == null) {
37 return false;
38 }
39 if (value.isQuantity) {
40 if (!isValidDecimal(value.value)) {
41 return true;
42 }
43 } else if ((value.isTime != null) && value.isTime()) {
44 if (value.after(MAX_TIME_VALUE)) {
45 return true;
46 }
47 if (value.before(MIN_TIME_VALUE)) {
48 return true;
49 }
50 } else if (value.isDateTime) {
51 if (value.after(MAX_DATETIME_VALUE)) {
52 return true;
53 }
54 if (value.before(MIN_DATETIME_VALUE)) {
55 return true;
56 }
57 } else if (value.isDate) {
58 if (value.after(MAX_DATE_VALUE)) {
59 return true;
60 }
61 if (value.before(MIN_DATE_VALUE)) {
62 return true;
63 }
64 } else if (Number.isInteger(value)) {
65 if (!isValidInteger(value)) {
66 return true;
67 }
68 } else {
69 if (!isValidDecimal(value)) {
70 return true;
71 }
72 }
73 return false;
74 };
75
76 module.exports.isValidInteger = isValidInteger = function(integer) {
77 if (isNaN(integer)) {
78 return false;
79 }
80 if (integer > MAX_INT_VALUE) {
81 return false;
82 }
83 if (integer < MIN_INT_VALUE) {
84 return false;
85 }
86 return true;
87 };
88
89 module.exports.isValidDecimal = isValidDecimal = function(decimal) {
90 if (isNaN(decimal)) {
91 return false;
92 }
93 if (decimal > MAX_FLOAT_VALUE) {
94 return false;
95 }
96 if (decimal < MIN_FLOAT_VALUE) {
97 return false;
98 }
99 return true;
100 };
101
102 module.exports.limitDecimalPrecision = function(decimal) {
103 var decimalPoints, decimalString, splitDecimalString;
104 decimalString = decimal.toString();
105 if (decimalString.indexOf('e') !== -1) {
106 return decimal;
107 }
108 splitDecimalString = decimalString.split('.');
109 decimalPoints = splitDecimalString[1];
110 if ((decimalPoints != null) && decimalPoints.length > 8) {
111 decimalString = splitDecimalString[0] + '.' + splitDecimalString[1].substring(0, 8);
112 }
113 return parseFloat(decimalString);
114 };
115
116 module.exports.OverFlowException = OverFlowException = OverFlowException = (function(superClass) {
117 extend(OverFlowException, superClass);
118
119 function OverFlowException() {
120 return OverFlowException.__super__.constructor.apply(this, arguments);
121 }
122
123 return OverFlowException;
124
125 })(Exception);
126
127 module.exports.successor = successor = function(val) {
128 var e, high, succ;
129 if (typeof val === "number") {
130 if (parseInt(val) === val) {
131 if (val === MAX_INT_VALUE) {
132 throw new OverFlowException();
133 } else {
134 return val + 1;
135 }
136 } else {
137 return val + MIN_FLOAT_PRECISION_VALUE;
138 }
139 } else if (val != null ? val.isDateTime : void 0) {
140 if (val.sameAs(MAX_DATETIME_VALUE)) {
141 throw new OverFlowException();
142 } else {
143 return val.successor();
144 }
145 } else if (val != null ? val.isDate : void 0) {
146 if (val.sameAs(MAX_DATE_VALUE)) {
147 throw new OverFlowException();
148 } else {
149 return val.successor();
150 }
151 } else if (val != null ? val.isTime : void 0) {
152 if (val.sameAs(MAX_TIME_VALUE)) {
153 throw new OverFlowException();
154 } else {
155 return val.successor();
156 }
157 } else if (val != null ? val.isUncertainty : void 0) {
158 high = (function() {
159 try {
160 return successor(val.high);
161 } catch (error) {
162 e = error;
163 return val.high;
164 }
165 })();
166 return new Uncertainty(successor(val.low), high);
167 } else if (val != null ? val.isQuantity : void 0) {
168 succ = val.clone();
169 succ.value = successor(val.value);
170 return succ;
171 } else if (val == null) {
172 return null;
173 }
174 };
175
176 module.exports.predecessor = predecessor = function(val) {
177 var e, low, pred;
178 if (typeof val === "number") {
179 if (parseInt(val) === val) {
180 if (val === MIN_INT_VALUE) {
181 throw new OverFlowException();
182 } else {
183 return val - 1;
184 }
185 } else {
186 return val - MIN_FLOAT_PRECISION_VALUE;
187 }
188 } else if (val != null ? val.isDateTime : void 0) {
189 if (val.sameAs(MIN_DATETIME_VALUE)) {
190 throw new OverFlowException();
191 } else {
192 return val.predecessor();
193 }
194 } else if (val != null ? val.isDate : void 0) {
195 if (val.sameAs(MIN_DATE_VALUE)) {
196 throw new OverFlowException();
197 } else {
198 return val.predecessor();
199 }
200 } else if (val != null ? val.isTime : void 0) {
201 if (val.sameAs(MIN_TIME_VALUE)) {
202 throw new OverFlowException();
203 } else {
204 return val.predecessor();
205 }
206 } else if (val != null ? val.isUncertainty : void 0) {
207 low = (function() {
208 try {
209 return predecessor(val.low);
210 } catch (error) {
211 e = error;
212 return val.low;
213 }
214 })();
215 return new Uncertainty(low, predecessor(val.high));
216 } else if (val != null ? val.isQuantity : void 0) {
217 pred = val.clone();
218 pred.value = predecessor(val.value);
219 return pred;
220 } else if (val == null) {
221 return null;
222 }
223 };
224
225 module.exports.maxValueForInstance = function(val) {
226 var val2;
227 if (typeof val === "number") {
228 if (parseInt(val) === val) {
229 return MAX_INT_VALUE;
230 } else {
231 return MAX_FLOAT_VALUE;
232 }
233 } else if (val != null ? val.isDateTime : void 0) {
234 return MAX_DATETIME_VALUE.copy();
235 } else if (val != null ? val.isDate : void 0) {
236 return MAX_DATE_VALUE.copy();
237 } else if (val != null ? val.isTime : void 0) {
238 return MAX_TIME_VALUE.copy();
239 } else if (val != null ? val.isQuantity : void 0) {
240 val2 = val.clone();
241 val2.value = maxValueForInstance(val2.value);
242 return val2;
243 } else {
244 return null;
245 }
246 };
247
248 module.exports.minValueForInstance = function(val) {
249 var val2;
250 if (typeof val === "number") {
251 if (parseInt(val) === val) {
252 return MIN_INT_VALUE;
253 } else {
254 return MIN_FLOAT_VALUE;
255 }
256 } else if (val != null ? val.isDateTime : void 0) {
257 return MIN_DATETIME_VALUE.copy();
258 } else if (val != null ? val.isDate : void 0) {
259 return MIN_DATE_VALUE.copy();
260 } else if (val != null ? val.isTime : void 0) {
261 return MIN_TIME_VALUE.copy();
262 } else if (val != null ? val.isQuantity : void 0) {
263 val2 = val.clone();
264 val2.value = minValueForInstance(val2.value);
265 return val2;
266 } else {
267 return null;
268 }
269 };
270
271 module.exports.decimalAdjust = function(type, value, exp) {
272 var v;
273 if (typeof exp === 'undefined' || +exp === 0) {
274 return Math[type](value);
275 }
276 value = +value;
277 exp = +exp;
278 if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
279 return 0/0;
280 }
281 value = value.toString().split('e');
282 v = value[1] ? +value[1] - exp : -exp;
283 value = Math[type](+(value[0] + 'e' + v));
284 value = value.toString().split('e');
285 v = value[1] ? +value[1] + exp : exp;
286 return +(value[0] + 'e' + v);
287 };
288
289}).call(this);
290
291//# sourceMappingURL=math.js.map