From 7f03a71c820962eeb75b56c01f4d7792cf12c100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Moreira?= Date: Thu, 18 Sep 2014 15:47:10 +0100 Subject: [PATCH] Version: 0.2.0 --- lib/isisnt.js | 2 +- lib/type/array.js | 16 +++++++--------- lib/type/object.js | 40 +++++++++++++++++++-------------------- package.json | 2 +- test/core.test.js | 28 +++++++++++++-------------- test/isisnt.test.js | 2 ++ test/type/array.test.js | 38 +++++++++++++++++++++++++++++++++++++ test/type/object.test.js | 41 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 123 insertions(+), 46 deletions(-) create mode 100644 test/type/array.test.js create mode 100644 test/type/object.test.js diff --git a/lib/isisnt.js b/lib/isisnt.js index f78cc6c..7c5cc5d 100644 --- a/lib/isisnt.js +++ b/lib/isisnt.js @@ -7,7 +7,7 @@ module.exports = (function ( Util ){ Util.isnt = Util.Isnt = {}; Util.is.instanceof = function ( ClassToEval, instance ) { - return instance && instance.constructor && instance.constructor === ClassToEval || instance instanceof ClassToEval; + return instance && instance.constructor ? instance.constructor === ClassToEval : instance instanceof ClassToEval; }; Util.isnt.instanceof = function ( ClassToEval, instance ) { return instance && instance.constructor ? instance.constructor !== ClassToEval : ! ( instance instanceof ClassToEval ); diff --git a/lib/type/array.js b/lib/type/array.js index 4d15853..f3f73c5 100644 --- a/lib/type/array.js +++ b/lib/type/array.js @@ -40,10 +40,10 @@ module.exports = (function ( Util ){ return res; }; - $.filter = function ( o, fn, context ) { + $.filter = function ( array, fn, context ) { - if ( Util.isnt.Array( o ) ) { - throw new TypeError("o must be an object"); + if ( Util.isnt.Array( array ) ) { + throw new TypeError("array must be an array"); } if ( Util.isnt.Function( fn ) ) { throw new TypeError("fn must be a function"); @@ -51,13 +51,11 @@ module.exports = (function ( Util ){ var res = [], i; - for( i in o ) { - if ( o.hasOwnProperty( i ) ) { - if( fn.call( context || o, o[ i ], i, o ) ) { - res.push( o[ i ] ); - } + Util.each( array, function ( v, k ) { + if( fn.call( context || array, v, k, array ) ) { + res.push( array[ k ] ); } - } + }); return res; }; diff --git a/lib/type/object.js b/lib/type/object.js index 1f30a24..a857103 100644 --- a/lib/type/object.js +++ b/lib/type/object.js @@ -1,14 +1,14 @@ module.exports = (function ( Util ){ - var $ = Util.Object = Util.object = {}; + var $ = Util.Object = Util.Object = {}; // is and isnt $.is = Util.is.Object; $.isnt = Util.isnt.Object; - $.forEach = $.each = function ( o, fn, context ) { + $.forEach = $.each = function ( object, fn, context ) { - if ( Util.isnt.Object( o ) ) { - throw new TypeError("o must be an object"); + if ( Util.isnt.Object( object ) ) { + throw new TypeError("object must be an object"); } if ( Util.isnt.Function( fn ) ) { throw new TypeError("fn must be a function"); @@ -16,16 +16,16 @@ module.exports = (function ( Util ){ var i; - for( i in o ) { - if ( o.hasOwnProperty( i ) ) { - fn.call( context || o, o[ i ], i, o ); + for( i in object ) { + if ( object.hasOwnProperty( i ) ) { + fn.call( context || object, object[ i ], i, object ); } } - return o; + return object; }; - $.map = function ( o, fn, context ) { + $.map = function ( object, fn, context ) { if ( Util.isnt.Function( fn ) ) { throw new TypeError("fn must be a function"); @@ -33,31 +33,29 @@ module.exports = (function ( Util ){ var res = {}, i; - Util.each( o, function ( v, k ) { - res[ k ] = fn.call( context || o, o[ i ], i, o ); + Util.each( object, function ( v, k ) { + res[ k ] = fn.call( context || object, v, k, object ); }); return res; }; - $.filter = function ( o, fn, context ) { + $.filter = function ( object, fn, context ) { - if ( Util.isnt.object( o ) && Util.isnt.array( o ) ) { - throw new TypeError("o must be an object"); + if ( Util.isnt.Object( object ) ) { + throw new TypeError("object must be an object"); } - if ( Util.isnt.function( fn ) ) { + if ( Util.isnt.Function( fn ) ) { throw new TypeError("fn must be a function"); } var res = {}, i; - for( i in o ) { - if ( o.hasOwnProperty( i ) ) { - if( fn.call( context || o, o[ i ], i, o ) ) { - res[ i ] = o[ i ]; - } + Util.each( object, function ( v, k ) { + if( fn.call( context || object, v, k, object ) ) { + res[ k ] = object[ k ]; } - } + }); return res; }; diff --git a/package.json b/package.json index 274f114..7cf71b1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "findhit-util", "description": "general propose utils for javascript", - "version": "0.1.1", + "version": "0.2.0", "author": "José Moreira ", "contributors": [ { diff --git a/test/core.test.js b/test/core.test.js index e361a30..6302db8 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -42,20 +42,20 @@ describe( "Util", function () { describe( "map", function () { - it( "get 3rd value of each array on array", function () { - var t, a = [ - [ 1, 2, 3 ], - [ 4, 5, 6 ], - [ 7, 8, 9 ], - ]; + it( "should return an array when array is given", function () { + var t, a = []; + + t = Util.map( a, function ( v ) { return v }); + + expect( Util.is.Array( t ) ).to.be.ok; + }); - t = Util.map( a, function ( v ) { return v[ 2 ] }); + it( "should return an object when object is given", function () { + var t, a = {}; - expect( t ).to.have.length( 3 ); - expect( t[0] ).to.be.equal( 3 ); - expect( t[1] ).to.be.equal( 6 ); - expect( t[2] ).to.be.equal( 9 ); + t = Util.map( a, function ( v ) { return v }); + expect( Util.is.Object( t ) ).to.be.ok; }); }); @@ -88,9 +88,9 @@ describe( "Util", function () { t = Util.filter( a, function ( value ) { return value.match(/a/); }); - expect( Object.keys( t ) ).to.have.length( 1 ); - - expect( t.foo ).to.be.equal( 'bar' ); + expect( t ).to.deep.equal({ + foo: 'bar' + }); expect( t.bar ).to.not.be.ok; }); diff --git a/test/isisnt.test.js b/test/isisnt.test.js index cc4e50b..6df4f15 100644 --- a/test/isisnt.test.js +++ b/test/isisnt.test.js @@ -49,6 +49,7 @@ describe( "Util", function () { it( "is", function () { expect( Util.is.object( {} ) ).to.be.ok + expect( Util.is.object( [] ) ).to.not.be.ok expect( Util.is.object( 'ola' ) ).to.not.be.ok }); @@ -56,6 +57,7 @@ describe( "Util", function () { it( "isn't", function () { expect( Util.isnt.object( {} ) ).to.not.be.ok + expect( Util.isnt.object( [] ) ).to.be.ok expect( Util.isnt.object( 'ola' ) ).to.be.ok }); diff --git a/test/type/array.test.js b/test/type/array.test.js new file mode 100644 index 0000000..e4a42b0 --- /dev/null +++ b/test/type/array.test.js @@ -0,0 +1,38 @@ +var Util = require('../../index'), + + sinon = require('sinon'), + chai = require('chai'), + expect = chai.expect; + +describe( "Util", function () { + + describe( "type", function () { + + describe( "array", function () { + + describe( "map", function () { + + it( "get 3rd value of each array on array", function () { + var t, a = [ + [ 1, 2, 3 ], + [ 4, 5, 6 ], + [ 7, 8, 9 ], + ]; + + t = Util.map( a, function ( v ) { return v[ 2 ] }); + + expect( t ).to.have.length( 3 ); + expect( t[0] ).to.be.equal( 3 ); + expect( t[1] ).to.be.equal( 6 ); + expect( t[2] ).to.be.equal( 9 ); + + }); + + + }); + + }); + + }); + +}); \ No newline at end of file diff --git a/test/type/object.test.js b/test/type/object.test.js new file mode 100644 index 0000000..fb8b1ca --- /dev/null +++ b/test/type/object.test.js @@ -0,0 +1,41 @@ +var Util = require('../../index'), + + sinon = require('sinon'), + chai = require('chai'), + expect = chai.expect; + +describe( "Util", function () { + + describe( "type", function () { + + describe( "object", function () { + + describe( "map", function () { + + it( "get keys of objects (PoC for objects)", function () { + var t, a = { // Social networks + findhit: '1', + facebook: '2', + instagram: '3', + snapchat: '4', + twitter: '5' + }; + + t = Util.map( a, function ( v, k ) { return k }); + + expect( t ).to.deep.equal({ + findhit: 'findhit', + facebook: 'facebook', + instagram: 'instagram', + snapchat: 'snapchat', + twitter: 'twitter' + }); + }); + + }); + + }); + + }); + +}); \ No newline at end of file