logger.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. 'use strict';
  2. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
  3. return typeof obj;
  4. } : function (obj) {
  5. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  6. };
  7. /**
  8. * Merge an array of objects into one.
  9. *
  10. * @param {Array<Object>} arr
  11. * @return {Object}
  12. */
  13. /**
  14. * Get the first item that pass the test
  15. * by second argument function
  16. *
  17. * @param {Array} list
  18. * @param {Function} f
  19. * @return {*}
  20. */
  21. function find(list, f) {
  22. return list.filter(f)[0];
  23. }
  24. /**
  25. * Deep copy the given object considering circular structure.
  26. * This function caches all nested objects and its copies.
  27. * If it detects circular structure, use cached copy to avoid infinite loop.
  28. *
  29. * @param {*} obj
  30. * @param {Array<Object>} cache
  31. * @return {*}
  32. */
  33. function deepCopy(obj) {
  34. var cache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  35. // just return if obj is immutable value
  36. if (obj === null || (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object') {
  37. return obj;
  38. }
  39. // if obj is hit, it is in circular structure
  40. var hit = find(cache, function (c) {
  41. return c.original === obj;
  42. });
  43. if (hit) {
  44. return hit.copy;
  45. }
  46. var copy = Array.isArray(obj) ? [] : {};
  47. // put the copy into cache at first
  48. // because we want to refer it in recursive deepCopy
  49. cache.push({
  50. original: obj,
  51. copy: copy
  52. });
  53. Object.keys(obj).forEach(function (key) {
  54. copy[key] = deepCopy(obj[key], cache);
  55. });
  56. return copy;
  57. }
  58. /**
  59. * Check whether the given value is Object or not
  60. *
  61. * @param {*} obj
  62. * @return {Boolean}
  63. */
  64. /**
  65. * Get state sub tree by given keys.
  66. *
  67. * @param {Object} state
  68. * @param {Array<String>} nestedKeys
  69. * @return {Object}
  70. */
  71. // Credits: borrowed code from fcomb/redux-logger
  72. function createLogger() {
  73. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  74. _ref$collapsed = _ref.collapsed,
  75. collapsed = _ref$collapsed === undefined ? true : _ref$collapsed,
  76. _ref$transformer = _ref.transformer,
  77. transformer = _ref$transformer === undefined ? function (state) {
  78. return state;
  79. } : _ref$transformer,
  80. _ref$mutationTransfor = _ref.mutationTransformer,
  81. mutationTransformer = _ref$mutationTransfor === undefined ? function (mut) {
  82. return mut;
  83. } : _ref$mutationTransfor;
  84. return function (store) {
  85. var prevState = deepCopy(store.state);
  86. store.subscribe(function (mutation, state) {
  87. if (typeof console === 'undefined') {
  88. return;
  89. }
  90. var nextState = deepCopy(state);
  91. var time = new Date();
  92. var formattedTime = ' @ ' + pad(time.getHours(), 2) + ':' + pad(time.getMinutes(), 2) + ':' + pad(time.getSeconds(), 2) + '.' + pad(time.getMilliseconds(), 3);
  93. var formattedMutation = mutationTransformer(mutation);
  94. var message = 'mutation ' + mutation.type + formattedTime;
  95. var startMessage = collapsed ? console.groupCollapsed : console.group;
  96. // render
  97. try {
  98. startMessage.call(console, message);
  99. } catch (e) {
  100. console.log(message);
  101. }
  102. console.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState));
  103. console.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation);
  104. console.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState));
  105. try {
  106. console.groupEnd();
  107. } catch (e) {
  108. console.log('—— log end ——');
  109. }
  110. prevState = nextState;
  111. });
  112. };
  113. }
  114. function repeat(str, times) {
  115. return new Array(times + 1).join(str);
  116. }
  117. function pad(num, maxLength) {
  118. return repeat('0', maxLength - num.toString().length) + num;
  119. }
  120. module.exports = createLogger;