Skip to content

Commit

Permalink
Add .slice + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
José Moreira committed Aug 31, 2014
1 parent 35255f4 commit 031bf70
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ module.exports = (function ( Util ){
return target;
};

Util.slice = function ( target, start, end ) {

if ( Util.is.Function( target.slice ) ) {
return target.slice( start, end );
}

if ( Util.is.Object( target ) ) {
var keys = Object.keys( target ).slice( start, end );

return Util.filter( target, function ( value, key ) {
return keys.indexOf( key ) !== -1;
});
}

return [];
};

Util.log = console.log;

});
189 changes: 189 additions & 0 deletions tests/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,195 @@ describe( "Util", function () {

});

describe( "slice", function () {

describe( "array", function () {

it( "from second element to the end", function () {
var t, a = [
'a',
'b',
'c',
];

t = Util.slice( a, 1 );

expect( Util.is.Array( t ) ).to.be.ok;
expect( t ).to.have.length( 2 );
expect( t[0] ).to.be.equal( 'b' );
expect( t[1] ).to.be.equal( 'c' );

});


it( "from second element to third", function () {
var t, a = [
'a',
'b',
'c',
];

t = Util.slice( a, 1, 2 );

expect( Util.is.Array( t ) ).to.be.ok;
expect( t ).to.have.length( 1 );
expect( t[0] ).to.be.equal( 'b' );

});

it( "from last two to the end", function () {
var t, a = [
'a',
'b',
'c',
];

t = Util.slice( a, -2 );

expect( Util.is.Array( t ) ).to.be.ok;
expect( t ).to.have.length( 2 );
expect( t[0] ).to.be.equal( 'b' );
expect( t[1] ).to.be.equal( 'c' );

});

it( "from last two except last one", function () {
var t, a = [
'a',
'b',
'c',
];

t = Util.slice( a, -2, -1 );

expect( Util.is.Array( t ) ).to.be.ok;
expect( t ).to.have.length( 1 );
expect( t[0] ).to.be.equal( 'b' );

});

});

describe( "string", function () {

it( "from second element to the end", function () {
var t, a = 'abc';

t = Util.slice( a, 1 );

expect( Util.is.String( t ) ).to.be.ok;
expect( t ).to.have.length( 2 );
expect( t[0] ).to.be.equal( 'b' );
expect( t[1] ).to.be.equal( 'c' );

});


it( "from second element to third", function () {
var t, a = 'abc';

t = Util.slice( a, 1, 2 );

expect( Util.is.String( t ) ).to.be.ok;
expect( t ).to.have.length( 1 );
expect( t[0] ).to.be.equal( 'b' );

});

it( "from last two to the end", function () {
var t, a = 'abc';

t = Util.slice( a, -2 );

expect( Util.is.String( t ) ).to.be.ok;
expect( t ).to.have.length( 2 );
expect( t[0] ).to.be.equal( 'b' );
expect( t[1] ).to.be.equal( 'c' );

});

it( "from last two except last one", function () {
var t, a = 'abc';

t = Util.slice( a, -2, -1 );

expect( Util.is.String( t ) ).to.be.ok;
expect( t ).to.have.length( 1 );
expect( t[0] ).to.be.equal( 'b' );

});

});

describe( "object", function () {

it( "from second element to the end", function () {
var t, a = {
first: 'a',
second: 'b',
third: 'c',
};

t = Util.slice( a, 1 );

expect( Util.is.Object( t ) ).to.be.ok;
expect( Object.keys( t ) ).to.have.length( 2 );
expect( t.second ).to.be.equal( 'b' );
expect( t.third ).to.be.equal( 'c' );

});


it( "from second element to third", function () {
var t, a = {
first: 'a',
second: 'b',
third: 'c',
};

t = Util.slice( a, 1, 2 );

expect( Util.is.Object( t ) ).to.be.ok;
expect( Object.keys( t ) ).to.have.length( 1 );
expect( t.second ).to.be.equal( 'b' );

});

it( "from last two to the end", function () {
var t, a = {
first: 'a',
second: 'b',
third: 'c',
};

t = Util.slice( a, -2 );

expect( Util.is.Object( t ) ).to.be.ok;
expect( Object.keys( t ) ).to.have.length( 2 );
expect( t.second ).to.be.equal( 'b' );
expect( t.third ).to.be.equal( 'c' );

});

it( "from last two except last one", function () {
var t, a = {
first: 'a',
second: 'b',
third: 'c',
};

t = Util.slice( a, -2, -1 );

expect( Util.is.Object( t ) ).to.be.ok;
expect( Object.keys( t ) ).to.have.length( 1 );
expect( t.second ).to.be.equal( 'b' );

});

});

});

});

});

0 comments on commit 031bf70

Please sign in to comment.