1const conversions = require('./conversions'); 2const route = require('./route'); 3 4const convert = {}; 5 6const models = Object.keys(conversions); 7 8function 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 30function 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 64models.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 81module.exports = convert; 82