Skip to content

Commit

Permalink
[CLEANUP canary] use Set for uniqBy and unique
Browse files Browse the repository at this point in the history
  • Loading branch information
bekzod committed Dec 8, 2017
1 parent e1dc61f commit 6e5cb66
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = {
'clearInterval': true,
'console': true,

'Set': true,
'Symbol': true,
'WeakMap': true,
},
Expand Down
14 changes: 7 additions & 7 deletions packages/ember-runtime/lib/computed/reduce_computed_macros.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
@module @ember/object
*/

import { guidFor } from 'ember-utils';
import { assert } from 'ember-debug';
import {
get,
Expand Down Expand Up @@ -407,12 +405,14 @@ export function filterBy(dependentKey, propertyKey, value) {
export function uniq(...args) {
return multiArrayMacro(args, function(dependentKeys) {
let uniq = emberA();
let seen = new Set();

dependentKeys.forEach(dependentKey => {
let value = get(this, dependentKey);
if (isArray(value)) {
value.forEach(item => {
if (uniq.indexOf(item) === -1) {
if (!seen.has(item)) {
seen.add(item);
uniq.push(item);
}
});
Expand Down Expand Up @@ -461,13 +461,13 @@ export function uniqBy(dependentKey, propertyKey) {

let cp = new ComputedProperty(function() {
let uniq = emberA();
let seen = Object.create(null);
let list = get(this, dependentKey);
if (isArray(list)) {
let seen = new Set();
list.forEach(item => {
let guid = guidFor(get(item, propertyKey));
if (!(guid in seen)) {
seen[guid] = true;
let val = get(item, propertyKey);
if (!seen.has(val)) {
seen.add(val);
uniq.push(item);
}
});
Expand Down
17 changes: 9 additions & 8 deletions packages/ember-runtime/lib/mixins/enumerable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// HELPERS
//

import { guidFor } from 'ember-utils';
import {
get,
set,
Expand Down Expand Up @@ -811,9 +810,11 @@ const Enumerable = Mixin.create({
uniq() {
let ret = emberA();

this.forEach(k => {
if (ret.indexOf(k) < 0) {
ret.push(k);
let seen = new Set();
this.forEach(item => {
if (!seen.has(item)) {
seen.add(item);
ret.push(item);
}
});

Expand Down Expand Up @@ -1066,12 +1067,12 @@ const Enumerable = Mixin.create({

uniqBy(key) {
let ret = emberA();
let seen = Object.create(null);
let seen = new Set();

this.forEach((item) => {
let guid = guidFor(get(item, key));
if (!(guid in seen)) {
seen[guid] = true;
let val = get(item, key);
if (!seen.has(val)) {
seen.add(val);
ret.push(item);
}
});
Expand Down

0 comments on commit 6e5cb66

Please sign in to comment.