1 | /**
|
2 | * Lo-Dash 2.4.0 (Custom Build) <http://lodash.com/>
|
3 | * Build: `lodash modularize modern exports="npm" -o ./npm/`
|
4 | * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
|
5 | * Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
|
6 | * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
7 | * Available under MIT license <http://lodash.com/license>
|
8 | */
|
9 | var createAggregator = require('lodash._createaggregator');
|
10 |
|
11 | /**
|
12 | * Creates an object composed of keys generated from the results of running
|
13 | * each element of the collection through the given callback. The corresponding
|
14 | * value of each key is the last element responsible for generating the key.
|
15 | * The callback is bound to `thisArg` and invoked with three arguments;
|
16 | * (value, index|key, collection).
|
17 | *
|
18 | * If a property name is provided for `callback` the created "_.pluck" style
|
19 | * callback will return the property value of the given element.
|
20 | *
|
21 | * If an object is provided for `callback` the created "_.where" style callback
|
22 | * will return `true` for elements that have the properties of the given object,
|
23 | * else `false`.
|
24 | *
|
25 | * @static
|
26 | * @memberOf _
|
27 | * @category Collections
|
28 | * @param {Array|Object|string} collection The collection to iterate over.
|
29 | * @param {Function|Object|string} [callback=identity] The function called
|
30 | * per iteration. If a property name or object is provided it will be used
|
31 | * to create a "_.pluck" or "_.where" style callback, respectively.
|
32 | * @param {*} [thisArg] The `this` binding of `callback`.
|
33 | * @returns {Object} Returns the composed aggregate object.
|
34 | * @example
|
35 | *
|
36 | * var keys = [
|
37 | * { 'dir': 'left', 'code': 97 },
|
38 | * { 'dir': 'right', 'code': 100 }
|
39 | * ];
|
40 | *
|
41 | * _.indexBy(keys, 'dir');
|
42 | * // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
|
43 | *
|
44 | * _.indexBy(keys, function(key) { return String.fromCharCode(key.code); });
|
45 | * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
|
46 | *
|
47 | * _.indexBy(characters, function(key) { this.fromCharCode(key.code); }, String);
|
48 | * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
|
49 | */
|
50 | var indexBy = createAggregator(function(result, value, key) {
|
51 | result[key] = value;
|
52 | });
|
53 |
|
54 | module.exports = indexBy;
|