diff --git a/lib/core.js b/lib/core.js index 1bfcf15..1ac343b 100644 --- a/lib/core.js +++ b/lib/core.js @@ -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; }); \ No newline at end of file diff --git a/tests/core.test.js b/tests/core.test.js index e897785..e361a30 100644 --- a/tests/core.test.js +++ b/tests/core.test.js @@ -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' ); + + }); + + }); + + }); + }); }); \ No newline at end of file