Skip to content

Commit

Permalink
clean-up, allow as in PHP, array with object (optionally as string) a…
Browse files Browse the repository at this point in the history
…s argument and callback (optionally as string) (per issue 31 at #31 )
  • Loading branch information
brettz9 committed Jul 12, 2013
1 parent 93f6b01 commit e6dfda4
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions functions/array/array_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ function array_map (callback) {
// http://kevin.vanzonneveld.net
// + original by: Andrea Giammarchi (http://webreflection.blogspot.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: thekid
// + improved by: Brett Zamir (http://brett-zamir.me)
// % note 1: Takes a function as an argument, not a function's name
// % note 2: If the callback is a string, it can only work if the function name is in the global context
// % note 1: If the callback is a string (or object, if an array is supplied), it can only work if the function name is in the global context
// * example 1: array_map( function (a){return (a * a * a)}, [1, 2, 3, 4, 5] );
// * returns 1: [ 1, 8, 27, 64, 125 ]
var argc = arguments.length,
argv = arguments;
var j = argv[1].length,
argv = arguments,
glbl = this.window,
obj = null,
cb = callback,
j = argv[1].length,
i = 0,
k = 1,
m = 0;
var tmp = [],
m = 0,
tmp = [],
tmp_ar = [];

while (i < j) {
Expand All @@ -26,9 +29,16 @@ function array_map (callback) {

if (callback) {
if (typeof callback === 'string') {
callback = this.window[callback];
cb = glbl[callback];
}
else if (typeof callback === 'object' && callback.length) {
obj = typeof callback[0] === 'string' ? glbl[callback[0]] : callback[0];
if (typeof obj === 'undefined') {
throw 'Object not found: ' + callback[0];
}
cb = typeof callback[1] === 'string' ? obj[callback[1]] : callback[1];
}
tmp_ar[i++] = callback.apply(null, tmp);
tmp_ar[i++] = cb.apply(obj, tmp);
} else {
tmp_ar[i++] = tmp;
}
Expand Down

0 comments on commit e6dfda4

Please sign in to comment.