UNPKG

meetup-web-mocks

Version:
140 lines (99 loc) 3.83 kB
'use strict'; exports.__esModule = true; 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; }; var _warning = require('warning'); var _warning2 = _interopRequireDefault(_warning); var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(_invariant); var _LocationUtils = require('./LocationUtils'); var _PathUtils = require('./PathUtils'); var _createHistory = require('./createHistory'); var _createHistory2 = _interopRequireDefault(_createHistory); var _Actions = require('./Actions'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var createStateStorage = function createStateStorage(entries) { return entries.filter(function (entry) { return entry.state; }).reduce(function (memo, entry) { memo[entry.key] = entry.state; return memo; }, {}); }; var createMemoryHistory = function createMemoryHistory() { var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; if (Array.isArray(options)) { options = { entries: options }; } else if (typeof options === 'string') { options = { entries: [options] }; } var getCurrentLocation = function getCurrentLocation() { var entry = entries[current]; var path = (0, _PathUtils.createPath)(entry); var key = void 0, state = void 0; if (entry.key) { key = entry.key; state = readState(key); } var init = (0, _PathUtils.parsePath)(path); return (0, _LocationUtils.createLocation)(_extends({}, init, { state: state }), undefined, key); }; var canGo = function canGo(n) { var index = current + n; return index >= 0 && index < entries.length; }; var go = function go(n) { if (!n) return; if (!canGo(n)) { process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(false, 'Cannot go(%s) there is not enough history', n) : void 0; return; } current += n; var currentLocation = getCurrentLocation(); // Change action to POP history.transitionTo(_extends({}, currentLocation, { action: _Actions.POP })); }; var pushLocation = function pushLocation(location) { current += 1; if (current < entries.length) entries.splice(current); entries.push(location); saveState(location.key, location.state); }; var replaceLocation = function replaceLocation(location) { entries[current] = location; saveState(location.key, location.state); }; var history = (0, _createHistory2.default)(_extends({}, options, { getCurrentLocation: getCurrentLocation, pushLocation: pushLocation, replaceLocation: replaceLocation, go: go })); var _options = options; var entries = _options.entries; var current = _options.current; if (typeof entries === 'string') { entries = [entries]; } else if (!Array.isArray(entries)) { entries = ['/']; } entries = entries.map(function (entry) { return (0, _LocationUtils.createLocation)(entry); }); if (current == null) { current = entries.length - 1; } else { !(current >= 0 && current < entries.length) ? process.env.NODE_ENV !== 'production' ? (0, _invariant2.default)(false, 'Current index must be >= 0 and < %s, was %s', entries.length, current) : (0, _invariant2.default)(false) : void 0; } var storage = createStateStorage(entries); var saveState = function saveState(key, state) { return storage[key] = state; }; var readState = function readState(key) { return storage[key]; }; return _extends({}, history, { canGo: canGo }); }; exports.default = createMemoryHistory;