map-properties
Version:
array.map equivalent that also works on Objects and Strings
22 lines (16 loc) • 494 B
JavaScript
var forEach = require('for-each');
module.exports = function (source, iterator, context, destination) {
var isString = false;
if (!destination) {
isString = typeof source === 'string';
destination = (isString || Array.isArray(source)) ? [] : {};
}
forEach(source, function (value, key, source) {
destination[key] = iterator.call(this, value, key, source, destination);
}, context);
if (isString) {
destination = destination.join('');
}
return destination;
};
;