Last active
August 29, 2015 14:07
-
-
Save Dammmien/ee65f29276cc99ef5476 to your computer and use it in GitHub Desktop.
Super Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SA = function( a ) { | |
a.has = function( v ) { | |
var i = a.length; | |
for ( i; i--; ) | |
if ( a[ i ] === v ) | |
return true; | |
return false; | |
}; | |
a.each = function( f ) { | |
var i = 0, | |
n = a.length; | |
for ( i; i < n; i++ ) | |
f( a[ i ], i ); | |
return a; | |
}; | |
a.remove = function( v ) { | |
var i = a.length; | |
for ( i; i--; ) | |
if ( a[ i ] === v ) | |
a.splice( i, 1 ); | |
return a; | |
}; | |
a.replace = function( b, c ) { | |
var i = a.length; | |
for ( i; i--; ) | |
if ( a[ i ] === b ) | |
a[ i ] = c; | |
return a; | |
}; | |
a.count = function( v ) { | |
var c = 0, | |
i = a.length; | |
for ( i; i--; ) | |
if ( a[ i ] === v ) | |
c++; | |
return c; | |
}; | |
a.removeDuplicate = function() { | |
var i = a.length; | |
for ( i; i--; ) | |
if ( a.count( a[ i ] ) > 1 ) | |
a.splice( i, 1 ); | |
return a; | |
}; | |
return a; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var foo = SA( [ 1, 2, 3 ] ); | |
foo.push( 'four' ); | |
foo.each( function( a ) { | |
console.log( a ); | |
} ) | |
foo.remove( 1 ); | |
foo.replace( 2, 'two' ); | |
foo.push( 'two' ); | |
foo.removeDuplicate(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment