Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make map and asyncMap generate an object if initially given an object…
… (rather than always generating an array
- Loading branch information
1 parent
bb2e781
commit b235fd8
Showing
6 changed files
with
25 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,15 @@ | ||
var isArray = require('./isArray'), | ||
isArguments = require('./isArguments') | ||
var isList = require('./isList') | ||
|
||
module.exports = function(items, ctx, fn) { | ||
module.exports = function each(items, fn) { | ||
if (!items) { return } | ||
if (!fn) { | ||
fn = ctx | ||
ctx = this | ||
} | ||
if (isArray(items) || isArguments(items)) { | ||
if (isList(items)) { | ||
for (var i=0; i < items.length; i++) { | ||
fn.call(ctx, items[i], i) | ||
fn(items[i], i) | ||
} | ||
} else { | ||
for (var key in items) { | ||
if (!items.hasOwnProperty(key)) { continue } | ||
fn.call(ctx, items[key], key) | ||
fn(items[key], key) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module.exports = function first(arr) { | ||
return arr[0] | ||
return arr ? arr[0] : null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
var each = require('./each') | ||
var isList = require('./isList') | ||
|
||
module.exports = function(items, ctx, fn) { | ||
var result = [] | ||
if (!fn) { | ||
fn = ctx | ||
ctx = this | ||
} | ||
each(items, ctx, function(item, key) { | ||
result.push(fn.call(ctx, item, key)) | ||
module.exports = function map(obj, fn) { | ||
var result = isList(obj) ? new Array(obj.length) : {} | ||
each(obj, function(val, key) { | ||
result[key] = fn(val, key) | ||
}) | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var identity = require('./identity') | ||
var each = require('./each') | ||
|
||
module.exports = function toArray(obj, fn) { | ||
if (!fn) { fn = identity } | ||
var result = [] | ||
each(obj, function(val, key) { | ||
result.push(fn(val, key)) | ||
}) | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters