Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup array mixin #16718

Merged
merged 3 commits into from
Jun 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 9 additions & 29 deletions packages/ember-runtime/lib/mixins/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

import { ARRAY_AT_EACH } from '@ember/deprecated-features';
import { DEBUG } from '@glimmer/env';
import { HAS_NATIVE_PROXY } from 'ember-utils';
import { PROXY_CONTENT } from 'ember-metal';
import { symbol, toString } from 'ember-utils';
import { symbol, toString, HAS_NATIVE_PROXY, tryInvoke } from 'ember-utils';
import {
get,
set,
Expand Down Expand Up @@ -146,10 +145,7 @@ export function isArray(_obj) {
if (!obj || obj.setInterval) {
return false;
}
if (Array.isArray(obj)) {
return true;
}
if (ArrayMixin.detect(obj)) {
if (Array.isArray(obj) || ArrayMixin.detect(obj)) {
return true;
}

Expand Down Expand Up @@ -786,7 +782,7 @@ const ArrayMixin = Mixin.create(Enumerable, {
@public
*/
findBy() {
return this.find(iter(...arguments));
return find(this, iter(...arguments));
},

/**
Expand Down Expand Up @@ -845,7 +841,7 @@ const ArrayMixin = Mixin.create(Enumerable, {
@public
*/
isEvery() {
return this.every(iter(...arguments));
return every(this, iter(...arguments));
},

/**
Expand Down Expand Up @@ -903,7 +899,7 @@ const ArrayMixin = Mixin.create(Enumerable, {
@public
*/
isAny() {
return this.any(iter(...arguments));
return any(this, iter(...arguments));
},

/**
Expand Down Expand Up @@ -936,17 +932,16 @@ const ArrayMixin = Mixin.create(Enumerable, {
@method reduce
@param {Function} callback The callback to execute
@param {Object} initialValue Initial value for the reduce
@param {String} reducerProperty internal use only.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reducerProperty seems not used anywhere and remains from SproutCore days I think

@return {Object} The reduced value.
@public
*/
reduce(callback, initialValue, reducerProperty) {
reduce(callback, initialValue) {
assert('`reduce` expects a function as first argument.', typeof callback === 'function');

let ret = initialValue;

this.forEach(function(item, i) {
ret = callback(ret, item, i, this, reducerProperty);
ret = callback(ret, item, i, this);
}, this);

return ret;
Expand All @@ -964,17 +959,7 @@ const ArrayMixin = Mixin.create(Enumerable, {
@public
*/
invoke(methodName, ...args) {
let ret = A();

this.forEach((x, idx) => {
let method = x && x[methodName];

if ('function' === typeof method) {
ret[idx] = args.length ? method.apply(x, args) : x[methodName]();
}
}, this);

return ret;
return this.map(item => tryInvoke(item, methodName, args));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change breaks the API of invoke, which now no longer returns an Ember.A when prototype extensions are disabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is wrapping it with Ember.A ok ? or revert will help ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to revert just this part, and add some tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},

/**
Expand Down Expand Up @@ -1172,17 +1157,12 @@ const ArrayMixin = Mixin.create(Enumerable, {

const OUT_OF_RANGE_EXCEPTION = 'Index out of range';

export function removeAt(array, start, len) {
export function removeAt(array, start, len = 1) {
if ('number' === typeof start) {
if (start < 0 || start >= array.length) {
throw new EmberError(OUT_OF_RANGE_EXCEPTION);
}

// fast case
if (len === undefined) {
len = 1;
}

array.replace(start, len, EMPTY_ARRAY);
}

Expand Down