forked from caolan/async
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
86 lines (77 loc) · 2.16 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
var utils = module.exports = {};
utils.toString = Object.prototype.toString;
utils.isArray = Array.isArray || function isArray( obj ) {
return utils.toString.call( obj ) === '[object Array]';
};
utils.each = function each( arr, iterator ) {
for ( var i = 0; i < arr.length; i += 1 ) {
iterator( arr[ i ], i, arr );
}
};
utils.map = function map( arr, iterator ) {
if ( arr.map ) {
return arr.map( iterator );
}
var results = [];
utils.each( arr, function( x, i, a ) {
results.push( iterator( x, i, a ) );
} );
return results;
};
utils.reduce = function reduce( arr, iterator, memo ) {
if ( arr.reduce ) {
return arr.reduce( iterator, memo );
}
utils.each( arr, function( x, i, a ) {
memo = iterator( memo, x, i, a );
} );
return memo;
};
utils.keys = function keys( obj ) {
if ( Object.keys ) {
return Object.keys( obj );
}
var _keys = [];
for ( var k in obj ) {
if ( obj.hasOwnProperty( k ) ) {
_keys.push( k );
}
}
return _keys;
};
utils.only_once = function only_once( fn ) {
var called = false;
return function() {
if ( called ) throw new Error( "Callback was already called." );
called = true;
fn.apply( null, arguments );
};
};
function _console_fn( name ) {
return function( fn ) {
var args = Array.prototype.slice.call( arguments, 1 );
fn.apply( null, args.concat( [ function( err ) {
var args = Array.prototype.slice.call( arguments, 1 );
if ( typeof console !== 'undefined' ) {
if ( err ) {
if ( console.error ) {
console.error( err );
}
}
else if ( console[ name ] ) {
utils.each( args, function( x ) {
console[ name ]( x );
} );
}
}
} ] ) );
};
}
utils.log = _console_fn( 'log' );
utils.dir = _console_fn( 'dir' );
/*
utils.info = _console_fn('info');
utils.warn = _console_fn('warn');
utils.error = _console_fn('error');
*/