UNPKG

1.18 kBJavaScriptView Raw
1'use strict';
2var uncurryThis = require('../internals/function-uncurry-this');
3var aCallable = require('../internals/a-callable');
4var isNullOrUndefined = require('../internals/is-null-or-undefined');
5var lengthOfArrayLike = require('../internals/length-of-array-like');
6var toObject = require('../internals/to-object');
7var MapHelpers = require('../internals/map-helpers');
8var iterate = require('../internals/map-iterate');
9
10var Map = MapHelpers.Map;
11var mapHas = MapHelpers.has;
12var mapSet = MapHelpers.set;
13var push = uncurryThis([].push);
14
15// `Array.prototype.uniqueBy` method
16// https://github.com/tc39/proposal-array-unique
17module.exports = function uniqueBy(resolver) {
18 var that = toObject(this);
19 var length = lengthOfArrayLike(that);
20 var result = [];
21 var map = new Map();
22 var resolverFunction = !isNullOrUndefined(resolver) ? aCallable(resolver) : function (value) {
23 return value;
24 };
25 var index, item, key;
26 for (index = 0; index < length; index++) {
27 item = that[index];
28 key = resolverFunction(item);
29 if (!mapHas(map, key)) mapSet(map, key, item);
30 }
31 iterate(map, function (value) {
32 push(result, value);
33 });
34 return result;
35};