Skip to content
This repository has been archived by the owner on Mar 11, 2018. It is now read-only.

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanqing committed Jun 7, 2015
1 parent 433c366 commit 242d303
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 41 deletions.
55 changes: 25 additions & 30 deletions jockey.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

var STOPPED = -1;

var checkIndex = function(i, len) {
if (i < 0 || i >= len) {
throw new Error('invalid index: ' + i);
}
};

var Jockey = function(items) {
if (!(this instanceof Jockey)) {
return new Jockey(items);
Expand All @@ -35,13 +29,13 @@
if (item == null) {
throw new Error('need an item');
}
checkIndex(i, this.items.length + 1);
this._c(i, this.items.length + 1);
this.items.splice(i, 0, item);
};

// Remove the item at index `i`. Throws for invalid `i`.
j.remove = function(i) {
checkIndex(i, this.items.length);
this._c(i);
this.items.splice(i, 1);
};

Expand All @@ -52,7 +46,7 @@

// Get the item at index `i`. Throws for invalid `i`.
j.get = function(i) {
checkIndex(i, this.items.length);
this._c(i);
return this.items[i];
};

Expand All @@ -63,7 +57,7 @@

// Returns the currently-playing item if playing, else returns `null`.
j.getCurrent = function() {
return this.i === STOPPED ? null : this.items[this.i];
return this.isPlaying() ? this.items[this.i] : null;
};

// Return `true` if playing.
Expand All @@ -74,15 +68,11 @@
// Play the item at index 0 if no `i` specified. Else plays the item at index
// `i`, and throws for invalid `i`.
j.play = function(i) {
var len = this.items.length;
if (len) {
if (i == null) {
this.i = 0;
} else {
checkIndex(i, len);
this.i = i;
}
if (i == null) {
i = 0;
}
this._c(i);
this.i = i;
};

// Stop playing.
Expand Down Expand Up @@ -119,7 +109,7 @@
j.next = function() {
var len = this.items.length;
var i = this.i;
if (i !== STOPPED && len) {
if (this.isPlaying() && len) {
if (i < len - 1) {
this.i++;
} else {
Expand All @@ -131,26 +121,31 @@
// Move the item at `oldIndex` to `newIndex`. Throws if either indices
// are invalid.
j.reorder = function(oldIndex, newIndex) {
var self = this;
var len = self.items.length;
checkIndex(oldIndex, len);
checkIndex(newIndex, len);
var item = self.items.splice(oldIndex, 1)[0];
self.items.splice(newIndex, 0, item);
var i = self.i;
if (i !== STOPPED) {
this._c(oldIndex);
this._c(newIndex);
var item = this.items.splice(oldIndex, 1)[0];
this.items.splice(newIndex, 0, item);
var i = this.i;
if (this.isPlaying()) {
if (i === oldIndex) {
self.i = newIndex;
this.i = newIndex;
} else {
if (oldIndex <= i && newIndex >= i) {
self.i--;
this.i--;
} else if (newIndex <= i && oldIndex >= i) {
self.i++;
this.i++;
}
}
}
};

// Throws if `i` is an invalid index.
j._c = function(i, len) {
if (i < 0 || (i >= (len || this.items.length))) {
throw new Error('invalid index: ' + i);
}
};

/* istanbul ignore else */
if (typeof module == 'object') {
module.exports = Jockey;
Expand Down
34 changes: 23 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,28 +182,40 @@ test('get the currently-playing item', function(t) {
t.equal(j.getCurrent(), 1);
});

// j.play([i]), j.isPlaying()
// play([i])

test('throws for invalid `i`', function(t) {
t.plan(5);
var j = jockey([1, 2, 3]);
test('throws if playlist is empty', function(t) {
t.plan(8);
var j = jockey();
t.false(j.isPlaying());
t.equal(j.getCurrentIndex(), -1);
t.throws(function() {
j.play(-1);
j.play();
});
t.false(j.isPlaying());
t.equal(j.getCurrentIndex(), -1);
t.throws(function() {
j.play(3);
j.play(0);
});
t.false(j.isPlaying());
t.equal(j.getCurrentIndex(), -1);
});

test('has no effect if playlist is empty', function(t) {
t.plan(2);
var j = jockey();
test('throws for invalid `i`', function(t) {
t.plan(8);
var j = jockey([1, 2, 3]);
t.false(j.isPlaying());
j.play();
t.equal(j.getCurrentIndex(), -1);
t.throws(function() {
j.play(-1);
});
t.false(j.isPlaying());
t.equal(j.getCurrentIndex(), -1);
t.throws(function() {
j.play(3);
});
t.false(j.isPlaying());
t.equal(j.getCurrentIndex(), -1);
});

test('plays the item at index 0 if no `i` specified', function(t) {
Expand All @@ -224,7 +236,7 @@ test('plays the item at index `i`', function(t) {
j.play(2);
t.true(j.isPlaying());
t.equal(j.getCurrentIndex(), 2);
j.play(0);
j.play();
t.true(j.isPlaying());
t.equal(j.getCurrentIndex(), 0);
});
Expand Down

0 comments on commit 242d303

Please sign in to comment.