Skip to content

Instantly share code, notes, and snippets.

@asakasinsky
Created September 26, 2013 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asakasinsky/6719174 to your computer and use it in GitHub Desktop.
Save asakasinsky/6719174 to your computer and use it in GitHub Desktop.
var ArrayModel = function(){};
ArrayModel.prototype = [];
_.extend(ArrayModel.prototype, {
pointer: -1,
next: function ()
{
if (!((this.pointer + 1) in this)) {return false;}
this.pointer = this.pointer + 1;
return this;
},
prev: function ()
{
if (!((this.pointer - 1) in this)) {return false;}
this.pointer = this.pointer - 1;
return this;
},
first: function ()
{
this.pointer = 0;
return this;
},
last: function ()
{
this.pointer = this.length - 1;
return this;
},
remove: function ()
{
return this.splice(this.pointer, 1)[0];
},
replace: function (arr)
{
return this.splice(this.pointer, 1, arr)[0];
},
insertAfter: function (arr)
{
this.pointer = this.pointer + 1;
return this.splice(this.pointer, 0, arr);
},
swap: function (index_a, index_b)
{
if (this.length === 1) {return this;}
this.splice(index_b, 1, this.splice(index_a, 1, this[index_b])[0]);
return this;
},
set_pointer: function (index)
{
index = parseInt(index, 10) || 0;
this.pointer = index;
return this;
},
get: function ()
{
if ( typeof this[this.pointer] === 'function' ) {
return this[this.pointer]();
}
return this[this.pointer];
}
});
var screenplay = new ArrayModel();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment