chain-lightning
Advanced tools
+21
-13
@@ -93,2 +93,10 @@ /* | ||
| // Array.prototype.includes() uses this for value equality | ||
| // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes | ||
| function isIdenticalTo( x , y ) { | ||
| return x === y || ( Number.isNaN( x ) && Number.isNaN( y ) ) ; | ||
| } | ||
| List.prototype.push = | ||
@@ -208,3 +216,3 @@ List.prototype.append = function append( ... elements ) { | ||
| while ( current ) { | ||
| if ( current.element === element ) { return current ; } | ||
| if ( isIdenticalTo( current.element , element ) ) { return current ; } | ||
| current = current.next ; | ||
@@ -230,3 +238,3 @@ } | ||
| while ( current ) { | ||
| if ( current.element === element ) { return current ; } | ||
| if ( isIdenticalTo( current.element , element ) ) { return current ; } | ||
| current = current.previous ; | ||
@@ -240,10 +248,2 @@ } | ||
| // Array.prototype.includes() uses this for value equality | ||
| // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes | ||
| function sameValueZero( x , y ) { | ||
| return x === y || ( typeof x === 'number' && typeof y === 'number' && isNaN( x ) && isNaN( y ) ) ; | ||
| } | ||
| // Almost like .indexOf() | ||
@@ -262,3 +262,3 @@ List.prototype.includes = function includes( element , fromSlot ) { | ||
| while ( current ) { | ||
| if ( sameValueZero( current.element , element ) ) { return true ; } | ||
| if ( isIdenticalTo( current.element , element ) ) { return true ; } | ||
| current = current.next ; | ||
@@ -466,4 +466,4 @@ } | ||
| List.prototype.delete = | ||
| List.prototype.remove = function remove( slot ) { | ||
| List.prototype.deleteSlot = | ||
| List.prototype.removeSlot = function removeSlot( slot ) { | ||
| if ( ! slot || slot.list !== this ) { return false ; } | ||
@@ -484,2 +484,10 @@ | ||
| // Delete all occurences of a value | ||
| List.prototype.delete = | ||
| List.prototype.remove = function remove( value ) { | ||
| this.inPlaceFilter( e => ! isIdenticalTo( e , value ) ) ; | ||
| } ; | ||
| List.prototype.moveAfter = function moveAfter( slot , afterSlot ) { | ||
@@ -486,0 +494,0 @@ if ( |
+1
-1
| { | ||
| "name": "chain-lightning", | ||
| "version": "0.1.1", | ||
| "version": "0.2.0", | ||
| "description": "Linked list", | ||
@@ -5,0 +5,0 @@ "main": "lib/chainlightning.js", |
@@ -449,2 +449,35 @@ /* | ||
| it( ".removeSlot()/.deleteSlot()" , () => { | ||
| var list , | ||
| e1 = { v: 'jack' } , | ||
| e2 = { v: 'bob' } , | ||
| e3 = { v: 'steve' } , | ||
| e4 = { v: 'bobby' } ; | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| list.removeSlot( list.slotOf( e2 ) ) ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List() ; | ||
| list.removeSlot( list.slotOf( e2 ) ) ; | ||
| expect( [ ... list ] ).to.equal( [] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e2 ) ; | ||
| list.removeSlot( list.slotOf( e2 ) ) ; | ||
| expect( [ ... list ] ).to.equal( [] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e2 , e1 , e3 ) ; | ||
| list.removeSlot( list.slotOf( e2 ) ) ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| list = new List( e1 , e3 , e2 ) ; | ||
| list.removeSlot( list.slotOf( e2 ) ) ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| } ) ; | ||
| it( ".remove()/.delete()" , () => { | ||
@@ -458,3 +491,3 @@ var list , | ||
| list = new List( e1 , e2 , e3 ) ; | ||
| list.remove( list.slotOf( e2 ) ) ; | ||
| list.remove( e2 ) ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 ] ) ; | ||
@@ -464,3 +497,3 @@ sanityCheck( list ) ; | ||
| list = new List() ; | ||
| list.remove( list.slotOf( e2 ) ) ; | ||
| list.remove( e2 ) ; | ||
| expect( [ ... list ] ).to.equal( [] ) ; | ||
@@ -470,3 +503,3 @@ sanityCheck( list ) ; | ||
| list = new List( e2 ) ; | ||
| list.remove( list.slotOf( e2 ) ) ; | ||
| list.remove( e2 ) ; | ||
| expect( [ ... list ] ).to.equal( [] ) ; | ||
@@ -476,3 +509,3 @@ sanityCheck( list ) ; | ||
| list = new List( e2 , e1 , e3 ) ; | ||
| list.remove( list.slotOf( e2 ) ) ; | ||
| list.remove( e2 ) ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 ] ) ; | ||
@@ -482,5 +515,17 @@ sanityCheck( list ) ; | ||
| list = new List( e1 , e3 , e2 ) ; | ||
| list.remove( list.slotOf( e2 ) ) ; | ||
| list.remove( e2 ) ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| // Remove all occurences | ||
| list = new List( e2 , e2 , e2 , e1 , e2 , e3 , e2 ) ; | ||
| list.remove( e2 ) ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| // NaN test | ||
| list = new List( NaN , NaN , NaN , e1 , NaN , e3 , NaN ) ; | ||
| list.remove( NaN ) ; | ||
| expect( [ ... list ] ).to.equal( [ e1 , e3 ] ) ; | ||
| sanityCheck( list ) ; | ||
| } ) ; | ||
@@ -487,0 +532,0 @@ |
44786
3.14%1126
3.87%