Skip to content

Commit

Permalink
Add objectAt to linked-list
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Aug 10, 2015
1 parent 3503e5e commit b13be70
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/js/utils/linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,11 @@ export default class LinkedList {
item = item.next;
}
}
objectAt(targetIndex) {
let index = -1;
return this.detect(() => {
index++;
return (targetIndex === index);
});
}
}
6 changes: 3 additions & 3 deletions tests/unit/models/markup-section-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ test('#splitMarker splits the marker at the offset', (assert) => {
s.splitMarker(m2, 3);
assert.equal(s.markers.length, 3, 'adds a 3rd marker');
assert.equal(s.markers.head.value, 'hi ', 'original marker unchanged');
assert.equal(s.markers.head.next.value, 'the', 'first half of split');
assert.equal(s.markers.objectAt(1).value, 'the', 'first half of split');
assert.equal(s.markers.tail.value, 're!', 'second half of split');
});

Expand All @@ -135,8 +135,8 @@ test('#splitMarker splits the marker at the end offset if provided', (assert) =>
s.splitMarker(m2, 1, 3);
assert.equal(s.markers.length, 4, 'adds a marker for the split and has one on each side');
assert.equal(s.markers.head.value, 'hi ', 'original marker unchanged');
assert.equal(s.markers.head.next.value, 't');
assert.equal(s.markers.head.next.next.value, 'he');
assert.equal(s.markers.objectAt(1).value, 't');
assert.equal(s.markers.objectAt(2).value, 'he');
assert.equal(s.markers.tail.value, 're!');
});

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/parsers/dom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ test('nested tags (section markup) should create a block', (assert) => {

assert.deepEqual(post, expectedPost);
let sectionMarkers = post.sections.head.markers;
assert.equal(sectionMarkers.head.next.next.markups[0], sectionMarkers.head.next.next.next.markups[0]);
assert.equal(sectionMarkers.objectAt(2).markups[0], sectionMarkers.objectAt(3).markups[0]);
});

/*
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/utils/linked-list-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,18 @@ test(`#detect finds`, (assert) => {
assert.equal(list.detect(item => item === itemThree), itemThree, 'itemThree detected');
assert.equal(list.detect(() => false), undefined, 'no item detected');
});

test(`#objectAt looks up by index`, (assert) => {
let list = new LinkedList();
let itemOne = new LinkedItem();
list.append(itemOne);
assert.equal(list.objectAt(0), itemOne, 'itemOne looked up');

let itemTwo = new LinkedItem();
let itemThree = new LinkedItem();
list.append(itemTwo);
list.append(itemThree);
assert.equal(list.objectAt(0), itemOne, 'itemOne looked up');
assert.equal(list.objectAt(1), itemTwo, 'itemTwo looked up');
assert.equal(list.objectAt(2), itemThree, 'itemThree looked up');
});

0 comments on commit b13be70

Please sign in to comment.