/
flux-angular.js
2304 lines (1954 loc) · 63.1 KB
/
flux-angular.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.flux=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function (global){
'use strict';
// When requiring Angular it is added to global for some reason
var angular = global.angular || require('angular') && global.angular;
var ImmutableStore = require('immutable-store');
// Dependencies
var safeDeepClone = require('./safeDeepClone.js');
var Dispatchr = require('dispatchr')();
var EventEmitter2 = require('eventemitter2').EventEmitter2;
var angularModule = angular.module;
var stores = [];
// A function that creates stores
var createStore = function (name, spec, maxListeners, flux) {
spec = spec || {};
// Constructor of a yahoo dispatchr store
var Store = function (dispatcher) {
this.dispatcher = dispatcher;
// Check if store exists when waiting for it
this.waitFor = function (stores, cb) {
stores = Array.isArray(stores) ? stores : [stores];
if (!flux.isStoresRegistered(stores)) {
throw new Error('Waiting for stores that are not injected into Angular yet, ' + stores.join(', ') + '. Be sure to inject stores before waiting for them');
}
this.dispatcher.waitFor(stores, cb.bind(this));
};
// Call the constructor of EventEmitter2
EventEmitter2.call(this, {
wildcard: true
});
if (typeof maxListeners === 'number') {
this.setMaxListeners(maxListeners);
} else if (maxListeners && typeof maxListeners[name] === 'number') {
this.setMaxListeners(maxListeners[name]);
}
if (this.initialize) {
this.initialize();
}
};
// Add constructor properties, as required by Yahoo Dispatchr
Store.handlers = spec.handlers;
Store.storeName = name;
// Inherits from EventEmitter2
Store.prototype = Object.create(EventEmitter2.prototype);
// Create conveniance for emitting change events
Store.prototype.emitChange = function () {
this.emit('change');
};
// Attach store definition to the prototype
Object.keys(spec).forEach(function (key) {
Store.prototype[key] = spec[key];
});
return Store;
};
// Flux Service is a wrapper for the Yahoo Dispatchr
var FluxService = function (useCloning, maxListeners) {
this.stores = [];
this.dispatcher = new Dispatchr();
this.dispatch = function () {
if (stores.length) {
console.warn("There are still stores not injected: " + stores.join(",") + ". Make sure to inject all stores before running any dispatches.");
}
this.dispatcher.dispatch.apply(this.dispatcher, arguments);
};
this.createStore = function (name, spec) {
var store = createStore(name, spec, maxListeners, this);
var storeInstance;
// Create the exports object
store.exports = {};
Dispatchr.registerStore(store);
this.stores.push(store);
// Add cloning to exports
if (!spec.exports) {
throw new Error('You have to add an exports object to your store: ' + name);
}
storeInstance = this.dispatcher.getStore(store);
Object.keys(spec.exports).forEach(function (key) {
// Create a getter
var descriptor = Object.getOwnPropertyDescriptor(spec.exports, key);
if (descriptor.get) {
Object.defineProperty(store.exports, key, {
enumerable: descriptor.enumerable,
configurable: descriptor.configurable,
get: function () {
var value = descriptor.get.apply(storeInstance, arguments);
return useCloning ? safeDeepClone('[Circular]', [], value) : value;
}
});
} else {
store.exports[key] = function () {
var value = spec.exports[key].apply(storeInstance, arguments);
return useCloning ? safeDeepClone('[Circular]', [], value) : value;
};
spec.exports[key] = spec.exports[key].bind(storeInstance);
}
});
return store.exports;
};
this.getStore = function (storeExport) {
var store = this.stores.filter(function (store) {
return store.exports === storeExport;
})[0];
return this.dispatcher.getStore(store);
};
this.isStoresRegistered = function (stores) {
var exists = true;
var storeNames = this.stores.map(function (store) {
return store.storeName;
});
stores.forEach(function (storeName) {
if (storeNames.indexOf(storeName) === -1) {
exists = false;
}
});
return exists;
};
this.reset = function () {
Dispatchr.stores = {};
Dispatchr.handlers = {};
this.stores = [];
};
this.immutable = function (state) {
return new ImmutableStore(state);
};
};
// Monkeypatch angular module (add .store)
// Wrap "angular.module" to attach store method to module instance
angular.module = function () {
// Call the module as normaly and grab the instance
var moduleInstance = angularModule.apply(angular, arguments);
// Attach store method to instance
moduleInstance.store = function (storeName, storeDefinition) {
// Add to stores array
stores.push(storeName);
// Create a new store
this.factory(storeName, ['$injector', 'flux', function ($injector, flux) {
var storeConfig = $injector.invoke(storeDefinition);
stores.splice(stores.indexOf(storeName), 1);
return flux.createStore(storeName, storeConfig);
}]);
return this;
};
return moduleInstance;
};
angular.module('flux', [])
.provider('flux', function FluxProvider () {
var cloning = true;
var maxListeners = null;
this.useCloning = function (useCloning) {
cloning = useCloning;
};
this.setMaxListeners = function (maxListenersDescription) {
maxListeners = maxListenersDescription;
};
this.$get = [function fluxFactory () {
return new FluxService(cloning, maxListeners);
}];
})
.run(['$rootScope', '$injector', 'flux', function ($rootScope, $injector, flux) {
if (angular.mock) {
flux.reset();
}
// Extend scopes with $listenTo
$rootScope.constructor.prototype.$listenTo = function (storeExport, eventName, callback) {
if (!callback) {
callback = eventName;
eventName = '*';
}
callback = callback.bind(this);
var store = flux.getStore(storeExport);
var addMethod = eventName === '*' ? 'onAny' : 'on';
var removeMethod = eventName === '*' ? 'offAny' : 'off';
var args = eventName === '*' ? [callback] : [eventName, callback];
store[addMethod].apply(store, args);
// Remove any listeners to the store when scope is destroyed (GC)
this.$on('$destroy', function () {
store[removeMethod].apply(store, args);
});
};
}]);
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"./safeDeepClone.js":14,"angular":"angular","dispatchr":2,"eventemitter2":8,"immutable-store":10}],2:[function(require,module,exports){
/**
* Copyright 2014, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
module.exports = require('./lib/Dispatcher');
},{"./lib/Dispatcher":4}],3:[function(require,module,exports){
/**
* Copyright 2014, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';
var debug = require('debug')('Dispatchr:Action');
function Action(name, payload) {
this.name = name;
this.payload = payload;
this._handlers = null;
this._isExecuting = false;
this._isCompleted = null;
}
/**
* Gets a name from a store
* @method getStoreName
* @param {String|Object} store The store name or class from which to extract
* the name
* @returns {String}
*/
Action.prototype.getStoreName = function getStoreName(store) {
if ('string' === typeof store) {
return store;
}
return store.storeName;
};
/**
* Executes all handlers for the action
* @method execute
* @param {Function[]} handlers A mapping of store names to handler function
* @throws {Error} if action has already been executed
*/
Action.prototype.execute = function execute(handlers) {
if (this._isExecuting) {
throw new Error('Action is already dispatched');
}
var self = this;
this._handlers = handlers;
this._isExecuting = true;
this._isCompleted = {};
Object.keys(handlers).forEach(function handlersEach(storeName) {
self._callHandler(storeName);
});
};
/**
* Calls an individual store's handler function
* @method _callHandler
* @param {String} storeName
* @private
* @throws {Error} if handler does not exist for storeName
*/
Action.prototype._callHandler = function callHandler(storeName) {
var self = this,
handlerFn = self._handlers[storeName];
if (!handlerFn) {
throw new Error(storeName + ' does not have a handler for action ' + self.name);
}
if (self._isCompleted[storeName]) {
return;
}
self._isCompleted[storeName] = false;
debug('executing handler for ' + storeName);
handlerFn(self.payload, self.name);
self._isCompleted[storeName] = true;
};
/**
* Waits until all stores have finished handling an action and then calls
* the callback
* @method waitFor
* @param {String|String[]|Constructor|Constructor[]} stores An array of stores as strings or constructors to wait for
* @param {Function} callback Called after all stores have completed handling their actions
* @throws {Error} if the action is not being executed
*/
Action.prototype.waitFor = function waitFor(stores, callback) {
var self = this;
if (!self._isExecuting) {
throw new Error('waitFor called even though there is no action being executed!');
}
if (!Array.isArray(stores)) {
stores = [stores];
}
debug('waiting on ' + stores.join(', '));
stores.forEach(function storesEach(storeName) {
storeName = self.getStoreName(storeName);
if (self._handlers[storeName]) {
self._callHandler(storeName);
}
});
callback();
};
module.exports = Action;
},{"debug":5}],4:[function(require,module,exports){
/**
* Copyright 2014, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';
var Action = require('./Action'),
DEFAULT = 'default';
module.exports = function () {
var debug = require('debug')('Dispatchr:dispatcher');
/**
* @class Dispatcher
* @param {Object} context The context to be used for store instances
* @constructor
*/
function Dispatcher (context) {
this.storeInstances = {};
this.currentAction = null;
this.dispatcherInterface = {
getContext: function getContext() { return context; },
getStore: this.getStore.bind(this),
waitFor: this.waitFor.bind(this)
};
}
Dispatcher.stores = {};
Dispatcher.handlers = {
'default': []
};
/**
* Registers a store so that it can handle actions.
* @method registerStore
* @static
* @param {Object} store A store class to be registered. The store should have a static
* `name` property so that it can be loaded later.
* @throws {Error} if store is invalid
* @throws {Error} if store is already registered
*/
Dispatcher.registerStore = function registerStore(store) {
if ('function' !== typeof store) {
throw new Error('registerStore requires a constructor as first parameter');
}
var storeName = Dispatcher.getStoreName(store);
if (!storeName) {
throw new Error('Store is required to have a `storeName` property.');
}
if (Dispatcher.stores[storeName]) {
if (Dispatcher.stores[storeName] === store) {
// Store is already registered, nothing to do
return;
}
throw new Error('Store with name `' + storeName + '` has already been registered.');
}
Dispatcher.stores[storeName] = store;
if (store.handlers) {
Object.keys(store.handlers).forEach(function storeHandlersEach(action) {
var handler = store.handlers[action];
Dispatcher._registerHandler(action, storeName, handler);
});
}
};
/**
* Method to discover if a storeName has been registered
* @method isRegistered
* @static
* @param {Object|String} store The store to check
* @returns {boolean}
*/
Dispatcher.isRegistered = function isRegistered(store) {
var storeName = Dispatcher.getStoreName(store),
storeInstance = Dispatcher.stores[storeName];
if (!storeInstance) {
return false;
}
if ('function' === typeof store) {
if (store !== storeInstance) {
return false;
}
}
return true;
};
/**
* Gets a name from a store
* @method getStoreName
* @static
* @param {String|Object} store The store name or class from which to extract
* the name
* @returns {String}
*/
Dispatcher.getStoreName = function getStoreName(store) {
if ('string' === typeof store) {
return store;
}
return store.storeName;
};
/**
* Adds a handler function to be called for the given action
* @method registerHandler
* @private
* @static
* @param {String} action Name of the action
* @param {String} name Name of the store that handles the action
* @param {String|Function} handler The function or name of the method that handles the action
* @returns {number}
*/
Dispatcher._registerHandler = function registerHandler(action, name, handler) {
Dispatcher.handlers[action] = Dispatcher.handlers[action] || [];
Dispatcher.handlers[action].push({
name: Dispatcher.getStoreName(name),
handler: handler
});
return Dispatcher.handlers.length - 1;
};
/**
* Returns a single store instance and creates one if it doesn't already exist
* @method getStore
* @param {String} name The name of the instance
* @returns {Object} The store instance
* @throws {Error} if store is not registered
*/
Dispatcher.prototype.getStore = function getStore(name) {
var storeName = Dispatcher.getStoreName(name);
if (!this.storeInstances[storeName]) {
var Store = Dispatcher.stores[storeName];
if (!Store) {
throw new Error('Store ' + storeName + ' was not registered.');
}
this.storeInstances[storeName] = new (Dispatcher.stores[storeName])(this.dispatcherInterface);
}
return this.storeInstances[storeName];
};
/**
* Dispatches a new action or queues it up if one is already in progress
* @method dispatch
* @param {String} actionName Name of the action to be dispatched
* @param {Object} payload Parameters to describe the action
* @throws {Error} if store has handler registered that does not exist
*/
Dispatcher.prototype.dispatch = function dispatch(actionName, payload) {
if (!actionName) {
throw new Error('actionName parameter `' + actionName + '` is invalid.');
}
if (this.currentAction) {
throw new Error('Cannot call dispatch while another dispatch is executing. Attempted to execute \'' + actionName + '\' but \'' + this.currentAction.name + '\' is already executing.');
}
var actionHandlers = Dispatcher.handlers[actionName] || [],
defaultHandlers = Dispatcher.handlers[DEFAULT] || [];
if (!actionHandlers.length && !defaultHandlers.length) {
debug(actionName + ' does not have any registered handlers');
return;
}
debug('dispatching ' + actionName, payload);
this.currentAction = new Action(actionName, payload);
var self = this,
allHandlers = actionHandlers.concat(defaultHandlers),
handlerFns = {};
try {
allHandlers.forEach(function actionHandlersEach(store) {
if (handlerFns[store.name]) {
// Don't call the default if the store has an explicit action handler
return;
}
var storeInstance = self.getStore(store.name);
if ('function' === typeof store.handler) {
handlerFns[store.name] = store.handler.bind(storeInstance);
} else {
if (!storeInstance[store.handler]) {
throw new Error(store.name + ' does not have a method called ' + store.handler);
}
handlerFns[store.name] = storeInstance[store.handler].bind(storeInstance);
}
});
this.currentAction.execute(handlerFns);
} catch (e) {
throw e;
} finally {
debug('finished ' + actionName);
this.currentAction = null;
}
};
/**
* Returns a raw data object representation of the current state of the
* dispatcher and all store instances. If the store implements a shouldDehdyrate
* function, then it will be called and only dehydrate if the method returns `true`
* @method dehydrate
* @returns {Object} dehydrated dispatcher data
*/
Dispatcher.prototype.dehydrate = function dehydrate() {
var self = this,
stores = {};
Object.keys(self.storeInstances).forEach(function storeInstancesEach(storeName) {
var store = self.storeInstances[storeName];
if (!store.dehydrate || (store.shouldDehydrate && !store.shouldDehydrate())) {
return;
}
stores[storeName] = store.dehydrate();
});
return {
stores: stores
};
};
/**
* Takes a raw data object and rehydrates the dispatcher and store instances
* @method rehydrate
* @param {Object} dispatcherState raw state typically retrieved from `dehydrate`
* method
*/
Dispatcher.prototype.rehydrate = function rehydrate(dispatcherState) {
var self = this;
if (dispatcherState.stores) {
Object.keys(dispatcherState.stores).forEach(function storeStateEach(storeName) {
var state = dispatcherState.stores[storeName],
store = self.getStore(storeName);
if (store.rehydrate) {
store.rehydrate(state);
}
});
}
};
/**
* Waits until all stores have finished handling an action and then calls
* the callback
* @method waitFor
* @param {String|String[]} stores An array of stores as strings to wait for
* @param {Function} callback Called after all stores have completed handling their actions
* @throws {Error} if there is no action dispatching
*/
Dispatcher.prototype.waitFor = function waitFor(stores, callback) {
if (!this.currentAction) {
throw new Error('waitFor called even though there is no action dispatching');
}
this.currentAction.waitFor(stores, callback);
};
return Dispatcher;
};
},{"./Action":3,"debug":5}],5:[function(require,module,exports){
/**
* This is the web browser implementation of `debug()`.
*
* Expose `debug()` as the module.
*/
exports = module.exports = require('./debug');
exports.log = log;
exports.formatArgs = formatArgs;
exports.save = save;
exports.load = load;
exports.useColors = useColors;
exports.storage = 'undefined' != typeof chrome
&& 'undefined' != typeof chrome.storage
? chrome.storage.local
: localstorage();
/**
* Colors.
*/
exports.colors = [
'lightseagreen',
'forestgreen',
'goldenrod',
'dodgerblue',
'darkorchid',
'crimson'
];
/**
* Currently only WebKit-based Web Inspectors, Firefox >= v31,
* and the Firebug extension (any Firefox version) are known
* to support "%c" CSS customizations.
*
* TODO: add a `localStorage` variable to explicitly enable/disable colors
*/
function useColors() {
// is webkit? http://stackoverflow.com/a/16459606/376773
return ('WebkitAppearance' in document.documentElement.style) ||
// is firebug? http://stackoverflow.com/a/398120/376773
(window.console && (console.firebug || (console.exception && console.table))) ||
// is firefox >= v31?
// https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
(navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31);
}
/**
* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
*/
exports.formatters.j = function(v) {
return JSON.stringify(v);
};
/**
* Colorize log arguments if enabled.
*
* @api public
*/
function formatArgs() {
var args = arguments;
var useColors = this.useColors;
args[0] = (useColors ? '%c' : '')
+ this.namespace
+ (useColors ? ' %c' : ' ')
+ args[0]
+ (useColors ? '%c ' : ' ')
+ '+' + exports.humanize(this.diff);
if (!useColors) return args;
var c = 'color: ' + this.color;
args = [args[0], c, 'color: inherit'].concat(Array.prototype.slice.call(args, 1));
// the final "%c" is somewhat tricky, because there could be other
// arguments passed either before or after the %c, so we need to
// figure out the correct index to insert the CSS into
var index = 0;
var lastC = 0;
args[0].replace(/%[a-z%]/g, function(match) {
if ('%%' === match) return;
index++;
if ('%c' === match) {
// we only are interested in the *last* %c
// (the user may have provided their own)
lastC = index;
}
});
args.splice(lastC, 0, c);
return args;
}
/**
* Invokes `console.log()` when available.
* No-op when `console.log` is not a "function".
*
* @api public
*/
function log() {
// this hackery is required for IE8/9, where
// the `console.log` function doesn't have 'apply'
return 'object' === typeof console
&& console.log
&& Function.prototype.apply.call(console.log, console, arguments);
}
/**
* Save `namespaces`.
*
* @param {String} namespaces
* @api private
*/
function save(namespaces) {
try {
if (null == namespaces) {
exports.storage.removeItem('debug');
} else {
exports.storage.debug = namespaces;
}
} catch(e) {}
}
/**
* Load `namespaces`.
*
* @return {String} returns the previously persisted debug modes
* @api private
*/
function load() {
var r;
try {
r = exports.storage.debug;
} catch(e) {}
return r;
}
/**
* Enable namespaces listed in `localStorage.debug` initially.
*/
exports.enable(load());
/**
* Localstorage attempts to return the localstorage.
*
* This is necessary because safari throws
* when a user disables cookies/localstorage
* and you attempt to access it.
*
* @return {LocalStorage}
* @api private
*/
function localstorage(){
try {
return window.localStorage;
} catch (e) {}
}
},{"./debug":6}],6:[function(require,module,exports){
/**
* This is the common logic for both the Node.js and web browser
* implementations of `debug()`.
*
* Expose `debug()` as the module.
*/
exports = module.exports = debug;
exports.coerce = coerce;
exports.disable = disable;
exports.enable = enable;
exports.enabled = enabled;
exports.humanize = require('ms');
/**
* The currently active debug mode names, and names to skip.
*/
exports.names = [];
exports.skips = [];
/**
* Map of special "%n" handling functions, for the debug "format" argument.
*
* Valid key names are a single, lowercased letter, i.e. "n".
*/
exports.formatters = {};
/**
* Previously assigned color.
*/
var prevColor = 0;
/**
* Previous log timestamp.
*/
var prevTime;
/**
* Select a color.
*
* @return {Number}
* @api private
*/
function selectColor() {
return exports.colors[prevColor++ % exports.colors.length];
}
/**
* Create a debugger with the given `namespace`.
*
* @param {String} namespace
* @return {Function}
* @api public
*/
function debug(namespace) {
// define the `disabled` version
function disabled() {
}
disabled.enabled = false;
// define the `enabled` version
function enabled() {
var self = enabled;
// set `diff` timestamp
var curr = +new Date();
var ms = curr - (prevTime || curr);
self.diff = ms;
self.prev = prevTime;
self.curr = curr;
prevTime = curr;
// add the `color` if not set
if (null == self.useColors) self.useColors = exports.useColors();
if (null == self.color && self.useColors) self.color = selectColor();
var args = Array.prototype.slice.call(arguments);
args[0] = exports.coerce(args[0]);
if ('string' !== typeof args[0]) {
// anything else let's inspect with %o
args = ['%o'].concat(args);
}
// apply any `formatters` transformations
var index = 0;
args[0] = args[0].replace(/%([a-z%])/g, function(match, format) {
// if we encounter an escaped % then don't increase the array index
if (match === '%%') return match;
index++;
var formatter = exports.formatters[format];
if ('function' === typeof formatter) {
var val = args[index];
match = formatter.call(self, val);
// now we need to remove `args[index]` since it's inlined in the `format`
args.splice(index, 1);
index--;
}
return match;
});
if ('function' === typeof exports.formatArgs) {
args = exports.formatArgs.apply(self, args);
}
var logFn = enabled.log || exports.log || console.log.bind(console);
logFn.apply(self, args);
}
enabled.enabled = true;
var fn = exports.enabled(namespace) ? enabled : disabled;
fn.namespace = namespace;
return fn;
}
/**
* Enables a debug mode by namespaces. This can include modes
* separated by a colon and wildcards.
*
* @param {String} namespaces
* @api public
*/
function enable(namespaces) {
exports.save(namespaces);
var split = (namespaces || '').split(/[\s,]+/);
var len = split.length;
for (var i = 0; i < len; i++) {
if (!split[i]) continue; // ignore empty strings
namespaces = split[i].replace(/\*/g, '.*?');
if (namespaces[0] === '-') {
exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
} else {
exports.names.push(new RegExp('^' + namespaces + '$'));
}
}
}
/**
* Disable debug output.
*
* @api public
*/
function disable() {
exports.enable('');
}
/**
* Returns true if the given mode name is enabled, false otherwise.
*
* @param {String} name
* @return {Boolean}
* @api public
*/
function enabled(name) {
var i, len;
for (i = 0, len = exports.skips.length; i < len; i++) {
if (exports.skips[i].test(name)) {
return false;
}
}
for (i = 0, len = exports.names.length; i < len; i++) {
if (exports.names[i].test(name)) {
return true;
}
}
return false;
}
/**
* Coerce `val`.
*
* @param {Mixed} val
* @return {Mixed}
* @api private
*/
function coerce(val) {
if (val instanceof Error) return val.stack || val.message;
return val;
}
},{"ms":7}],7:[function(require,module,exports){
/**
* Helpers.
*/
var s = 1000;
var m = s * 60;
var h = m * 60;
var d = h * 24;
var y = d * 365.25;
/**
* Parse or format the given `val`.
*
* Options:
*
* - `long` verbose formatting [false]
*
* @param {String|Number} val
* @param {Object} options
* @return {String|Number}
* @api public
*/
module.exports = function(val, options){
options = options || {};
if ('string' == typeof val) return parse(val);
return options.long