1 | (function webpackUniversalModuleDefinition(root, factory) {
|
2 | if(typeof exports === 'object' && typeof module === 'object')
|
3 | module.exports = factory();
|
4 | else if(typeof define === 'function' && define.amd)
|
5 | define([], factory);
|
6 | else if(typeof exports === 'object')
|
7 | exports["reduxLogger"] = factory();
|
8 | else
|
9 | root["reduxLogger"] = factory();
|
10 | })(this, function() {
|
11 | return (function(modules) {
|
12 |
|
13 | var installedModules = {};
|
14 |
|
15 |
|
16 | function __webpack_require__(moduleId) {
|
17 |
|
18 |
|
19 | if(installedModules[moduleId])
|
20 | return installedModules[moduleId].exports;
|
21 |
|
22 |
|
23 | var module = installedModules[moduleId] = {
|
24 | exports: {},
|
25 | id: moduleId,
|
26 | loaded: false
|
27 | };
|
28 |
|
29 |
|
30 | modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
31 |
|
32 |
|
33 | module.loaded = true;
|
34 |
|
35 |
|
36 | return module.exports;
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 | __webpack_require__.m = modules;
|
42 |
|
43 |
|
44 | __webpack_require__.c = installedModules;
|
45 |
|
46 |
|
47 | __webpack_require__.p = "";
|
48 |
|
49 |
|
50 | return __webpack_require__(0);
|
51 | })
|
52 |
|
53 | ([
|
54 |
|
55 | function(module, exports, __webpack_require__) {
|
56 |
|
57 | __webpack_require__(2);
|
58 | module.exports = __webpack_require__(2);
|
59 |
|
60 |
|
61 | },
|
62 |
|
63 | function(module, exports) {
|
64 |
|
65 | "use strict";
|
66 |
|
67 | Object.defineProperty(exports, "__esModule", {
|
68 | value: true
|
69 | });
|
70 | var repeat = exports.repeat = function repeat(str, times) {
|
71 | return new Array(times + 1).join(str);
|
72 | };
|
73 |
|
74 | var pad = exports.pad = function pad(num, maxLength) {
|
75 | return repeat("0", maxLength - num.toString().length) + num;
|
76 | };
|
77 |
|
78 | var formatTime = exports.formatTime = function formatTime(time) {
|
79 | return pad(time.getHours(), 2) + ":" + pad(time.getMinutes(), 2) + ":" + pad(time.getSeconds(), 2) + "." + pad(time.getMilliseconds(), 3);
|
80 | };
|
81 |
|
82 |
|
83 | var timer = exports.timer = typeof performance !== "undefined" && performance !== null && typeof performance.now === "function" ? performance : Date;
|
84 |
|
85 | },
|
86 |
|
87 | function(module, exports, __webpack_require__) {
|
88 |
|
89 | 'use strict';
|
90 |
|
91 | Object.defineProperty(exports, "__esModule", {
|
92 | value: true
|
93 | });
|
94 |
|
95 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
96 |
|
97 | var _core = __webpack_require__(3);
|
98 |
|
99 | var _helpers = __webpack_require__(1);
|
100 |
|
101 | var _defaults = __webpack_require__(4);
|
102 |
|
103 | var _defaults2 = _interopRequireDefault(_defaults);
|
104 |
|
105 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
106 |
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | function createLogger() {
|
127 | var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
128 |
|
129 | var loggerOptions = _extends({}, _defaults2.default, options);
|
130 |
|
131 | var logger = loggerOptions.logger,
|
132 | transformer = loggerOptions.transformer,
|
133 | stateTransformer = loggerOptions.stateTransformer,
|
134 | errorTransformer = loggerOptions.errorTransformer,
|
135 | predicate = loggerOptions.predicate,
|
136 | logErrors = loggerOptions.logErrors,
|
137 | diffPredicate = loggerOptions.diffPredicate;
|
138 |
|
139 |
|
140 |
|
141 | if (typeof logger === 'undefined') {
|
142 | return function () {
|
143 | return function (next) {
|
144 | return function (action) {
|
145 | return next(action);
|
146 | };
|
147 | };
|
148 | };
|
149 | }
|
150 |
|
151 | if (transformer) {
|
152 | console.error('Option \'transformer\' is deprecated, use \'stateTransformer\' instead!');
|
153 | }
|
154 |
|
155 |
|
156 | if (options.getState && options.dispatch) {
|
157 |
|
158 | console.error('redux-logger not installed. Make sure to pass logger instance as middleware:\n\nimport createLogger from \'redux-logger\';\n\nconst logger = createLogger();\nconst store = createStore(\n reducer,\n applyMiddleware(logger)\n);');
|
159 |
|
160 | return function () {
|
161 | return function (next) {
|
162 | return function (action) {
|
163 | return next(action);
|
164 | };
|
165 | };
|
166 | };
|
167 | }
|
168 |
|
169 | var logBuffer = [];
|
170 |
|
171 | return function (_ref) {
|
172 | var getState = _ref.getState;
|
173 | return function (next) {
|
174 | return function (action) {
|
175 |
|
176 | if (typeof predicate === 'function' && !predicate(getState, action)) {
|
177 | return next(action);
|
178 | }
|
179 |
|
180 | var logEntry = {};
|
181 | logBuffer.push(logEntry);
|
182 |
|
183 | logEntry.started = _helpers.timer.now();
|
184 | logEntry.startedTime = new Date();
|
185 | logEntry.prevState = stateTransformer(getState());
|
186 | logEntry.action = action;
|
187 |
|
188 | var returnedValue = void 0;
|
189 | if (logErrors) {
|
190 | try {
|
191 | returnedValue = next(action);
|
192 | } catch (e) {
|
193 | logEntry.error = errorTransformer(e);
|
194 | }
|
195 | } else {
|
196 | returnedValue = next(action);
|
197 | }
|
198 |
|
199 | logEntry.took = _helpers.timer.now() - logEntry.started;
|
200 | logEntry.nextState = stateTransformer(getState());
|
201 |
|
202 | var diff = loggerOptions.diff && typeof diffPredicate === 'function' ? diffPredicate(getState, action) : loggerOptions.diff;
|
203 |
|
204 | (0, _core.printBuffer)(logBuffer, _extends({}, loggerOptions, { diff: diff }));
|
205 | logBuffer.length = 0;
|
206 |
|
207 | if (logEntry.error) throw logEntry.error;
|
208 | return returnedValue;
|
209 | };
|
210 | };
|
211 | };
|
212 | }
|
213 |
|
214 | exports.default = createLogger;
|
215 | module.exports = exports['default'];
|
216 |
|
217 | },
|
218 |
|
219 | function(module, exports, __webpack_require__) {
|
220 |
|
221 | 'use strict';
|
222 |
|
223 | Object.defineProperty(exports, "__esModule", {
|
224 | value: true
|
225 | });
|
226 |
|
227 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
228 |
|
229 | exports.printBuffer = printBuffer;
|
230 |
|
231 | var _helpers = __webpack_require__(1);
|
232 |
|
233 | var _diff = __webpack_require__(5);
|
234 |
|
235 | var _diff2 = _interopRequireDefault(_diff);
|
236 |
|
237 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
238 |
|
239 | function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
240 |
|
241 | |
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 | function getLogLevel(level, action, payload, type) {
|
252 | switch (typeof level === 'undefined' ? 'undefined' : _typeof(level)) {
|
253 | case 'object':
|
254 | return typeof level[type] === 'function' ? level[type].apply(level, _toConsumableArray(payload)) : level[type];
|
255 | case 'function':
|
256 | return level(action);
|
257 | default:
|
258 | return level;
|
259 | }
|
260 | }
|
261 |
|
262 | function defaultTitleFormatter(options) {
|
263 | var timestamp = options.timestamp,
|
264 | duration = options.duration;
|
265 |
|
266 |
|
267 | return function (action, time, took) {
|
268 | var parts = ['action'];
|
269 |
|
270 | if (timestamp) parts.push('@ ' + time);
|
271 | parts.push(String(action.type));
|
272 | if (duration) parts.push('(in ' + took.toFixed(2) + ' ms)');
|
273 |
|
274 | return parts.join(' ');
|
275 | };
|
276 | }
|
277 |
|
278 | function printBuffer(buffer, options) {
|
279 | var logger = options.logger,
|
280 | actionTransformer = options.actionTransformer,
|
281 | _options$titleFormatt = options.titleFormatter,
|
282 | titleFormatter = _options$titleFormatt === undefined ? defaultTitleFormatter(options) : _options$titleFormatt,
|
283 | collapsed = options.collapsed,
|
284 | colors = options.colors,
|
285 | level = options.level,
|
286 | diff = options.diff;
|
287 |
|
288 |
|
289 | buffer.forEach(function (logEntry, key) {
|
290 | var started = logEntry.started,
|
291 | startedTime = logEntry.startedTime,
|
292 | action = logEntry.action,
|
293 | prevState = logEntry.prevState,
|
294 | error = logEntry.error;
|
295 | var took = logEntry.took,
|
296 | nextState = logEntry.nextState;
|
297 |
|
298 | var nextEntry = buffer[key + 1];
|
299 |
|
300 | if (nextEntry) {
|
301 | nextState = nextEntry.prevState;
|
302 | took = nextEntry.started - started;
|
303 | }
|
304 |
|
305 |
|
306 | var formattedAction = actionTransformer(action);
|
307 | var isCollapsed = typeof collapsed === 'function' ? collapsed(function () {
|
308 | return nextState;
|
309 | }, action, logEntry) : collapsed;
|
310 |
|
311 | var formattedTime = (0, _helpers.formatTime)(startedTime);
|
312 | var titleCSS = colors.title ? 'color: ' + colors.title(formattedAction) + ';' : null;
|
313 | var title = titleFormatter(formattedAction, formattedTime, took);
|
314 |
|
315 |
|
316 | try {
|
317 | if (isCollapsed) {
|
318 | if (colors.title) logger.groupCollapsed('%c ' + title, titleCSS);else logger.groupCollapsed(title);
|
319 | } else {
|
320 | if (colors.title) logger.group('%c ' + title, titleCSS);else logger.group(title);
|
321 | }
|
322 | } catch (e) {
|
323 | logger.log(title);
|
324 | }
|
325 |
|
326 | var prevStateLevel = getLogLevel(level, formattedAction, [prevState], 'prevState');
|
327 | var actionLevel = getLogLevel(level, formattedAction, [formattedAction], 'action');
|
328 | var errorLevel = getLogLevel(level, formattedAction, [error, prevState], 'error');
|
329 | var nextStateLevel = getLogLevel(level, formattedAction, [nextState], 'nextState');
|
330 |
|
331 | if (prevStateLevel) {
|
332 | if (colors.prevState) logger[prevStateLevel]('%c prev state', 'color: ' + colors.prevState(prevState) + '; font-weight: bold', prevState);else logger[prevStateLevel]('prev state', prevState);
|
333 | }
|
334 |
|
335 | if (actionLevel) {
|
336 | if (colors.action) logger[actionLevel]('%c action', 'color: ' + colors.action(formattedAction) + '; font-weight: bold', formattedAction);else logger[actionLevel]('action', formattedAction);
|
337 | }
|
338 |
|
339 | if (error && errorLevel) {
|
340 | if (colors.error) logger[errorLevel]('%c error', 'color: ' + colors.error(error, prevState) + '; font-weight: bold', error);else logger[errorLevel]('error', error);
|
341 | }
|
342 |
|
343 | if (nextStateLevel) {
|
344 | if (colors.nextState) logger[nextStateLevel]('%c next state', 'color: ' + colors.nextState(nextState) + '; font-weight: bold', nextState);else logger[nextStateLevel]('next state', nextState);
|
345 | }
|
346 |
|
347 | if (diff) {
|
348 | (0, _diff2.default)(prevState, nextState, logger, isCollapsed);
|
349 | }
|
350 |
|
351 | try {
|
352 | logger.groupEnd();
|
353 | } catch (e) {
|
354 | logger.log('\u2014\u2014 log end \u2014\u2014');
|
355 | }
|
356 | });
|
357 | }
|
358 |
|
359 | },
|
360 |
|
361 | function(module, exports) {
|
362 |
|
363 | "use strict";
|
364 |
|
365 | Object.defineProperty(exports, "__esModule", {
|
366 | value: true
|
367 | });
|
368 | exports.default = {
|
369 | level: "log",
|
370 | logger: console,
|
371 | logErrors: true,
|
372 | collapsed: undefined,
|
373 | predicate: undefined,
|
374 | duration: false,
|
375 | timestamp: true,
|
376 | stateTransformer: function stateTransformer(state) {
|
377 | return state;
|
378 | },
|
379 | actionTransformer: function actionTransformer(action) {
|
380 | return action;
|
381 | },
|
382 | errorTransformer: function errorTransformer(error) {
|
383 | return error;
|
384 | },
|
385 | colors: {
|
386 | title: function title() {
|
387 | return "inherit";
|
388 | },
|
389 | prevState: function prevState() {
|
390 | return "#9E9E9E";
|
391 | },
|
392 | action: function action() {
|
393 | return "#03A9F4";
|
394 | },
|
395 | nextState: function nextState() {
|
396 | return "#4CAF50";
|
397 | },
|
398 | error: function error() {
|
399 | return "#F20404";
|
400 | }
|
401 | },
|
402 | diff: false,
|
403 | diffPredicate: undefined,
|
404 |
|
405 |
|
406 | transformer: undefined
|
407 | };
|
408 | module.exports = exports["default"];
|
409 |
|
410 | },
|
411 |
|
412 | function(module, exports, __webpack_require__) {
|
413 |
|
414 | 'use strict';
|
415 |
|
416 | Object.defineProperty(exports, "__esModule", {
|
417 | value: true
|
418 | });
|
419 | exports.default = diffLogger;
|
420 |
|
421 | var _deepDiff = __webpack_require__(6);
|
422 |
|
423 | var _deepDiff2 = _interopRequireDefault(_deepDiff);
|
424 |
|
425 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
426 |
|
427 | function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
428 |
|
429 |
|
430 | var dictionary = {
|
431 | 'E': {
|
432 | color: '#2196F3',
|
433 | text: 'CHANGED:'
|
434 | },
|
435 | 'N': {
|
436 | color: '#4CAF50',
|
437 | text: 'ADDED:'
|
438 | },
|
439 | 'D': {
|
440 | color: '#F44336',
|
441 | text: 'DELETED:'
|
442 | },
|
443 | 'A': {
|
444 | color: '#2196F3',
|
445 | text: 'ARRAY:'
|
446 | }
|
447 | };
|
448 |
|
449 | function style(kind) {
|
450 | return 'color: ' + dictionary[kind].color + '; font-weight: bold';
|
451 | }
|
452 |
|
453 | function render(diff) {
|
454 | var kind = diff.kind,
|
455 | path = diff.path,
|
456 | lhs = diff.lhs,
|
457 | rhs = diff.rhs,
|
458 | index = diff.index,
|
459 | item = diff.item;
|
460 |
|
461 |
|
462 | switch (kind) {
|
463 | case 'E':
|
464 | return [path.join('.'), lhs, '\u2192', rhs];
|
465 | case 'N':
|
466 | return [path.join('.'), rhs];
|
467 | case 'D':
|
468 | return [path.join('.')];
|
469 | case 'A':
|
470 | return [path.join('.') + '[' + index + ']', item];
|
471 | default:
|
472 | return [];
|
473 | }
|
474 | }
|
475 |
|
476 | function diffLogger(prevState, newState, logger, isCollapsed) {
|
477 | var diff = (0, _deepDiff2.default)(prevState, newState);
|
478 |
|
479 | try {
|
480 | if (isCollapsed) {
|
481 | logger.groupCollapsed('diff');
|
482 | } else {
|
483 | logger.group('diff');
|
484 | }
|
485 | } catch (e) {
|
486 | logger.log('diff');
|
487 | }
|
488 |
|
489 | if (diff) {
|
490 | diff.forEach(function (elem) {
|
491 | var kind = elem.kind;
|
492 |
|
493 | var output = render(elem);
|
494 |
|
495 | logger.log.apply(logger, ['%c ' + dictionary[kind].text, style(kind)].concat(_toConsumableArray(output)));
|
496 | });
|
497 | } else {
|
498 | logger.log('\u2014\u2014 no diff \u2014\u2014');
|
499 | }
|
500 |
|
501 | try {
|
502 | logger.groupEnd();
|
503 | } catch (e) {
|
504 | logger.log('\u2014\u2014 diff end \u2014\u2014 ');
|
505 | }
|
506 | }
|
507 | module.exports = exports['default'];
|
508 |
|
509 | },
|
510 |
|
511 | function(module, exports, __webpack_require__) {
|
512 |
|
513 | var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;(function(global) { |
514 |
|
515 |
|
516 |
|
517 | ;(function(root, factory) {
|
518 | 'use strict';
|
519 | if (true) {
|
520 |
|
521 | !(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_RESULT__ = function() {
|
522 | return factory();
|
523 | }.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
|
524 | } else if (typeof exports === 'object') {
|
525 |
|
526 |
|
527 |
|
528 | module.exports = factory();
|
529 | } else {
|
530 |
|
531 | root.DeepDiff = factory();
|
532 | }
|
533 | }(this, function(undefined) {
|
534 | 'use strict';
|
535 |
|
536 | var $scope, conflict, conflictResolution = [];
|
537 | if (typeof global === 'object' && global) {
|
538 | $scope = global;
|
539 | } else if (typeof window !== 'undefined') {
|
540 | $scope = window;
|
541 | } else {
|
542 | $scope = {};
|
543 | }
|
544 | conflict = $scope.DeepDiff;
|
545 | if (conflict) {
|
546 | conflictResolution.push(
|
547 | function() {
|
548 | if ('undefined' !== typeof conflict && $scope.DeepDiff === accumulateDiff) {
|
549 | $scope.DeepDiff = conflict;
|
550 | conflict = undefined;
|
551 | }
|
552 | });
|
553 | }
|
554 |
|
555 |
|
556 | function inherits(ctor, superCtor) {
|
557 | ctor.super_ = superCtor;
|
558 | ctor.prototype = Object.create(superCtor.prototype, {
|
559 | constructor: {
|
560 | value: ctor,
|
561 | enumerable: false,
|
562 | writable: true,
|
563 | configurable: true
|
564 | }
|
565 | });
|
566 | }
|
567 |
|
568 | function Diff(kind, path) {
|
569 | Object.defineProperty(this, 'kind', {
|
570 | value: kind,
|
571 | enumerable: true
|
572 | });
|
573 | if (path && path.length) {
|
574 | Object.defineProperty(this, 'path', {
|
575 | value: path,
|
576 | enumerable: true
|
577 | });
|
578 | }
|
579 | }
|
580 |
|
581 | function DiffEdit(path, origin, value) {
|
582 | DiffEdit.super_.call(this, 'E', path);
|
583 | Object.defineProperty(this, 'lhs', {
|
584 | value: origin,
|
585 | enumerable: true
|
586 | });
|
587 | Object.defineProperty(this, 'rhs', {
|
588 | value: value,
|
589 | enumerable: true
|
590 | });
|
591 | }
|
592 | inherits(DiffEdit, Diff);
|
593 |
|
594 | function DiffNew(path, value) {
|
595 | DiffNew.super_.call(this, 'N', path);
|
596 | Object.defineProperty(this, 'rhs', {
|
597 | value: value,
|
598 | enumerable: true
|
599 | });
|
600 | }
|
601 | inherits(DiffNew, Diff);
|
602 |
|
603 | function DiffDeleted(path, value) {
|
604 | DiffDeleted.super_.call(this, 'D', path);
|
605 | Object.defineProperty(this, 'lhs', {
|
606 | value: value,
|
607 | enumerable: true
|
608 | });
|
609 | }
|
610 | inherits(DiffDeleted, Diff);
|
611 |
|
612 | function DiffArray(path, index, item) {
|
613 | DiffArray.super_.call(this, 'A', path);
|
614 | Object.defineProperty(this, 'index', {
|
615 | value: index,
|
616 | enumerable: true
|
617 | });
|
618 | Object.defineProperty(this, 'item', {
|
619 | value: item,
|
620 | enumerable: true
|
621 | });
|
622 | }
|
623 | inherits(DiffArray, Diff);
|
624 |
|
625 | function arrayRemove(arr, from, to) {
|
626 | var rest = arr.slice((to || from) + 1 || arr.length);
|
627 | arr.length = from < 0 ? arr.length + from : from;
|
628 | arr.push.apply(arr, rest);
|
629 | return arr;
|
630 | }
|
631 |
|
632 | function realTypeOf(subject) {
|
633 | var type = typeof subject;
|
634 | if (type !== 'object') {
|
635 | return type;
|
636 | }
|
637 |
|
638 | if (subject === Math) {
|
639 | return 'math';
|
640 | } else if (subject === null) {
|
641 | return 'null';
|
642 | } else if (Array.isArray(subject)) {
|
643 | return 'array';
|
644 | } else if (Object.prototype.toString.call(subject) === '[object Date]') {
|
645 | return 'date';
|
646 | } else if (typeof subject.toString !== 'undefined' && /^\/.*\//.test(subject.toString())) {
|
647 | return 'regexp';
|
648 | }
|
649 | return 'object';
|
650 | }
|
651 |
|
652 | function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
|
653 | path = path || [];
|
654 | var currentPath = path.slice(0);
|
655 | if (typeof key !== 'undefined') {
|
656 | if (prefilter) {
|
657 | if (typeof(prefilter) === 'function' && prefilter(currentPath, key)) { return; }
|
658 | else if (typeof(prefilter) === 'object') {
|
659 | if (prefilter.prefilter && prefilter.prefilter(currentPath, key)) { return; }
|
660 | if (prefilter.normalize) {
|
661 | var alt = prefilter.normalize(currentPath, key, lhs, rhs);
|
662 | if (alt) {
|
663 | lhs = alt[0];
|
664 | rhs = alt[1];
|
665 | }
|
666 | }
|
667 | }
|
668 | }
|
669 | currentPath.push(key);
|
670 | }
|
671 |
|
672 |
|
673 | if (realTypeOf(lhs) === 'regexp' && realTypeOf(rhs) === 'regexp') {
|
674 | lhs = lhs.toString();
|
675 | rhs = rhs.toString();
|
676 | }
|
677 |
|
678 | var ltype = typeof lhs;
|
679 | var rtype = typeof rhs;
|
680 | if (ltype === 'undefined') {
|
681 | if (rtype !== 'undefined') {
|
682 | changes(new DiffNew(currentPath, rhs));
|
683 | }
|
684 | } else if (rtype === 'undefined') {
|
685 | changes(new DiffDeleted(currentPath, lhs));
|
686 | } else if (realTypeOf(lhs) !== realTypeOf(rhs)) {
|
687 | changes(new DiffEdit(currentPath, lhs, rhs));
|
688 | } else if (Object.prototype.toString.call(lhs) === '[object Date]' && Object.prototype.toString.call(rhs) === '[object Date]' && ((lhs - rhs) !== 0)) {
|
689 | changes(new DiffEdit(currentPath, lhs, rhs));
|
690 | } else if (ltype === 'object' && lhs !== null && rhs !== null) {
|
691 | stack = stack || [];
|
692 | if (stack.indexOf(lhs) < 0) {
|
693 | stack.push(lhs);
|
694 | if (Array.isArray(lhs)) {
|
695 | var i, len = lhs.length;
|
696 | for (i = 0; i < lhs.length; i++) {
|
697 | if (i >= rhs.length) {
|
698 | changes(new DiffArray(currentPath, i, new DiffDeleted(undefined, lhs[i])));
|
699 | } else {
|
700 | deepDiff(lhs[i], rhs[i], changes, prefilter, currentPath, i, stack);
|
701 | }
|
702 | }
|
703 | while (i < rhs.length) {
|
704 | changes(new DiffArray(currentPath, i, new DiffNew(undefined, rhs[i++])));
|
705 | }
|
706 | } else {
|
707 | var akeys = Object.keys(lhs);
|
708 | var pkeys = Object.keys(rhs);
|
709 | akeys.forEach(function(k, i) {
|
710 | var other = pkeys.indexOf(k);
|
711 | if (other >= 0) {
|
712 | deepDiff(lhs[k], rhs[k], changes, prefilter, currentPath, k, stack);
|
713 | pkeys = arrayRemove(pkeys, other);
|
714 | } else {
|
715 | deepDiff(lhs[k], undefined, changes, prefilter, currentPath, k, stack);
|
716 | }
|
717 | });
|
718 | pkeys.forEach(function(k) {
|
719 | deepDiff(undefined, rhs[k], changes, prefilter, currentPath, k, stack);
|
720 | });
|
721 | }
|
722 | stack.length = stack.length - 1;
|
723 | }
|
724 | } else if (lhs !== rhs) {
|
725 | if (!(ltype === 'number' && isNaN(lhs) && isNaN(rhs))) {
|
726 | changes(new DiffEdit(currentPath, lhs, rhs));
|
727 | }
|
728 | }
|
729 | }
|
730 |
|
731 | function accumulateDiff(lhs, rhs, prefilter, accum) {
|
732 | accum = accum || [];
|
733 | deepDiff(lhs, rhs,
|
734 | function(diff) {
|
735 | if (diff) {
|
736 | accum.push(diff);
|
737 | }
|
738 | },
|
739 | prefilter);
|
740 | return (accum.length) ? accum : undefined;
|
741 | }
|
742 |
|
743 | function applyArrayChange(arr, index, change) {
|
744 | if (change.path && change.path.length) {
|
745 | var it = arr[index],
|
746 | i, u = change.path.length - 1;
|
747 | for (i = 0; i < u; i++) {
|
748 | it = it[change.path[i]];
|
749 | }
|
750 | switch (change.kind) {
|
751 | case 'A':
|
752 | applyArrayChange(it[change.path[i]], change.index, change.item);
|
753 | break;
|
754 | case 'D':
|
755 | delete it[change.path[i]];
|
756 | break;
|
757 | case 'E':
|
758 | case 'N':
|
759 | it[change.path[i]] = change.rhs;
|
760 | break;
|
761 | }
|
762 | } else {
|
763 | switch (change.kind) {
|
764 | case 'A':
|
765 | applyArrayChange(arr[index], change.index, change.item);
|
766 | break;
|
767 | case 'D':
|
768 | arr = arrayRemove(arr, index);
|
769 | break;
|
770 | case 'E':
|
771 | case 'N':
|
772 | arr[index] = change.rhs;
|
773 | break;
|
774 | }
|
775 | }
|
776 | return arr;
|
777 | }
|
778 |
|
779 | function applyChange(target, source, change) {
|
780 | if (target && source && change && change.kind) {
|
781 | var it = target,
|
782 | i = -1,
|
783 | last = change.path ? change.path.length - 1 : 0;
|
784 | while (++i < last) {
|
785 | if (typeof it[change.path[i]] === 'undefined') {
|
786 | it[change.path[i]] = (typeof change.path[i] === 'number') ? [] : {};
|
787 | }
|
788 | it = it[change.path[i]];
|
789 | }
|
790 | switch (change.kind) {
|
791 | case 'A':
|
792 | applyArrayChange(change.path ? it[change.path[i]] : it, change.index, change.item);
|
793 | break;
|
794 | case 'D':
|
795 | delete it[change.path[i]];
|
796 | break;
|
797 | case 'E':
|
798 | case 'N':
|
799 | it[change.path[i]] = change.rhs;
|
800 | break;
|
801 | }
|
802 | }
|
803 | }
|
804 |
|
805 | function revertArrayChange(arr, index, change) {
|
806 | if (change.path && change.path.length) {
|
807 |
|
808 | var it = arr[index],
|
809 | i, u = change.path.length - 1;
|
810 | for (i = 0; i < u; i++) {
|
811 | it = it[change.path[i]];
|
812 | }
|
813 | switch (change.kind) {
|
814 | case 'A':
|
815 | revertArrayChange(it[change.path[i]], change.index, change.item);
|
816 | break;
|
817 | case 'D':
|
818 | it[change.path[i]] = change.lhs;
|
819 | break;
|
820 | case 'E':
|
821 | it[change.path[i]] = change.lhs;
|
822 | break;
|
823 | case 'N':
|
824 | delete it[change.path[i]];
|
825 | break;
|
826 | }
|
827 | } else {
|
828 |
|
829 | switch (change.kind) {
|
830 | case 'A':
|
831 | revertArrayChange(arr[index], change.index, change.item);
|
832 | break;
|
833 | case 'D':
|
834 | arr[index] = change.lhs;
|
835 | break;
|
836 | case 'E':
|
837 | arr[index] = change.lhs;
|
838 | break;
|
839 | case 'N':
|
840 | arr = arrayRemove(arr, index);
|
841 | break;
|
842 | }
|
843 | }
|
844 | return arr;
|
845 | }
|
846 |
|
847 | function revertChange(target, source, change) {
|
848 | if (target && source && change && change.kind) {
|
849 | var it = target,
|
850 | i, u;
|
851 | u = change.path.length - 1;
|
852 | for (i = 0; i < u; i++) {
|
853 | if (typeof it[change.path[i]] === 'undefined') {
|
854 | it[change.path[i]] = {};
|
855 | }
|
856 | it = it[change.path[i]];
|
857 | }
|
858 | switch (change.kind) {
|
859 | case 'A':
|
860 |
|
861 |
|
862 | revertArrayChange(it[change.path[i]], change.index, change.item);
|
863 | break;
|
864 | case 'D':
|
865 |
|
866 | it[change.path[i]] = change.lhs;
|
867 | break;
|
868 | case 'E':
|
869 |
|
870 | it[change.path[i]] = change.lhs;
|
871 | break;
|
872 | case 'N':
|
873 |
|
874 | delete it[change.path[i]];
|
875 | break;
|
876 | }
|
877 | }
|
878 | }
|
879 |
|
880 | function applyDiff(target, source, filter) {
|
881 | if (target && source) {
|
882 | var onChange = function(change) {
|
883 | if (!filter || filter(target, source, change)) {
|
884 | applyChange(target, source, change);
|
885 | }
|
886 | };
|
887 | deepDiff(target, source, onChange);
|
888 | }
|
889 | }
|
890 |
|
891 | Object.defineProperties(accumulateDiff, {
|
892 |
|
893 | diff: {
|
894 | value: accumulateDiff,
|
895 | enumerable: true
|
896 | },
|
897 | observableDiff: {
|
898 | value: deepDiff,
|
899 | enumerable: true
|
900 | },
|
901 | applyDiff: {
|
902 | value: applyDiff,
|
903 | enumerable: true
|
904 | },
|
905 | applyChange: {
|
906 | value: applyChange,
|
907 | enumerable: true
|
908 | },
|
909 | revertChange: {
|
910 | value: revertChange,
|
911 | enumerable: true
|
912 | },
|
913 | isConflict: {
|
914 | value: function() {
|
915 | return 'undefined' !== typeof conflict;
|
916 | },
|
917 | enumerable: true
|
918 | },
|
919 | noConflict: {
|
920 | value: function() {
|
921 | if (conflictResolution) {
|
922 | conflictResolution.forEach(function(it) {
|
923 | it();
|
924 | });
|
925 | conflictResolution = null;
|
926 | }
|
927 | return accumulateDiff;
|
928 | },
|
929 | enumerable: true
|
930 | }
|
931 | });
|
932 |
|
933 | return accumulateDiff;
|
934 | }));
|
935 |
|
936 | }.call(exports, (function() { return this; }())))
|
937 |
|
938 | }
|
939 | ])
|
940 | });
|
941 | ; |
\ | No newline at end of file |