Skip to content

Commit

Permalink
eq / get / toggleClass added with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlhannes committed Nov 7, 2016
1 parent 3532aef commit 253e93c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/core/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
25 changes: 25 additions & 0 deletions src/core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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];
};
}
16 changes: 15 additions & 1 deletion tests/classes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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" );
} );

} );
23 changes: 23 additions & 0 deletions tests/selection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<div>...</div> <div>!!!</div>";

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 = "<div class='parent'> <span class='child'>...</span> </div>";

Expand All @@ -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 = "<div>...</div> <div>!!!</div>";

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;
} );

} );

0 comments on commit 253e93c

Please sign in to comment.