Skip to content

Commit

Permalink
Add splice method to linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Aug 10, 2015
1 parent b13be70 commit 6e12e70
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/js/models/markup-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ export default class Section extends LinkedItem {
}

replaceMarker(previousMarker, newMarkers=[]) {
let nextMarker = previousMarker.next;
newMarkers.forEach(marker => {
this.markers.insertBefore(marker, nextMarker);
});
this.removeMarker(previousMarker);
this.markers.splice(previousMarker, 1, newMarkers);
}

prependMarker(marker) {
Expand Down
14 changes: 14 additions & 0 deletions src/js/utils/linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,18 @@ export default class LinkedList {
return (targetIndex === index);
});
}
splice(targetItem, removalCount, newItems) {
let item = targetItem;
let nextItem = item.next;
let count = 0;
while (item && count < removalCount) {
count++;
nextItem = item.next;
this.remove(item);
item = nextItem;
}
newItems.forEach((newItem) => {
this.insertBefore(newItem, nextItem);
});
}
}
43 changes: 43 additions & 0 deletions tests/unit/utils/linked-list-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,46 @@ test(`#objectAt looks up by index`, (assert) => {
assert.equal(list.objectAt(1), itemTwo, 'itemTwo looked up');
assert.equal(list.objectAt(2), itemThree, 'itemThree looked up');
});

test(`#splice removes a target and inserts an array of items`, (assert) => {
let list = new LinkedList();
let itemOne = new LinkedItem();
let itemTwo = new LinkedItem();
let itemThree = new LinkedItem();
list.append(itemOne);
list.append(itemThree);

list.splice(itemOne, 1, [itemTwo]);

assert.equal(list.head, itemTwo, 'itemOne is head');
assert.equal(list.objectAt(1), itemThree, 'itemThree is present');
});

test(`#splice remove nothing and inserts an array of nothing`, (assert) => {
let list = new LinkedList();
let itemOne = new LinkedItem();
let itemTwo = new LinkedItem();
list.append(itemOne);
list.append(itemTwo);

list.splice(itemTwo, 0, []);

assert.equal(list.head, itemOne, 'itemOne is head');
assert.equal(list.objectAt(1), itemTwo, 'itemTwo is present');
});

test(`#splice can reorganize items`, (assert) => {
let list = new LinkedList();
let itemOne = new LinkedItem();
let itemTwo = new LinkedItem();
let itemThree = new LinkedItem();
list.append(itemOne);
list.append(itemTwo);
list.append(itemThree);

list.splice(itemOne, 3, [itemThree, itemOne, itemTwo]);

assert.equal(list.head, itemThree, 'itemThree is head');
assert.equal(list.objectAt(1), itemOne, 'itemOne is present');
assert.equal(list.objectAt(2), itemTwo, 'itemTwo is present');
});

0 comments on commit 6e12e70

Please sign in to comment.