Last change
on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 | var toLength = require('../internals/to-length');
|
---|
| 3 | var toObject = require('../internals/to-object');
|
---|
| 4 | var getBuiltIn = require('../internals/get-built-in');
|
---|
| 5 | var arraySpeciesCreate = require('../internals/array-species-create');
|
---|
| 6 |
|
---|
| 7 | var push = [].push;
|
---|
| 8 |
|
---|
| 9 | // `Array.prototype.uniqueBy` method
|
---|
| 10 | // https://github.com/tc39/proposal-array-unique
|
---|
| 11 | module.exports = function uniqueBy(resolver) {
|
---|
| 12 | var that = toObject(this);
|
---|
| 13 | var length = toLength(that.length);
|
---|
| 14 | var result = arraySpeciesCreate(that, 0);
|
---|
| 15 | var Map = getBuiltIn('Map');
|
---|
| 16 | var map = new Map();
|
---|
| 17 | var resolverFunction, index, item, key;
|
---|
| 18 | if (typeof resolver == 'function') resolverFunction = resolver;
|
---|
| 19 | else if (resolver == null) resolverFunction = function (value) {
|
---|
| 20 | return value;
|
---|
| 21 | };
|
---|
| 22 | else throw new TypeError('Incorrect resolver!');
|
---|
| 23 | for (index = 0; index < length; index++) {
|
---|
| 24 | item = that[index];
|
---|
| 25 | key = resolverFunction(item);
|
---|
| 26 | if (!map.has(key)) map.set(key, item);
|
---|
| 27 | }
|
---|
| 28 | map.forEach(function (value) {
|
---|
| 29 | push.call(result, value);
|
---|
| 30 | });
|
---|
| 31 | return result;
|
---|
| 32 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.