UNPKG

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