UNPKG

fluorine-orchestra

Version:
137 lines (103 loc) 3.73 kB
import _extends from 'babel-runtime/helpers/extends'; import _classCallCheck from 'babel-runtime/helpers/classCallCheck'; import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn'; import _inherits from 'babel-runtime/helpers/inherits'; import _Symbol from 'babel-runtime/core-js/symbol'; import invariant from 'invariant'; import { OrderedMap } from 'extendable-immutable'; import { Set, OrderedSet } from 'immutable'; var groupsSymbol = _Symbol('groups'); function _ref(groupId, id) { invariant(typeof groupId === 'string', 'Collection: `groupId` is expected to be an id (string).'); invariant(typeof id === 'string', 'Collection: `id` is expected to be an id (string).'); var groups = this[groupsSymbol]; var _group = groups[groupId] || new OrderedSet(); groups[groupId] = _group.add(id); return this; } function _ref5(x) { return typeof x === 'string'; } function _ref2(groupId, ids) { invariant(typeof groupId === 'string', 'Collection: `groupId` is expected to be an id (string).'); invariant(Set.isSet(ids) && ids.every(_ref5), 'Collection: `ids` is expected to be a Set containing ids (string).'); var groups = this[groupsSymbol]; var _group = groups[groupId] || new OrderedSet(); groups[groupId] = _group.union(ids); return this; } function _ref3(groupId) { var _this2 = this; invariant(typeof groupId === 'string', 'Collection: `groupId` is expected to be an id (string).'); var groups = this[groupsSymbol] || {}; var _group = groups[groupId] || new OrderedSet(); var empty = this.clear(); if (!_group.size) { return empty; } return empty.withMutations(function (map) { _group.forEach(function (id) { var item = _this2.get(id); if (item) { map.set(id, item); } }); }); } function _ref4() { return this.__toString('Collection {', '}'); } export var Collection = function (_OrderedMap) { _inherits(Collection, _OrderedMap); function Collection() { _classCallCheck(this, Collection); return _possibleConstructorReturn(this, _OrderedMap.apply(this, arguments)); } Collection.of = function of(val) { return new Collection(val); }; Collection.isCollection = function isCollection(val) { return val && val instanceof Collection; }; Collection.prototype.__wrapImmutable = function __wrapImmutable() { var _OrderedMap$prototype; for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var res = (_OrderedMap$prototype = _OrderedMap.prototype.__wrapImmutable).call.apply(_OrderedMap$prototype, [this].concat(args)); if (res.size) { var groups = this[groupsSymbol] || {}; res[groupsSymbol] = _extends({}, groups); } else { res[groupsSymbol] = {}; } return res; }; Collection.prototype.merge = function merge(x) { if (!this.size) { return x; } var res = _OrderedMap.prototype.merge.call(this, x); if (!x instanceof Collection) { return res; } var xGroups = x[groupsSymbol] || {}; var groups = res[groupsSymbol] || {}; for (var groupId in xGroups) { if (xGroups.hasOwnProperty(groupId)) { var _xGroup = xGroups[groupId] || new OrderedSet(); var _group = groups[groupId] || new OrderedSet(); groups[groupId] = _group.union(_xGroup); } } return res; }; Collection.prototype.addIdToGroup = _ref; Collection.prototype.addIdsToGroup = _ref2; Collection.prototype.filterForGroup = _ref3; Collection.prototype.toString = _ref4; return Collection; }(OrderedMap); export default function createCollection(obj) { return new Collection(obj); }