UNPKG

1.26 kBJavaScriptView Raw
1'use strict';
2var $ = require('../internals/export');
3var toLength = require('../internals/to-length');
4var toObject = require('../internals/to-object');
5var getBuiltIn = require('../internals/get-built-in');
6var arraySpeciesCreate = require('../internals/array-species-create');
7var addToUnscopables = require('../internals/add-to-unscopables');
8
9var push = [].push;
10
11// `Array.prototype.uniqueBy` method
12// https://github.com/tc39/proposal-array-unique
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
38addToUnscopables('uniqueBy');