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