1 | 'use strict';
|
2 | var $ = require('../internals/export');
|
3 | var toLength = require('../internals/to-length');
|
4 | var toObject = require('../internals/to-object');
|
5 | var getBuiltIn = require('../internals/get-built-in');
|
6 | var arraySpeciesCreate = require('../internals/array-species-create');
|
7 | var addToUnscopables = require('../internals/add-to-unscopables');
|
8 |
|
9 | var push = [].push;
|
10 |
|
11 |
|
12 |
|
13 | $({ target: 'Array', proto: true }, {
|
14 | uniqueBy: function uniqueBy(resolver) {
|
15 | var that = toObject(this);
|
16 | var length = toLength(that.length);
|
17 | var result = arraySpeciesCreate(that, 0);
|
18 | var Map = getBuiltIn('Map');
|
19 | var map = new Map();
|
20 | var resolverFunction, index, item, key;
|
21 | if (typeof resolver == 'function') resolverFunction = resolver;
|
22 | else if (resolver == null) resolverFunction = function (value) {
|
23 | return value;
|
24 | };
|
25 | else throw new TypeError('Incorrect resolver!');
|
26 | for (index = 0; index < length; index++) {
|
27 | item = that[index];
|
28 | key = resolverFunction(item);
|
29 | if (!map.has(key)) map.set(key, item);
|
30 | }
|
31 | map.forEach(function (value) {
|
32 | push.call(result, value);
|
33 | });
|
34 | return result;
|
35 | }
|
36 | });
|
37 |
|
38 | addToUnscopables('uniqueBy');
|