UNPKG

flow-immutable-models

Version:

Generates model classes from Flow types using Immutable.js

128 lines (116 loc) 4.31 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _immutable = require('immutable'); var Immutable = _interopRequireWildcard(_immutable); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var ImmutableModel = function () { function ImmutableModel(state) { _classCallCheck(this, ImmutableModel); this._state = state; } _createClass(ImmutableModel, [{ key: 'getState', value: function getState() { return this._state; } }, { key: 'clone', value: function clone(value) { var constructor = this.constructor; return value === this._state ? this : new constructor(value); } }, { key: 'get', value: function get(property) { return this._state.get(property); } }, { key: 'set', value: function set(property, value) { return this.clone(this._state.set(property, value)); } }, { key: 'update', value: function update(property, updater) { return this.clone(this._state.update(property, updater)); } }, { key: 'getIn', value: function getIn(properties) { return this._state.getIn(properties); } }, { key: 'setIn', value: function setIn(properties, value) { return this.clone(this._state.setIn(properties, value)); } }, { key: 'updateIn', value: function updateIn(properties, notSetValue, updater) { return this.clone(this._state.updateIn(properties, notSetValue, updater)); } }, { key: 'has', value: function has(property) { return this._state.has(property); } }, { key: 'equals', value: function equals(other) { return this._state.equals(other); } }, { key: 'addToMap', value: function addToMap(property, key, value) { var map = this.get(property); return this.clone(this._state.set(property, map.set(key, value))); } }, { key: 'removeFromMap', value: function removeFromMap(property, key) { var map = this.get(property); return this.clone(this._state.set(property, map.remove(key))); } }, { key: 'addToList', value: function addToList(property, value) { return this.clone(this._state.update(property, Immutable.List(), function (lst) { return lst.push(value); })); } }, { key: 'concatToList', value: function concatToList(property) { for (var _len = arguments.length, value = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { value[_key - 1] = arguments[_key]; } return this.clone(this._state.update(property, Immutable.List(), function (lst) { return lst.concat.apply(lst, value); })); } }, { key: 'removeFromList', value: function removeFromList(property, index) { var list = this.get(property); return this.clone(this._state.set(property, list.remove(index))); } }, { key: 'addToSet', value: function addToSet(property, value) { var collection = this.get(property); return this.clone(this._state.set(property, collection.add(value))); } }, { key: 'removeFromSet', value: function removeFromSet(property, value) { var list = this.get(property); return this.clone(this._state.set(property, list.remove(value))); } }]); return ImmutableModel; }(); exports.default = ImmutableModel;