From 253e93cc116d55e23b07693ac869c7642e38469c Mon Sep 17 00:00:00 2001 From: Hannes Lindquist Date: Mon, 7 Nov 2016 21:11:12 +0100 Subject: [PATCH] eq / get / toggleClass added with tests --- src/core/classes.js | 16 ++++++++++++++++ src/core/selection.js | 25 +++++++++++++++++++++++++ tests/classes.spec.js | 16 +++++++++++++++- tests/selection.spec.js | 23 +++++++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/core/classes.js b/src/core/classes.js index 8c6c347..054642f 100644 --- a/src/core/classes.js +++ b/src/core/classes.js @@ -84,4 +84,20 @@ export function classes( obj ) { return obj; }; + + /** + * Toggle a class + * + * @param {String} className The class name to toggle + * @return {Object} + */ + obj.toggleClass = function( className ) { + if ( obj.hasClass( className ) ) { + obj.removeClass( className ); + } else { + obj.addClass( className ); + } + + return obj; + }; } diff --git a/src/core/selection.js b/src/core/selection.js index 43f890d..355cb14 100644 --- a/src/core/selection.js +++ b/src/core/selection.js @@ -60,9 +60,20 @@ export function selection( obj ) { return select( items ); }; + /** + * Return a specific item + * + * @param {Integer} index Index to get by, zero-based + * @return {Object} + */ + obj.eq = function( index ) { + return obj.elements[index] ? select( obj.elements[index] ) : undefined; + }; + /** * Return the the children of all selected objects objects * + * @param {String} selectItem Selector string * @return {Object} */ obj.find = function( selectItem ) { @@ -92,4 +103,18 @@ export function selection( obj ) { return select( items ); }; + + /** + * Get element NOT wrapped in select by index + * + * @param {Integer} index Index to get by, zero-based + * @return {NodeElement/Array} Node Element if index specified, otherwise array of node elements + */ + obj.get = function( index ) { + if ( index === undefined ) { + return obj.elements; + } + + return obj.elements[index]; + }; } diff --git a/tests/classes.spec.js b/tests/classes.spec.js index 56e7b1a..e7da692 100644 --- a/tests/classes.spec.js +++ b/tests/classes.spec.js @@ -26,7 +26,7 @@ describe( "Class manipulation", () => { expect( n.elements[0].className ).to.be.equal( "duplicate" ); } ); - it( "should deduplicate classes", function () { + it( "should remove classes", function () { let n = document.createElement( "test" ); n.className = "test1 test2"; @@ -36,4 +36,18 @@ describe( "Class manipulation", () => { expect( n.elements[0].className ).to.be.equal( "test1" ); } ); + it( "should toggle classes", function () { + let n = document.createElement( "test" ); + + n.className = "test1"; + + n = select( n ).toggleClass( "test1" ); + + expect( n.elements[0].className ).to.be.equal( "" ); + + n.toggleClass( "test1" ); + + expect( n.elements[0].className ).to.be.equal( "test1" ); + } ); + } ); diff --git a/tests/selection.spec.js b/tests/selection.spec.js index a9d33f6..2983a41 100644 --- a/tests/selection.spec.js +++ b/tests/selection.spec.js @@ -33,6 +33,18 @@ describe( "Selection", () => { expect( select( "div.parent" ).children().elements[0].className ).to.be.equal( "child" ); } ); + it( "should be able to fetch by index", function () { + document.body.innerHTML = "
...
!!!
"; + + expect( select( "div" ).eq( 0 ).elements.length ).to.be.equal( 1 ); + expect( select( "div" ).eq( 0 ).elements[0].innerHTML ).to.be.equal( "..." ); + + expect( select( "div" ).eq( 1 ).elements.length ).to.be.equal( 1 ); + expect( select( "div" ).eq( 1 ).elements[0].innerHTML ).to.be.equal( "!!!" ); + + expect( select( "div" ).eq( 2 ) ).to.be.undefined; + } ); + it( "should find elements in elements correctly", function () { document.body.innerHTML = "
...
"; @@ -47,4 +59,15 @@ describe( "Selection", () => { expect( select( "span" ).filter( ( item ) => item.hasClass( "item" ) ).elements.length ).to.be.equal( 1 ); } ); + it( "should be able to fetch by index", function () { + document.body.innerHTML = "
...
!!!
"; + + expect( select( "div" ).get().length ).to.be.equal( 2 ); + expect( select( "div" ).get( 0 ).innerHTML ).to.be.equal( "..." ); + + expect( select( "div" ).get( 1 ).innerHTML ).to.be.equal( "!!!" ); + + expect( select( "div" ).get( 2 ) ).to.be.undefined; + } ); + } );