|
1 | 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 |
| -/** |
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 |
| 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] |
34 | 17 | }
|
35 | 18 |
|
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 |
| 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 |
40 | 55 | }
|
41 | 56 |
|
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 |
| -/** |
58 |
| - * forEach for object |
59 |
| - */ |
60 |
| - |
61 |
| -// Credits: borrowed code from fcomb/redux-logger |
62 |
| - |
63 |
| -function createLogger (ref) { |
64 |
| - if ( ref === void 0 ) ref = {}; |
65 |
| - var collapsed = ref.collapsed; if ( collapsed === void 0 ) collapsed = true; |
66 |
| - var filter = ref.filter; if ( filter === void 0 ) filter = function (mutation, stateBefore, stateAfter) { return true; }; |
67 |
| - var transformer = ref.transformer; if ( transformer === void 0 ) transformer = function (state) { return state; }; |
68 |
| - var mutationTransformer = ref.mutationTransformer; if ( mutationTransformer === void 0 ) mutationTransformer = function (mut) { return mut; }; |
69 |
| - var logger = ref.logger; if ( logger === void 0 ) logger = console; |
70 |
| - |
71 |
| - return function (store) { |
72 |
| - var prevState = deepCopy(store.state); |
73 |
| - |
74 |
| - store.subscribe(function (mutation, state) { |
75 |
| - if (typeof logger === 'undefined') { |
76 |
| - return |
77 |
| - } |
78 |
| - var nextState = deepCopy(state); |
79 |
| - |
80 |
| - if (filter(mutation, prevState, nextState)) { |
81 |
| - var time = new Date(); |
82 |
| - var formattedTime = " @ " + (pad(time.getHours(), 2)) + ":" + (pad(time.getMinutes(), 2)) + ":" + (pad(time.getSeconds(), 2)) + "." + (pad(time.getMilliseconds(), 3)); |
83 |
| - var formattedMutation = mutationTransformer(mutation); |
84 |
| - var message = "mutation " + (mutation.type) + formattedTime; |
85 |
| - var startMessage = collapsed |
86 |
| - ? logger.groupCollapsed |
87 |
| - : logger.group; |
88 |
| - |
89 |
| - // render |
90 |
| - try { |
91 |
| - startMessage.call(logger, message); |
92 |
| - } catch (e) { |
93 |
| - console.log(message); |
94 |
| - } |
| 57 | + // Credits: borrowed code from fcomb/redux-logger |
95 | 58 |
|
96 |
| - logger.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState)); |
97 |
| - logger.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation); |
98 |
| - logger.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState)); |
| 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; |
99 | 66 |
|
100 |
| - try { |
101 |
| - logger.groupEnd(); |
102 |
| - } catch (e) { |
103 |
| - logger.log('—— log end ——'); |
| 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 | + } |
104 | 101 | }
|
105 |
| - } |
106 | 102 |
|
107 |
| - prevState = nextState; |
108 |
| - }); |
| 103 | + prevState = nextState; |
| 104 | + }); |
| 105 | + } |
109 | 106 | }
|
110 |
| -} |
111 | 107 |
|
112 |
| -function repeat (str, times) { |
113 |
| - return (new Array(times + 1)).join(str) |
114 |
| -} |
| 108 | + function repeat (str, times) { |
| 109 | + return (new Array(times + 1)).join(str) |
| 110 | + } |
115 | 111 |
|
116 |
| -function pad (num, maxLength) { |
117 |
| - return repeat('0', maxLength - num.toString().length) + num |
118 |
| -} |
| 112 | + function pad (num, maxLength) { |
| 113 | + return repeat('0', maxLength - num.toString().length) + num |
| 114 | + } |
119 | 115 |
|
120 |
| -return createLogger; |
| 116 | + return createLogger; |
121 | 117 |
|
122 |
| -}))); |
| 118 | +})); |
0 commit comments