Skip to content

Instantly share code, notes, and snippets.

@nexpr
Created December 4, 2015 08:57
Show Gist options
  • Save nexpr/96d5b75d44a14774e676 to your computer and use it in GitHub Desktop.
Save nexpr/96d5b75d44a14774e676 to your computer and use it in GitHub Desktop.
配列にカーソル付ける
!function(){
Array.prototype.cursorInit = function(){
this.cursor = 0
return this
}
Array.prototype.cursorEnd = function(){
this.cursor = this.length ? this.length - 1 : 0
return this
}
Array.prototype.cursorNext = function(n){
this.cursor += ~~n ? n : 1
return this
}
Array.prototype.cursorPrev = function(n){
this.cursor -= ~~n ? n : 1
return this
}
Array.prototype.cursorToIndex = function(idx){
this.cursor = ~~idx
return this
}
Array.prototype.cursorToNext = function(fn, n){
if(typeof fn !== "function"){
return this.cursorToNextMatch(fn, n)
}
n = ~~n ? ~~n : 1
while(this.cursor < this.length){
this.cursor++
if(fn(this[this.cursor]) && --n === 0){
break
}
}
return this
}
Array.prototype.cursorToPrev = function(fn, n){
if(typeof fn !== "function"){
return this.cursorToPrevMatch(fn, n)
}
n = ~~n ? ~~n : 1
while(this.cursor >= 0){
this.cursor--
if(fn(this[this.cursor]) && --n === 0){
break
}
}
return this
}
Array.prototype.cursorFromFirst = function(fn, n){
if(typeof fn !== "function"){
return this.cursorFromFirstMatch(fn, n)
}
n = ~~n ? ~~n : 1
for(var i=0;i < this.length;i++){
if(fn(this[i]) && --n === 0){
break
}
}
this.cursor = i
return this
}
Array.prototype.cursorFromLast = function(fn, n){
if(typeof fn !== "function"){
return this.cursorFromLastMatch(fn, n)
}
n = ~~n ? ~~n : 1
for(var i=this.length - 1;i >= 0;i--){
if(fn(this[i]) && --n === 0){
break
}
}
this.cursor = i
return this
}
Array.prototype.cursorToNextMatch = function(match, n){
n = ~~n ? ~~n : 1
while(this.cursor < this.length){
this.cursor++
if(match === this[this.cursor] && --n === 0){
break
}
}
return this
}
Array.prototype.cursorToPrevMatch = function(match, n){
n = ~~n ? ~~n : 1
while(this.cursor >= 0){
this.cursor--
if(match === this[this.cursor] && --n === 0){
break
}
}
return this
}
Array.prototype.cursorFromFirstMatch = function(match, n){
n = ~~n ? ~~n : 1
for(var i=0;i < this.length;i++){
if(match === this[i] && --n === 0){
break
}
}
this.cursor = i
return this
}
Array.prototype.cursorFromLastMatch = function(match, n){
n = ~~n ? ~~n : 1
for(var i=this.length - 1;i >= 0;i--){
if(match === this[i] && --n === 0){
break
}
}
this.cursor = i
return this
}
Array.prototype.cursorGet = function(){
return this.cursor
}
Array.prototype.cursorGetVal = function(){
return this[this.cursor]
}
Array.prototype.cursorPos = function(pos){
if(arguments.length){
this.cursor = ~~pos
}
return this.cursor
}
Array.prototype.cursorNull = function(){
return this[this.cursor] == null
}
Array.prototype.cursorOutOfRange = function(){
return this.cursor < 0 || this.cursor >= this.length
}
Array.prototype.cursorValExists = function(){
return this.cursor in this
}
Array.prototype.cursorInjectAfter = function(){
var args = [].slice.call(arguments)
var apargs = [this.cursor + 1, 0].concat(args)
this.splice.apply(this, apargs)
return this
}
Array.prototype.cursorInjectBefore = function(){
var args = [].slice.call(arguments)
var apargs = [this.cursor, 0].concat(args)
this.splice.apply(this, apargs)
this.cursorNext(args.length)
return this
}
Array.prototype.cursorRemove = function(){
this.splice(this.cursor, 1)
return this
}
Array.prototype.cursorSetVal = function(val){
this[this.cursor] = val
return this
}
Array.prototype.cursorReplace = function(val){
if(arguments.length === 0){
return this.cursorRemove()
}
if(arguments.length === 1){
return this.setVal(val)
}
var args = [].slice.call(arguments)
var apargs = [this.cursor, 1].concat(args)
this.splice.apply(this, apargs)
return this
}
Array.prototype.cursorSlice = function(num, not_contain){
var negative = num < 0 || 1 / num < 0
var pos = this.cursor + (not_contain ^ negative ? 1 : 0)
num = ~~num
if(num === 0){
return negative ? this.slice(0, pos) : this.slice(pos)
}else if(num > 0){
return this.slice(pos, pos + num)
}else{
return this.slice(pos + num, pos)
}
}
Array.prototype.cursorSwap = function(pos){
pos = ~~pos
var t = this[pos]
this[pos] = this[this.cursor]
this[this.cursor] = t
}
Array.prototype.getCursor = function(){
return new Cursor(this)
}
function Cursor(owner){
this.owner = owner
}
Cursor.prototype.init = function(){
return apply(this.owner, "cursorInit", arguments), this
}
Cursor.prototype.reset = function(){
return apply(this.owner, "cursorInit", arguments), this
}
Cursor.prototype.end = function(){
return apply(this.owner, "cursorEnd", arguments), this
}
Cursor.prototype.next = function(){
return apply(this.owner, "cursorNext", arguments), this
}
Cursor.prototype.prev = function(){
return apply(this.owner, "cursorPrev", arguments), this
}
Cursor.prototype.to = function(){
return apply(this.owner, "cursorToIndex", arguments), this
}
Cursor.prototype.toIndex = function(){
return apply(this.owner, "cursorToIndex", arguments), this
}
Cursor.prototype.set = function(){
return apply(this.owner, "cursorToIndex", arguments), this
}
Cursor.prototype.toNext = function(){
return apply(this.owner, "cursorToNext", arguments), this
}
Cursor.prototype.toPrev = function(){
return apply(this.owner, "cursorToPrev", arguments), this
}
Cursor.prototype.fromFirst = function(){
return apply(this.owner, "cursorFromFirst", arguments), this
}
Cursor.prototype.fromLast = function(){
return apply(this.owner, "cursorFromLast", arguments), this
}
Cursor.prototype.nextSearch = function(){
return apply(this.owner, "cursorToNext", arguments), this
}
Cursor.prototype.prevSearch = function(){
return apply(this.owner, "cursorToPrev", arguments), this
}
Cursor.prototype.firstSearch = function(){
return apply(this.owner, "cursorFromFirst", arguments), this
}
Cursor.prototype.lastSearch = function(){
return apply(this.owner, "cursorFromLast", arguments), this
}
Cursor.prototype.toNextMatch = function(){
return apply(this.owner, "cursorToNextMatch", arguments), this
}
Cursor.prototype.toPrevMatch = function(){
return apply(this.owner, "cursorToPrevMatch", arguments), this
}
Cursor.prototype.fromFirstMatch = function(){
return apply(this.owner, "cursorFromFirstMatch", arguments), this
}
Cursor.prototype.fromLastMatch = function(){
return apply(this.owner, "cursorFromLastMatch", arguments), this
}
Cursor.prototype.nextMatch = function(){
return apply(this.owner, "cursorToNextMatch", arguments), this
}
Cursor.prototype.prevMatch = function(){
return apply(this.owner, "cursorToPrevMatch", arguments), this
}
Cursor.prototype.firstMatch = function(){
return apply(this.owner, "cursorFromFirstMatch", arguments), this
}
Cursor.prototype.lastMatch = function(){
return apply(this.owner, "cursorFromLastMatch", arguments), this
}
Cursor.prototype.get = function(){
return apply(this.owner, "cursorGet", arguments)
}
Cursor.prototype.getVal = function(){
return apply(this.owner, "cursorGetVal", arguments)
}
Cursor.prototype.val = function(){
return apply(this.owner, "cursorGetVal", arguments)
}
Cursor.prototype.pos = function(){
return apply(this.owner, "cursorPos", arguments)
}
Cursor.prototype.null = function(){
return apply(this.owner, "cursorNull", arguments)
}
Cursor.prototype.isNull = function(){
return apply(this.owner, "cursorNull", arguments)
}
Cursor.prototype.outOfRange = function(){
return apply(this.owner, "cursorOutOfRange", arguments)
}
Cursor.prototype.isOutOfRange = function(){
return apply(this.owner, "cursorOutOfRange", arguments)
}
Cursor.prototype.valExists = function(){
return apply(this.owner, "cursorValExists", arguments)
}
Cursor.prototype.isValExists = function(){
return apply(this.owner, "cursorValExists", arguments)
}
Cursor.prototype.injectAfter = function(){
return apply(this.owner, "cursorInjectAfter", arguments), this
}
Cursor.prototype.injectBefore = function(){
return apply(this.owner, "cursorInjectBefore", arguments), this
}
Cursor.prototype.remove = function(){
return apply(this.owner, "cursorRemove", arguments), this
}
Cursor.prototype.setVal = function(){
return apply(this.owner, "cursorSetVal", arguments), this
}
Cursor.prototype.replace = function(){
return apply(this.owner, "cursorReplace", arguments), this
}
Cursor.prototype.slice = function(){
return apply(this.owner, "cursorSlice", arguments)
}
Cursor.prototype.swap = function(){
return apply(this.owner, "cursorSwap", arguments), this
}
function apply(obj, method, args){
return obj[method].apply(obj, args)
}
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment