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
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 transformer = ref . transformer ; if ( transformer === void 0 ) transformer = function ( state ) { return state ; } ;
63+ var mutationTransformer = ref . mutationTransformer ; if ( mutationTransformer === void 0 ) mutationTransformer = function ( mut ) { return mut ; } ;
64+
65+ return function ( store ) {
66+ var prevState = deepCopy ( store . state )
67+
68+ store . subscribe ( function ( mutation , state ) {
69+ if ( typeof console === 'undefined' ) {
70+ return
71+ }
72+ var nextState = deepCopy ( state )
73+ var time = new Date ( )
74+ var formattedTime = " @ " + ( pad ( time . getHours ( ) , 2 ) ) + ":" + ( pad ( time . getMinutes ( ) , 2 ) ) + ":" + ( pad ( time . getSeconds ( ) , 2 ) ) + "." + ( pad ( time . getMilliseconds ( ) , 3 ) )
75+ var formattedMutation = mutationTransformer ( mutation )
76+ var message = "mutation " + ( mutation . type ) + formattedTime
77+ var startMessage = collapsed
78+ ? console . groupCollapsed
79+ : console . group
80+
81+ // render
82+ try {
83+ startMessage . call ( console , message )
84+ } catch ( e ) {
85+ console . log ( message )
86+ }
87+
88+ console . log ( '%c prev state' , 'color: #9E9E9E; font-weight: bold' , transformer ( prevState ) )
89+ console . log ( '%c mutation' , 'color: #03A9F4; font-weight: bold' , formattedMutation )
90+ console . log ( '%c next state' , 'color: #4CAF50; font-weight: bold' , transformer ( nextState ) )
91+
92+ try {
93+ console . groupEnd ( )
94+ } catch ( e ) {
95+ console . log ( '—— log end ——' )
96+ }
97+
98+ prevState = nextState
99+ } )
100+ }
101+ }
102+
103+ function repeat ( str , times ) {
104+ return ( new Array ( times + 1 ) ) . join ( str )
105+ }
106+
107+ function pad ( num , maxLength ) {
108+ return repeat ( '0' , maxLength - num . toString ( ) . length ) + num
109+ }
110+
111+ return createLogger ;
112+
113+ } ) ) ) ;
0 commit comments