Skip to content

Commit

Permalink
extract uniqBy as generic method and reuse internally
Browse files Browse the repository at this point in the history
  • Loading branch information
bekzod committed Apr 22, 2018
1 parent fe6f3df commit ec812bd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 39 deletions.
15 changes: 2 additions & 13 deletions packages/@ember/object/lib/computed/reduce_computed_macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { assert } from '@ember/debug';
import { get, ComputedProperty, addObserver, removeObserver, getProperties } from 'ember-metal';
import { compare, isArray, A as emberA } from 'ember-runtime';
import { compare, isArray, A as emberA, uniqBy as uniqByArray } from 'ember-runtime';

function reduceMacro(dependentKey, callback, initialValue, name) {
assert(
Expand Down Expand Up @@ -495,19 +495,8 @@ export function uniqBy(dependentKey, propertyKey) {

let cp = new ComputedProperty(
function() {
let uniq = emberA();
let list = get(this, dependentKey);
if (isArray(list)) {
let seen = new Set();
list.forEach(item => {
let val = get(item, propertyKey);
if (!seen.has(val)) {
seen.add(val);
uniq.push(item);
}
});
}
return uniq;
return isArray(list) ? uniqByArray(list, propertyKey) : emberA();
},
{ dependentKeys: [`${dependentKey}.[]`], readOnly: true }
);
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export { default as Evented } from './lib/mixins/evented';
export { default as PromiseProxyMixin } from './lib/mixins/promise_proxy';

export { default as RSVP, onerrorDefault } from './lib/ext/rsvp'; // just for side effect of extending Ember.RSVP
export { isArray, typeOf } from './lib/utils';
export { isArray, typeOf, uniqBy } from './lib/utils';

import './lib/ext/function'; // just for side effect of extending Function.prototype
26 changes: 3 additions & 23 deletions packages/ember-runtime/lib/mixins/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Copyable from '../mixins/copyable';
import copy from '../copy';
import EmberError from '@ember/error';
import MutableEnumerable from './mutable_enumerable';
import { uniqBy } from '../utils';

const EMPTY_ARRAY = Object.freeze([]);
const EMBER_ARRAY = symbol('EMBER_ARRAY');
Expand Down Expand Up @@ -1002,17 +1003,7 @@ const ArrayMixin = Mixin.create(Enumerable, {
@public
*/
uniq() {
let ret = A();

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

return ret;
return uniqBy(this);
},

/**
Expand All @@ -1030,18 +1021,7 @@ const ArrayMixin = Mixin.create(Enumerable, {
*/

uniqBy(key) {
let ret = A();
let seen = new Set();

this.forEach(item => {
let val = get(item, key);
if (!seen.has(val)) {
seen.add(val);
ret.push(item);
}
});

return ret;
return uniqBy(this, key);
},

/**
Expand Down
25 changes: 23 additions & 2 deletions packages/ember-runtime/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { DEBUG } from '@glimmer/env';
import { PROXY_CONTENT } from 'ember-metal';
import { PROXY_CONTENT, get } from 'ember-metal';
import { HAS_NATIVE_PROXY } from 'ember-utils';
import EmberArray from './mixins/array';
import EmberArray, { A } from './mixins/array';
import EmberObject from './system/object';
import { assert } from '@ember/debug';

// ........................................
// TYPING & ARRAY MESSAGING
Expand Down Expand Up @@ -168,3 +169,23 @@ export function typeOf(item) {

return ret;
}

const identityFunction = item => item;

export function uniqBy(array, key = identityFunction) {
assert(`first argument passed to \`uniqBy\` should be array`, isArray(array));

let ret = A();
let seen = new Set();
let getter = typeof key === 'function' ? key : get;

array.forEach(item => {
let val = getter(item, key);
if (!seen.has(val)) {
seen.add(val);
ret.push(item);
}
});

return ret;
}

0 comments on commit ec812bd

Please sign in to comment.