1 | 'use strict';
|
2 |
|
3 | const EventEmitter = require('eventemitter3');
|
4 | const keys = require('lodash.keys');
|
5 | const isObject = require('lodash.isplainobject');
|
6 | const isArray = require('lodash.isarray');
|
7 | const isEqual = require('lodash.isequal');
|
8 | const isString = require('lodash.isstring');
|
9 | const includes = require('lodash.includes');
|
10 | const ObjectGenerator = require('./object-generator');
|
11 | const TypeChecker = require('hadron-type-checker');
|
12 | const uuid = require('uuid');
|
13 |
|
14 | const DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss.SSS';
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | const Events = {
|
20 | 'Added': 'Element::Added',
|
21 | 'Edited': 'Element::Edited',
|
22 | 'Removed': 'Element::Removed',
|
23 | 'Reverted': 'Element::Reverted',
|
24 | 'Converted': 'Element::Converted',
|
25 | 'Invalid': 'Element::Invalid',
|
26 | 'Valid': 'Element::Valid'
|
27 | };
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | const ID = '_id';
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | const UNEDITABLE_TYPES = [
|
38 | 'Binary',
|
39 | 'Code',
|
40 | 'MinKey',
|
41 | 'MaxKey',
|
42 | 'Timestamp',
|
43 | 'BSONRegExp',
|
44 | 'Undefined',
|
45 | 'Null'
|
46 | ];
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | const CURLY = '{';
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | const BRACKET = '[';
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | const ARRAY_OR_OBJECT = /^(\[|\{)(.+)(\]|\})$/;
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | class Element extends EventEmitter {
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 | bulkEdit(value) {
|
73 | if (value.match(ARRAY_OR_OBJECT)) {
|
74 | this.edit(JSON.parse(value));
|
75 | this._bubbleUp(Events.Converted);
|
76 | } else {
|
77 | this.edit(value);
|
78 | }
|
79 | }
|
80 |
|
81 | |
82 |
|
83 |
|
84 | cancel() {
|
85 | if (this.elements) {
|
86 | for (let element of this.elements) {
|
87 | element.cancel();
|
88 | }
|
89 | }
|
90 | if (this.isModified()) {
|
91 | this.revert();
|
92 | }
|
93 | }
|
94 |
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | constructor(key, value, added, parent, previousElement, nextElement) {
|
106 | super();
|
107 | this.uuid = uuid.v4();
|
108 | this.key = key;
|
109 | this.currentKey = key;
|
110 | this.parent = parent;
|
111 | this.previousElement = previousElement;
|
112 | this.nextElement = nextElement;
|
113 | this.added = added;
|
114 | this.removed = false;
|
115 | this.type = TypeChecker.type(value);
|
116 | this.currentType = this.type;
|
117 | this.setValid();
|
118 |
|
119 | if (this._isExpandable(value)) {
|
120 | this.elements = this._generateElements(value);
|
121 | this.originalExpandableValue = value;
|
122 | } else {
|
123 | this.value = value;
|
124 | this.currentValue = value;
|
125 | }
|
126 | }
|
127 |
|
128 | |
129 |
|
130 |
|
131 |
|
132 |
|
133 | edit(value) {
|
134 | this.currentType = TypeChecker.type(value);
|
135 | if (this._isExpandable(value) && !this._isExpandable(this.currentValue)) {
|
136 | this.currentValue = null;
|
137 | this.elements = this._generateElements(value);
|
138 | } else if (!this._isExpandable(value) && this.elements) {
|
139 | this.currentValue = value;
|
140 | this.elements = undefined;
|
141 | } else {
|
142 | this.currentValue = value;
|
143 | }
|
144 | this.setValid();
|
145 | this._bubbleUp(Events.Edited);
|
146 | }
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | get(key) {
|
156 | return this.elements ? this.elements.get(key) : undefined;
|
157 | }
|
158 |
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | at(i) {
|
167 | return this.elements ? this.elements.at(i) : undefined;
|
168 | }
|
169 |
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | next() {
|
178 | if (this.currentValue === CURLY) {
|
179 | return this._convertToEmptyObject();
|
180 | } else if (this.currentValue === BRACKET) {
|
181 | return this._convertToEmptyArray();
|
182 | }
|
183 | return this._next();
|
184 | }
|
185 |
|
186 | |
187 |
|
188 |
|
189 |
|
190 |
|
191 | rename(key) {
|
192 | if (this.parent !== undefined) {
|
193 | const elm = this.parent.elements._map[this.currentKey];
|
194 | delete this.parent.elements._map[this.currentKey];
|
195 | this.parent.elements._map[key] = elm;
|
196 | }
|
197 |
|
198 | this.currentKey = key;
|
199 | this._bubbleUp(Events.Edited);
|
200 | }
|
201 |
|
202 | |
203 |
|
204 |
|
205 |
|
206 |
|
207 | generateObject() {
|
208 | if (this.currentType === 'Array') {
|
209 | return ObjectGenerator.generateArray(this.elements);
|
210 | }
|
211 | if (this.elements) {
|
212 | return ObjectGenerator.generate(this.elements);
|
213 | }
|
214 | return this.currentValue;
|
215 | }
|
216 |
|
217 | |
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | generateOriginalObject() {
|
224 | if (this.type === 'Array') {
|
225 | const originalElements = this._generateElements(this.originalExpandableValue);
|
226 | return ObjectGenerator.generateOriginalArray(originalElements);
|
227 | }
|
228 | if (this.type === 'Object') {
|
229 | const originalElements = this._generateElements(this.originalExpandableValue);
|
230 | return ObjectGenerator.generateOriginal(originalElements);
|
231 | }
|
232 |
|
233 | return this.value;
|
234 | }
|
235 |
|
236 | |
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | insertAfter(element, key, value) {
|
248 | if (this.currentType === 'Array') {
|
249 | if (element.currentKey === '') {
|
250 | this.elements.handleEmptyKeys(element);
|
251 | }
|
252 | key = element.currentKey + 1;
|
253 | }
|
254 | var newElement = this.elements.insertAfter(element, key, value, true, this);
|
255 | if (this.currentType === 'Array') {
|
256 | this.elements.updateKeys(newElement, 1);
|
257 | }
|
258 | this._bubbleUp(Events.Added);
|
259 | return newElement;
|
260 | }
|
261 |
|
262 | |
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 | insertEnd(key, value) {
|
271 | if (this.currentType === 'Array') {
|
272 | this.elements.flush();
|
273 | key = 0;
|
274 | if (this.elements.lastElement) {
|
275 | if (this.elements.lastElement.currentKey === '') {
|
276 | this.elements.handleEmptyKeys(this.elements.lastElement);
|
277 | }
|
278 | key = this.elements.lastElement.currentKey + 1;
|
279 | }
|
280 | }
|
281 | var newElement = this.elements.insertEnd(key, value, true, this);
|
282 | this._bubbleUp(Events.Added);
|
283 | return newElement;
|
284 | }
|
285 |
|
286 | |
287 |
|
288 |
|
289 |
|
290 |
|
291 | insertPlaceholder() {
|
292 | var newElement = this.elements.insertEnd('', '', true, this);
|
293 | this._bubbleUp(Events.Added);
|
294 | return newElement;
|
295 | }
|
296 |
|
297 | |
298 |
|
299 |
|
300 |
|
301 |
|
302 | isAdded() {
|
303 | return this.added || (this.parent && this.parent.isAdded());
|
304 | }
|
305 |
|
306 | |
307 |
|
308 |
|
309 |
|
310 |
|
311 | isBlank() {
|
312 | return this.currentKey === '' && this.currentValue === '';
|
313 | }
|
314 |
|
315 | |
316 |
|
317 |
|
318 |
|
319 |
|
320 | isCurrentTypeValid() {
|
321 | return this.currentTypeValid;
|
322 | }
|
323 |
|
324 | |
325 |
|
326 |
|
327 | setValid() {
|
328 | this.currentTypeValid = true;
|
329 | this.invalidTypeMessage = undefined;
|
330 | this._bubbleUp(Events.Valid, this.uuid);
|
331 | }
|
332 |
|
333 | |
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 | setInvalid(value, newType, message) {
|
341 | this.currentValue = value;
|
342 | this.currentType = newType;
|
343 | this.currentTypeValid = false;
|
344 | this.invalidTypeMessage = message;
|
345 | this._bubbleUp(Events.Invalid, this.uuid);
|
346 | }
|
347 |
|
348 | |
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 | isDuplicateKey(value) {
|
356 | if (value === this.currentKey) {
|
357 | return false;
|
358 | }
|
359 | for (let element of this.parent.elements) {
|
360 | if (element.currentKey === value) {
|
361 | return true;
|
362 | }
|
363 | }
|
364 | return false;
|
365 | }
|
366 |
|
367 | |
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 | isEdited() {
|
375 | return (this.isRenamed() ||
|
376 | !this._valuesEqual() ||
|
377 | this.type !== this.currentType) &&
|
378 | !this.isAdded();
|
379 | }
|
380 |
|
381 | |
382 |
|
383 |
|
384 |
|
385 |
|
386 | _valuesEqual() {
|
387 | if (this.currentType === 'Date' && isString(this.currentValue)) {
|
388 | return isEqual(this.value, new Date(this.currentValue));
|
389 | } else if (this.currentType === 'ObjectId' && isString(this.currentValue)) {
|
390 | return this._isObjectIdEqual();
|
391 | }
|
392 | return isEqual(this.value, this.currentValue);
|
393 | }
|
394 |
|
395 | _isObjectIdEqual() {
|
396 | try {
|
397 | return this.value.toHexString() === this.currentValue;
|
398 | } catch (_) {
|
399 | return false;
|
400 | }
|
401 | }
|
402 |
|
403 | |
404 |
|
405 |
|
406 |
|
407 |
|
408 | isLast() {
|
409 | return this.parent.elements.lastElement === this;
|
410 | }
|
411 |
|
412 | |
413 |
|
414 |
|
415 |
|
416 |
|
417 | isRenamed() {
|
418 | let keyChanged = false;
|
419 | if (!this.parent || this.parent.isRoot() || this.parent.currentType === 'Object') {
|
420 | keyChanged = (this.key !== this.currentKey);
|
421 | }
|
422 |
|
423 | return keyChanged;
|
424 | }
|
425 |
|
426 | |
427 |
|
428 |
|
429 |
|
430 |
|
431 | isRevertable() {
|
432 | return this.isEdited() || this.isRemoved();
|
433 | }
|
434 |
|
435 | |
436 |
|
437 |
|
438 |
|
439 |
|
440 | isRemovable() {
|
441 | return !this.parent.isRemoved();
|
442 | }
|
443 |
|
444 | |
445 |
|
446 |
|
447 |
|
448 |
|
449 | isNotActionable() {
|
450 | return (this.key === ID && !this.isAdded()) || !this.isRemovable();
|
451 | }
|
452 |
|
453 | |
454 |
|
455 |
|
456 |
|
457 |
|
458 | isValueEditable() {
|
459 | return this.isKeyEditable() && !includes(UNEDITABLE_TYPES, this.currentType);
|
460 | }
|
461 |
|
462 | |
463 |
|
464 |
|
465 |
|
466 |
|
467 | isParentEditable() {
|
468 | if (this.parent && !this.parent.isRoot()) {
|
469 | return this.parent.isKeyEditable();
|
470 | }
|
471 | return true;
|
472 | }
|
473 |
|
474 | |
475 |
|
476 |
|
477 |
|
478 |
|
479 | isKeyEditable() {
|
480 | return this.isParentEditable() && (this.isAdded() || (this.currentKey !== ID));
|
481 | }
|
482 |
|
483 | |
484 |
|
485 |
|
486 |
|
487 |
|
488 | isModified() {
|
489 | if (this.elements) {
|
490 | for (let element of this.elements) {
|
491 | if (element.isModified()) {
|
492 | return true;
|
493 | }
|
494 | }
|
495 | }
|
496 | return this.isAdded() || this.isEdited() || this.isRemoved();
|
497 | }
|
498 |
|
499 | |
500 |
|
501 |
|
502 |
|
503 |
|
504 | isRemoved() {
|
505 | return this.removed;
|
506 | }
|
507 |
|
508 | |
509 |
|
510 |
|
511 |
|
512 |
|
513 | isRoot() {
|
514 | return false;
|
515 | }
|
516 |
|
517 | |
518 |
|
519 |
|
520 | remove() {
|
521 | this.revert();
|
522 | this.removed = true;
|
523 | this._bubbleUp(Events.Removed);
|
524 | }
|
525 |
|
526 | |
527 |
|
528 |
|
529 | revert() {
|
530 | if (this.isAdded()) {
|
531 | if (this.parent && this.parent.currentType === 'Array') {
|
532 | this.parent.elements.updateKeys(this, -1);
|
533 | }
|
534 | this.parent.elements.remove(this);
|
535 | this.parent.emit(Events.Removed);
|
536 | this.parent = null;
|
537 | } else {
|
538 | if (this.originalExpandableValue) {
|
539 | this.elements = this._generateElements(this.originalExpandableValue);
|
540 | this.currentValue = undefined;
|
541 | } else {
|
542 | if (this.currentValue === null && this.value !== null) {
|
543 | this.elements = null;
|
544 | } else {
|
545 | this._removeAddedElements();
|
546 | }
|
547 | this.currentValue = this.value;
|
548 | }
|
549 | this.currentKey = this.key;
|
550 | this.currentType = this.type;
|
551 | this.removed = false;
|
552 | }
|
553 | this.setValid();
|
554 | this._bubbleUp(Events.Reverted);
|
555 | }
|
556 |
|
557 | |
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 | _bubbleUp(evt, data) {
|
564 | this.emit(evt, data);
|
565 | var element = this.parent;
|
566 | if (element) {
|
567 | if (element.isRoot()) {
|
568 | element.emit(evt, data);
|
569 | } else {
|
570 | element._bubbleUp(evt, data);
|
571 | }
|
572 | }
|
573 | }
|
574 |
|
575 | |
576 |
|
577 |
|
578 | _convertToEmptyObject() {
|
579 | this.edit({});
|
580 | this.insertPlaceholder();
|
581 | }
|
582 |
|
583 | |
584 |
|
585 |
|
586 | _convertToEmptyArray() {
|
587 | this.edit([]);
|
588 | this.insertPlaceholder();
|
589 | }
|
590 |
|
591 | |
592 |
|
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 | _isElementEmpty(element) {
|
599 | return element && element.isAdded() && element.isBlank();
|
600 | }
|
601 |
|
602 | |
603 |
|
604 |
|
605 |
|
606 |
|
607 |
|
608 |
|
609 | _isExpandable(value) {
|
610 | return isObject(value) || isArray(value);
|
611 | }
|
612 |
|
613 | |
614 |
|
615 |
|
616 |
|
617 |
|
618 |
|
619 |
|
620 | _generateElements(object) {
|
621 | return new LinkedList(this, object);
|
622 | }
|
623 |
|
624 | |
625 |
|
626 |
|
627 |
|
628 |
|
629 |
|
630 |
|
631 |
|
632 | _key(key, index) {
|
633 | return this.currentType === 'Array' ? index : key;
|
634 | }
|
635 |
|
636 | |
637 |
|
638 |
|
639 | _next() {
|
640 | if (!this._isElementEmpty(this.nextElement) && !this._isElementEmpty(this)) {
|
641 | this.parent.insertAfter(this, '', '');
|
642 | }
|
643 | }
|
644 |
|
645 | |
646 |
|
647 |
|
648 | _removeAddedElements() {
|
649 | if (this.elements) {
|
650 | for (let element of this.elements) {
|
651 | if (element.isAdded()) {
|
652 | this.elements.remove(element);
|
653 | }
|
654 | }
|
655 | }
|
656 | }
|
657 | }
|
658 |
|
659 |
|
660 |
|
661 |
|
662 | class LinkedList {
|
663 | |
664 |
|
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 | at(index) {
|
671 | this.flush();
|
672 | if (!Number.isInteger(index)) {
|
673 | return undefined;
|
674 | }
|
675 |
|
676 | var element = this.firstElement;
|
677 | for (var i = 0; i < index; i++) {
|
678 | if (!element) {
|
679 | return undefined;
|
680 | }
|
681 | element = element.nextElement;
|
682 | }
|
683 | return element === null ? undefined : element;
|
684 | }
|
685 |
|
686 | get(key) {
|
687 | this.flush();
|
688 | return this._map[key];
|
689 | }
|
690 |
|
691 |
|
692 | constructor(doc, originalDoc) {
|
693 | this.firstElement = null;
|
694 | this.lastElement = null;
|
695 | this.doc = doc;
|
696 | this.originalDoc = originalDoc;
|
697 | this.keys = keys(this.originalDoc);
|
698 | if (this.doc.currentType === 'Array') {
|
699 | this.keys = this.keys.map(k => parseInt(k, 10));
|
700 | }
|
701 | this.size = this.keys.length;
|
702 | this.loaded = 0;
|
703 | this._map = {};
|
704 | }
|
705 |
|
706 | |
707 |
|
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 | insertAfter(element, key, value, added, parent) {
|
718 | this.flush();
|
719 | return this._insertAfter(element, key, value, added, parent);
|
720 | }
|
721 |
|
722 | |
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 | updateKeys(element, add) {
|
729 | this.flush();
|
730 | while (element.nextElement) {
|
731 | element.nextElement.currentKey += add;
|
732 | element = element.nextElement;
|
733 | }
|
734 | }
|
735 |
|
736 | |
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 | handleEmptyKeys(element) {
|
743 | if (element.currentKey === '') {
|
744 | let e = element;
|
745 | while (e.currentKey === '') {
|
746 | if (!e.previousElement) {
|
747 | e.currentKey = 0;
|
748 | break;
|
749 | } else {
|
750 | e = e.previousElement;
|
751 | }
|
752 | }
|
753 | while (e.nextElement) {
|
754 | e.nextElement.currentKey = e.currentKey + 1;
|
755 | e = e.nextElement;
|
756 | }
|
757 | }
|
758 | }
|
759 |
|
760 | |
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 |
|
767 |
|
768 |
|
769 |
|
770 |
|
771 | insertBefore(element, key, value, added, parent) {
|
772 | this.flush();
|
773 | return this._insertBefore(element, key, value, added, parent);
|
774 | }
|
775 |
|
776 | |
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 | insertBeginning(key, value, added, parent) {
|
787 | this.flush();
|
788 | return this._insertBeginning(key, value, added, parent);
|
789 | }
|
790 |
|
791 | |
792 |
|
793 |
|
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 | insertEnd(key, value, added, parent) {
|
802 | this.flush();
|
803 | if (!this.lastElement) {
|
804 | return this.insertBeginning(key, value, added, parent);
|
805 | }
|
806 | return this.insertAfter(this.lastElement, key, value, added, parent);
|
807 | }
|
808 |
|
809 | flush() {
|
810 | if (this.loaded < this.size) {
|
811 |
|
812 | for (let element of this) {
|
813 | if (element && element.elements) {
|
814 | element.elements.flush();
|
815 | }
|
816 | }
|
817 | }
|
818 | }
|
819 |
|
820 | |
821 |
|
822 |
|
823 |
|
824 |
|
825 | [Symbol.iterator]() {
|
826 | let currentElement;
|
827 | let index = 0;
|
828 | return {
|
829 | next: () => {
|
830 | if (this._needsLazyLoad(index)) {
|
831 | const key = this.keys[index];
|
832 | index += 1;
|
833 | currentElement = this._lazyInsertEnd(key);
|
834 | return { value: currentElement };
|
835 | } else if (this._needsStandardIteration(index)) {
|
836 | if (currentElement) {
|
837 | currentElement = currentElement.nextElement;
|
838 | } else {
|
839 | currentElement = this.firstElement;
|
840 | }
|
841 | if (currentElement) {
|
842 | index += 1;
|
843 | return { value: currentElement };
|
844 | }
|
845 | return { done: true };
|
846 | }
|
847 | return { done: true };
|
848 | }
|
849 | };
|
850 | }
|
851 |
|
852 | _needsLazyLoad(index) {
|
853 | return (index === 0 && this.loaded === 0 && this.size > 0) ||
|
854 | (this.loaded <= index && index < this.size);
|
855 | }
|
856 |
|
857 | _needsStandardIteration(index) {
|
858 | return (this.loaded > 0 && index < this.loaded && index < this.size);
|
859 | }
|
860 |
|
861 | |
862 |
|
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 | _lazyInsertEnd(key) {
|
869 | this.size -= 1;
|
870 | return this._insertEnd(key, this.originalDoc[key], this.doc.cloned, this.doc);
|
871 | }
|
872 |
|
873 | _insertEnd(key, value, added, parent) {
|
874 | if (!this.lastElement) {
|
875 | return this._insertBeginning(key, value, added, parent);
|
876 | }
|
877 | return this._insertAfter(this.lastElement, key, value, added, parent);
|
878 | }
|
879 |
|
880 | _insertBefore(element, key, value, added, parent) {
|
881 | var newElement = new Element(key, value, added, parent, element.previousElement, element);
|
882 | if (element.previousElement) {
|
883 | element.previousElement.nextElement = newElement;
|
884 | } else {
|
885 | this.firstElement = newElement;
|
886 | }
|
887 | element.previousElement = newElement;
|
888 | this._map[newElement.key] = newElement;
|
889 | this.size += 1;
|
890 | this.loaded += 1;
|
891 | return newElement;
|
892 | }
|
893 |
|
894 | _insertBeginning(key, value, added, parent) {
|
895 | if (!this.firstElement) {
|
896 | var element = new Element(key, value, added, parent, null, null);
|
897 | this.firstElement = this.lastElement = element;
|
898 | this.size += 1;
|
899 | this.loaded += 1;
|
900 | this._map[element.key] = element;
|
901 | return element;
|
902 | }
|
903 | const newElement = this.insertBefore(this.firstElement, key, value, added, parent);
|
904 | this._map[newElement.key] = newElement;
|
905 | return newElement;
|
906 | }
|
907 |
|
908 | _insertAfter(element, key, value, added, parent) {
|
909 | var newElement = new Element(key, value, added, parent, element, element.nextElement);
|
910 | if (element.nextElement) {
|
911 | element.nextElement.previousElement = newElement;
|
912 | } else {
|
913 | this.lastElement = newElement;
|
914 | }
|
915 | element.nextElement = newElement;
|
916 | this._map[newElement.key] = newElement;
|
917 | this.size += 1;
|
918 | this.loaded += 1;
|
919 | return newElement;
|
920 | }
|
921 |
|
922 | |
923 |
|
924 |
|
925 |
|
926 |
|
927 |
|
928 |
|
929 | remove(element) {
|
930 | this.flush();
|
931 | if (element.previousElement) {
|
932 | element.previousElement.nextElement = element.nextElement;
|
933 | } else {
|
934 | this.firstElement = element.nextElement;
|
935 | }
|
936 | if (element.nextElement) {
|
937 | element.nextElement.previousElement = element.previousElement;
|
938 | } else {
|
939 | this.lastElement = element.previousElement;
|
940 | }
|
941 | element.nextElement = element.previousElement = null;
|
942 | delete this._map[element.currentKey];
|
943 | this.size -= 1;
|
944 | this.loaded -= 1;
|
945 | return this;
|
946 | }
|
947 | }
|
948 |
|
949 | module.exports = Element;
|
950 | module.exports.LinkedList = LinkedList;
|
951 | module.exports.Events = Events;
|
952 | module.exports.DATE_FORMAT = DATE_FORMAT;
|