main
Last change
on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[79a0317] | 1 | 'use strict';
|
---|
| 2 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
| 3 | var aCallable = require('../internals/a-callable');
|
---|
| 4 | var isNullOrUndefined = require('../internals/is-null-or-undefined');
|
---|
| 5 | var lengthOfArrayLike = require('../internals/length-of-array-like');
|
---|
| 6 | var toObject = require('../internals/to-object');
|
---|
| 7 | var MapHelpers = require('../internals/map-helpers');
|
---|
| 8 | var iterate = require('../internals/map-iterate');
|
---|
| 9 |
|
---|
| 10 | var Map = MapHelpers.Map;
|
---|
| 11 | var mapHas = MapHelpers.has;
|
---|
| 12 | var mapSet = MapHelpers.set;
|
---|
| 13 | var push = uncurryThis([].push);
|
---|
| 14 |
|
---|
| 15 | // `Array.prototype.uniqueBy` method
|
---|
| 16 | // https://github.com/tc39/proposal-array-unique
|
---|
| 17 | module.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 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.