Skip to content

Commit

Permalink
Version: 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
José Moreira committed Sep 18, 2014
1 parent 88ec765 commit 7f03a71
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 46 deletions.
2 changes: 1 addition & 1 deletion lib/isisnt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
16 changes: 7 additions & 9 deletions lib/type/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,22 @@ 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");
}

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;
};
Expand Down
40 changes: 19 additions & 21 deletions lib/type/object.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,61 @@
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");
}

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

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;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "findhit-util",
"description": "general propose utils for javascript",
"version": "0.1.1",
"version": "0.2.0",
"author": "José Moreira <[email protected]>",
"contributors": [
{
Expand Down
28 changes: 14 additions & 14 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

});
Expand Down Expand Up @@ -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;

});
Expand Down
2 changes: 2 additions & 0 deletions test/isisnt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ 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

});

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

});
Expand Down
38 changes: 38 additions & 0 deletions test/type/array.test.js
Original file line number Diff line number Diff line change
@@ -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 );

});


});

});

});

});
41 changes: 41 additions & 0 deletions test/type/object.test.js
Original file line number Diff line number Diff line change
@@ -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'
});
});

});

});

});

});

0 comments on commit 7f03a71

Please sign in to comment.