logger.js 3.2 KB

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