UNPKG

lr-core

Version:
45 lines (37 loc) 1.12 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _sortedIndexBy = require('lodash/sortedIndexBy.js'); var _sortedIndexBy2 = _interopRequireDefault(_sortedIndexBy); var _subclassable = require('../subclassable'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class OrderedObjectArray extends _subclassable.SubclassableArray { constructor(key, descending = false) { super(); this._key = key; this._getID = descending ? obj => -obj[this._key] : obj => obj[this._key]; this._set = new Set(); } getIndexOf(obj) { return (0, _sortedIndexBy2.default)(this.toArray(), obj, this._getID); } has(obj) { return this._set.has(obj[this._key]); } add(obj) { if (!this.has(obj)) { this._set.add(obj[this._key]); let index = this.getIndexOf(obj); this.splice(index, 0, obj); } } remove(obj) { if (this.has(obj)) { this._set.delete(obj[this._key]); let index = this.getIndexOf(obj); this.splice(index, 1); } } } exports.default = OrderedObjectArray;