UNPKG

fluorine-orchestra

Version:
166 lines (116 loc) 4.61 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Collection = undefined; var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _symbol = require('babel-runtime/core-js/symbol'); var _symbol2 = _interopRequireDefault(_symbol); exports.default = createCollection; var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(_invariant); var _extendableImmutable = require('extendable-immutable'); var _immutable = require('immutable'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var groupsSymbol = (0, _symbol2.default)('groups'); function _ref(groupId, id) { (0, _invariant2.default)(typeof groupId === 'string', 'Collection: `groupId` is expected to be an id (string).'); (0, _invariant2.default)(typeof id === 'string', 'Collection: `id` is expected to be an id (string).'); var groups = this[groupsSymbol]; var _group = groups[groupId] || new _immutable.OrderedSet(); groups[groupId] = _group.add(id); return this; } function _ref5(x) { return typeof x === 'string'; } function _ref2(groupId, ids) { (0, _invariant2.default)(typeof groupId === 'string', 'Collection: `groupId` is expected to be an id (string).'); (0, _invariant2.default)(_immutable.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 _immutable.OrderedSet(); groups[groupId] = _group.union(ids); return this; } function _ref3(groupId) { var _this2 = this; (0, _invariant2.default)(typeof groupId === 'string', 'Collection: `groupId` is expected to be an id (string).'); var groups = this[groupsSymbol] || {}; var _group = groups[groupId] || new _immutable.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 {', '}'); } var Collection = exports.Collection = function (_OrderedMap) { (0, _inherits3.default)(Collection, _OrderedMap); function Collection() { (0, _classCallCheck3.default)(this, Collection); return (0, _possibleConstructorReturn3.default)(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] = (0, _extends3.default)({}, 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 _immutable.OrderedSet(); var _group = groups[groupId] || new _immutable.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; }(_extendableImmutable.OrderedMap); function createCollection(obj) { return new Collection(obj); }