No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.7KB

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