UNPKG

7.51 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.12.7
2(function() {
3 var DateTime, Uncertainty, areDateTimesOrQuantities, areNumbers, classesEqual, codesAreEquivalent, compareEveryItemInArrays, compareObjects, deepCompareKeysAndValues, equals, equivalent, getClassOfObjects, getKeysFromObject, isCode, isFunction, isUncertainty;
4
5 DateTime = require('../datatypes/datetime').DateTime;
6
7 Uncertainty = require('../datatypes/uncertainty').Uncertainty;
8
9 areNumbers = function(a, b) {
10 return typeof a === 'number' && typeof b === 'number';
11 };
12
13 areDateTimesOrQuantities = function(a, b) {
14 return (a != null ? a.isDateTime : void 0) && (b != null ? b.isDateTime : void 0) || (a != null ? a.isDate : void 0) && (b != null ? b.isDate : void 0) || (a != null ? a.isTime : void 0) && (b != null ? b.isTime : void 0) || (a != null ? a.isQuantity : void 0) && (b != null ? b.isQuantity : void 0);
15 };
16
17 isUncertainty = function(x) {
18 return x instanceof Uncertainty;
19 };
20
21 module.exports.lessThan = function(a, b, precision) {
22 switch (false) {
23 case !areNumbers(a, b):
24 return a < b;
25 case !areDateTimesOrQuantities(a, b):
26 return a.before(b, precision);
27 case !isUncertainty(a):
28 return a.lessThan(b);
29 case !isUncertainty(b):
30 return Uncertainty.from(a).lessThan(b);
31 default:
32 return null;
33 }
34 };
35
36 module.exports.lessThanOrEquals = function(a, b, precision) {
37 switch (false) {
38 case !areNumbers(a, b):
39 return a <= b;
40 case !areDateTimesOrQuantities(a, b):
41 return a.sameOrBefore(b, precision);
42 case !isUncertainty(a):
43 return a.lessThanOrEquals(b);
44 case !isUncertainty(b):
45 return Uncertainty.from(a).lessThanOrEquals(b);
46 default:
47 return null;
48 }
49 };
50
51 module.exports.greaterThan = function(a, b, precision) {
52 switch (false) {
53 case !areNumbers(a, b):
54 return a > b;
55 case !areDateTimesOrQuantities(a, b):
56 return a.after(b, precision);
57 case !isUncertainty(a):
58 return a.greaterThan(b);
59 case !isUncertainty(b):
60 return Uncertainty.from(a).greaterThan(b);
61 default:
62 return null;
63 }
64 };
65
66 module.exports.greaterThanOrEquals = function(a, b, precision) {
67 switch (false) {
68 case !areNumbers(a, b):
69 return a >= b;
70 case !areDateTimesOrQuantities(a, b):
71 return a.sameOrAfter(b, precision);
72 case !isUncertainty(a):
73 return a.greaterThanOrEquals(b);
74 case !isUncertainty(b):
75 return Uncertainty.from(a).greaterThanOrEquals(b);
76 default:
77 return null;
78 }
79 };
80
81 module.exports.equivalent = equivalent = function(a, b) {
82 var aClass, bClass, ref;
83 if ((a == null) && (b == null)) {
84 return true;
85 }
86 if (!((a != null) && (b != null))) {
87 return false;
88 }
89 if (isCode(a)) {
90 return codesAreEquivalent(a, b);
91 }
92 if (typeof a.equivalent === 'function') {
93 return a.equivalent(b);
94 }
95 ref = getClassOfObjects(a, b), aClass = ref[0], bClass = ref[1];
96 switch (aClass) {
97 case '[object Array]':
98 return compareEveryItemInArrays(a, b, equivalent);
99 case '[object Object]':
100 return compareObjects(a, b, equivalent);
101 case '[object String]':
102 if (bClass === '[object String]') {
103 a = a.replace(/\s/g, ' ');
104 b = b.replace(/\s/g, ' ');
105 return (a.localeCompare(b, 'en', {
106 sensitivity: 'base'
107 })) === 0;
108 }
109 }
110 return equals(a, b);
111 };
112
113 isCode = function(object) {
114 return object.hasMatch && typeof object.hasMatch === 'function';
115 };
116
117 codesAreEquivalent = function(code1, code2) {
118 return code1.hasMatch(code2);
119 };
120
121 getClassOfObjects = function(object1, object2) {
122 var obj;
123 return (function() {
124 var j, len, ref, results;
125 ref = [object1, object2];
126 results = [];
127 for (j = 0, len = ref.length; j < len; j++) {
128 obj = ref[j];
129 results.push({}.toString.call(obj));
130 }
131 return results;
132 })();
133 };
134
135 compareEveryItemInArrays = function(array1, array2, comparisonFunction) {
136 return array1.length === array2.length && array1.every(function(item, i) {
137 return comparisonFunction(item, array2[i]);
138 });
139 };
140
141 compareObjects = function(a, b, comparisonFunction) {
142 if (!classesEqual(a, b)) {
143 return false;
144 }
145 return deepCompareKeysAndValues(a, b, comparisonFunction);
146 };
147
148 classesEqual = function(object1, object2) {
149 return object2 instanceof object1.constructor && object1 instanceof object2.constructor;
150 };
151
152 deepCompareKeysAndValues = function(a, b, comparisonFunction) {
153 var aKeys, bKeys, finalComparisonResult, shouldReturnNull;
154 aKeys = getKeysFromObject(a).sort();
155 bKeys = getKeysFromObject(b).sort();
156 shouldReturnNull = false;
157 if (aKeys.length === bKeys.length && aKeys.every((function(_this) {
158 return function(value, index) {
159 return value === bKeys[index];
160 };
161 })(this))) {
162 finalComparisonResult = aKeys.every(function(key) {
163 var comparisonResult;
164 if ((a[key] == null) && (b[key] == null)) {
165 return true;
166 }
167 comparisonResult = comparisonFunction(a[key], b[key]);
168 if (comparisonResult === null) {
169 shouldReturnNull = true;
170 }
171 return comparisonResult;
172 });
173 } else {
174 finalComparisonResult = false;
175 }
176 if (shouldReturnNull) {
177 return null;
178 }
179 return finalComparisonResult;
180 };
181
182 getKeysFromObject = function(object) {
183 var key, objectClass;
184 objectClass = {}.toString.call(object);
185 return ((function() {
186 var results;
187 if (!isFunction(key)) {
188 results = [];
189 for (key in object) {
190 results.push(key);
191 }
192 return results;
193 }
194 })());
195 };
196
197 isFunction = function(input) {
198 return input instanceof Function || {}.toString.call(input) === '[object Function]';
199 };
200
201 module.exports.equals = equals = function(a, b) {
202 var aClass, bClass, ref;
203 if (!((a != null) && (b != null))) {
204 return null;
205 }
206 if (a != null ? a.isQuantity : void 0) {
207 return a.equals(b);
208 }
209 if (a != null ? a.isRatio : void 0) {
210 return a.equals(b);
211 }
212 if (a instanceof Uncertainty) {
213 b = Uncertainty.from(b);
214 } else if (b instanceof Uncertainty) {
215 a = Uncertainty.from(a);
216 }
217 if (typeof a.equals === 'function') {
218 return a.equals(b);
219 }
220 if (typeof a === typeof b && typeof a === 'string' || typeof a === 'number' || typeof a === 'boolean') {
221 return a === b;
222 }
223 ref = getClassOfObjects(a, b), aClass = ref[0], bClass = ref[1];
224 if (aClass !== bClass) {
225 return false;
226 }
227 switch (aClass) {
228 case '[object Date]':
229 return a.getTime() === b.getTime();
230 case '[object RegExp]':
231 return ['source', 'global', 'ignoreCase', 'multiline'].every(function(p) {
232 return a[p] === b[p];
233 });
234 case '[object Array]':
235 if (a.indexOf(null) >= 0 || a.indexOf(void 0) >= 0 || b.indexOf(null) >= 0 || b.indexOf(void 0) >= 0) {
236 return null;
237 }
238 return compareEveryItemInArrays(a, b, equals);
239 case '[object Object]':
240 return compareObjects(a, b, equals);
241 case '[object Function]':
242 return a.toString() === b.toString();
243 }
244 return false;
245 };
246
247}).call(this);
248
249//# sourceMappingURL=comparison.js.map