UNPKG

@segment/analytics.js-core

Version:

The hassle-free way to integrate analytics into any web application.

56 lines (55 loc) 2.16 kB
'use strict'; /* * The MIT License (MIT) * Copyright (c) 2015 Nathan Houle * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ /* * Module dependencies. */ var each = require('./each'); /** * Produce a new array by passing each value in the input `collection` through a transformative * `iterator` function. The `iterator` function is passed three arguments: * `(value, index, collection)`. * * @name map * @api public * @param {Function} iterator The transformer function to invoke per iteration. * @param {Array} collection The collection to iterate over. * @return {Array} A new array containing the results of each `iterator` invocation. * @example * var square = function(x) { return x * x; }; * * map(square, [1, 2, 3]); * //=> [1, 4, 9] */ var map = function map(iterator, collection) { if (typeof iterator !== 'function') { throw new TypeError('Expected a function but received a ' + typeof iterator); } var result = []; each(function (val, i, collection) { result.push(iterator(val, i, collection)); }, collection); return result; }; /* * Exports. */ module.exports = map;