1 | const conversions = require('./conversions');
|
---|
2 | const route = require('./route');
|
---|
3 |
|
---|
4 | const convert = {};
|
---|
5 |
|
---|
6 | const models = Object.keys(conversions);
|
---|
7 |
|
---|
8 | function wrapRaw(fn) {
|
---|
9 | const wrappedFn = function (...args) {
|
---|
10 | const arg0 = args[0];
|
---|
11 | if (arg0 === undefined || arg0 === null) {
|
---|
12 | return arg0;
|
---|
13 | }
|
---|
14 |
|
---|
15 | if (arg0.length > 1) {
|
---|
16 | args = arg0;
|
---|
17 | }
|
---|
18 |
|
---|
19 | return fn(args);
|
---|
20 | };
|
---|
21 |
|
---|
22 | // Preserve .conversion property if there is one
|
---|
23 | if ('conversion' in fn) {
|
---|
24 | wrappedFn.conversion = fn.conversion;
|
---|
25 | }
|
---|
26 |
|
---|
27 | return wrappedFn;
|
---|
28 | }
|
---|
29 |
|
---|
30 | function wrapRounded(fn) {
|
---|
31 | const wrappedFn = function (...args) {
|
---|
32 | const arg0 = args[0];
|
---|
33 |
|
---|
34 | if (arg0 === undefined || arg0 === null) {
|
---|
35 | return arg0;
|
---|
36 | }
|
---|
37 |
|
---|
38 | if (arg0.length > 1) {
|
---|
39 | args = arg0;
|
---|
40 | }
|
---|
41 |
|
---|
42 | const result = fn(args);
|
---|
43 |
|
---|
44 | // We're assuming the result is an array here.
|
---|
45 | // see notice in conversions.js; don't use box types
|
---|
46 | // in conversion functions.
|
---|
47 | if (typeof result === 'object') {
|
---|
48 | for (let len = result.length, i = 0; i < len; i++) {
|
---|
49 | result[i] = Math.round(result[i]);
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | return result;
|
---|
54 | };
|
---|
55 |
|
---|
56 | // Preserve .conversion property if there is one
|
---|
57 | if ('conversion' in fn) {
|
---|
58 | wrappedFn.conversion = fn.conversion;
|
---|
59 | }
|
---|
60 |
|
---|
61 | return wrappedFn;
|
---|
62 | }
|
---|
63 |
|
---|
64 | models.forEach(fromModel => {
|
---|
65 | convert[fromModel] = {};
|
---|
66 |
|
---|
67 | Object.defineProperty(convert[fromModel], 'channels', {value: conversions[fromModel].channels});
|
---|
68 | Object.defineProperty(convert[fromModel], 'labels', {value: conversions[fromModel].labels});
|
---|
69 |
|
---|
70 | const routes = route(fromModel);
|
---|
71 | const routeModels = Object.keys(routes);
|
---|
72 |
|
---|
73 | routeModels.forEach(toModel => {
|
---|
74 | const fn = routes[toModel];
|
---|
75 |
|
---|
76 | convert[fromModel][toModel] = wrapRounded(fn);
|
---|
77 | convert[fromModel][toModel].raw = wrapRaw(fn);
|
---|
78 | });
|
---|
79 | });
|
---|
80 |
|
---|
81 | module.exports = convert;
|
---|