@leofavre/memory-game-component
Version:
A memory game web component.
76 lines (61 loc) • 3.25 kB
JavaScript
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import { INITIAL_STATE, DISTRIBUTE_CARDS, REVEAL_CARD, MATCH_CARDS, HIDE_CARDS, ALLOW_INTERACTION, DISALLOW_INTERACTION, SEND_EVENT } from './memoryGameConstants.js';
export default (function () {
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : INITIAL_STATE;
var action = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
switch (action.type) {
case DISTRIBUTE_CARDS:
var cards = action.cards,
positions = action.positions;
var nextCards = cards.map(function (card, index) {
return _objectSpread({}, card, {
positions: positions.slice(index * 2, index * 2 + 2)
});
});
return _objectSpread({}, state, {
cards: nextCards
});
case REVEAL_CARD:
return _objectSpread({}, state, {
revealed: _toConsumableArray(state.revealed).concat([action.position])
});
case MATCH_CARDS:
var nextMatched = _toConsumableArray(state.matched).concat(_toConsumableArray(state.cards.reduce(function (result, card) {
return _toConsumableArray(result).concat(_toConsumableArray(card.positions.every(function (position) {
return state.revealed.includes(position);
}) ? card.positions : []));
}, [])));
return _objectSpread({}, state, {
matched: nextMatched
});
case HIDE_CARDS:
return _objectSpread({}, state, {
revealed: []
});
case ALLOW_INTERACTION:
return _objectSpread({}, state, {
isInteractive: true
});
case DISALLOW_INTERACTION:
return _objectSpread({}, state, {
isInteractive: false
});
case SEND_EVENT:
var event = action.detail != null ? {
name: action.name,
detail: action.detail
} : {
name: action.name
};
return _objectSpread({}, state, {
events: _toConsumableArray(state.events).concat([event])
});
default:
return state;
}
});