Skip to content

Commit

Permalink
Make array macros work with native JS arrays (#384)
Browse files Browse the repository at this point in the history
* don't assume Ember.Arrays in array macros
  • Loading branch information
marcoow authored and Kelly Selden committed Sep 8, 2017
1 parent 06b1d8b commit 9bc537c
Show file tree
Hide file tree
Showing 42 changed files with 406 additions and 11 deletions.
3 changes: 2 additions & 1 deletion addon/array/any.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { A as emberA } from '@ember/array';
import { normalizeArray } from './-utils';

export default normalizeArray({ defaultValue: () => false }, (array, callback) => {
return array.any(callback);
return emberA(array).any(callback);
});
3 changes: 2 additions & 1 deletion addon/array/compact.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { A as emberA } from '@ember/array';
import { normalizeArray } from './-utils';

export default normalizeArray({}, array => {
return array.compact();
return emberA(array).compact();
});
3 changes: 2 additions & 1 deletion addon/array/filter-by.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { A as emberA } from '@ember/array';
import createClassComputed from 'ember-macro-helpers/create-class-computed';
import computed from 'ember-macro-helpers/computed';
import normalizeArrayKey from 'ember-macro-helpers/normalize-array-key';
Expand All @@ -13,7 +14,7 @@ export default createClassComputed(
if (!array || !key) {
return [];
}
return array.filterBy(key, ...args);
return emberA(array).filterBy(key, ...args);
});
}
);
3 changes: 2 additions & 1 deletion addon/array/find-by.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { A as emberA } from '@ember/array';
import createClassComputed from 'ember-macro-helpers/create-class-computed';
import computed from 'ember-macro-helpers/computed';
import normalizeArrayKey from 'ember-macro-helpers/normalize-array-key';
Expand All @@ -9,7 +10,7 @@ export default createClassComputed(
if (!array || !key) {
return undefined;
}
return array.findBy(key, value);
return emberA(array).findBy(key, value);
});
}
);
3 changes: 2 additions & 1 deletion addon/array/is-any.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { A as emberA } from '@ember/array';
import createClassComputed from 'ember-macro-helpers/create-class-computed';
import computed from 'ember-macro-helpers/computed';
import normalizeArrayKey from 'ember-macro-helpers/normalize-array-key';
Expand All @@ -11,7 +12,7 @@ export default createClassComputed(
}
return computed(...args, (array, ...args) => {
if (array) {
return array.isAny(key, ...args);
return emberA(array).isAny(key, ...args);
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion addon/array/is-every.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { A as emberA } from '@ember/array';
import createClassComputed from 'ember-macro-helpers/create-class-computed';
import computed from 'ember-macro-helpers/computed';
import normalizeArrayKey from 'ember-macro-helpers/normalize-array-key';
Expand All @@ -11,7 +12,7 @@ export default createClassComputed(
}
return computed(...args, (array, ...args) => {
if (array) {
return array.isEvery(key, ...args);
return emberA(array).isEvery(key, ...args);
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion addon/array/map-by.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { A as emberA } from '@ember/array';
import createClassComputed from 'ember-macro-helpers/create-class-computed';
import computed from 'ember-macro-helpers/computed';
import normalizeArrayKey from 'ember-macro-helpers/normalize-array-key';
Expand All @@ -9,7 +10,7 @@ export default createClassComputed(
if (!array || !key) {
return array;
}
return array.mapBy(key);
return emberA(array).mapBy(key);
});
}
);
3 changes: 2 additions & 1 deletion addon/array/object-at.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { A as emberA } from '@ember/array';
import { normalizeArray } from './-utils';

export default normalizeArray({}, (array, index) => {
return array.objectAt(index);
return emberA(array).objectAt(index);
});
3 changes: 2 additions & 1 deletion addon/array/reject-by.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { A as emberA } from '@ember/array';
import createClassComputed from 'ember-macro-helpers/create-class-computed';
import computed from 'ember-macro-helpers/computed';
import normalizeArrayKey from 'ember-macro-helpers/normalize-array-key';
Expand All @@ -16,7 +17,7 @@ export default createClassComputed(
if (!key) {
return array;
}
return array.rejectBy(key, ...args);
return emberA(array).rejectBy(key, ...args);
});
}
);
2 changes: 2 additions & 0 deletions addon/array/uniq-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default createClassComputed(
if (array === undefined || key === undefined) {
return array;
}

if (!array.uniqBy) {
// TODO: polyfill this
// from https://github.com/emberjs/ember.js/blob/v2.11.0/packages/ember-runtime/lib/mixins/enumerable.js#L1094-L1105
Expand All @@ -28,6 +29,7 @@ export default createClassComputed(

return ret;
}

return array.uniqBy(key);
});
}
Expand Down
3 changes: 2 additions & 1 deletion addon/array/uniq.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { A as emberA } from '@ember/array';
import { normalizeArray } from './-utils';

export default normalizeArray({}, array => {
return array.uniq();
return emberA(array).uniq();
});
3 changes: 2 additions & 1 deletion addon/array/without.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { A as emberA } from '@ember/array';
import { normalizeArray } from './-utils';

export default normalizeArray({}, (array, item) => {
return array.without(item);
return emberA(array).without(item);
});
11 changes: 11 additions & 0 deletions tests/integration/array/any-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,14 @@ test('composable: it returns true if any true', function(assert) {
strictEqual: true
});
});

test('it handles native arrays', function(assert) {
compute({
assert,
computed: any('array', result => result === 3),
properties: {
array: [1, 2]
},
strictEqual: false
});
});
11 changes: 11 additions & 0 deletions tests/integration/array/compact-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,14 @@ test('composable: it calls compact on array', function(assert) {
deepEqual: [2]
});
});

test('it handles native arrays', function(assert) {
compute({
assert,
computed: compact('array'),
properties: {
array: [undefined, 2]
},
deepEqual: [2]
});
});
13 changes: 13 additions & 0 deletions tests/integration/array/concat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,16 @@ test('composable: it calls concat on array', function(assert) {
deepEqual: [0, value1, value2]
});
});

test('it handles native arrays', function(assert) {
compute({
assert,
computed: concat('array', 'value1', 'value2'),
properties: {
array: [0],
value1,
value2
},
deepEqual: [0, value1, value2]
});
});
11 changes: 11 additions & 0 deletions tests/integration/array/every-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,14 @@ test('composable: it returns true if all true', function(assert) {
strictEqual: true
});
});

test('it handles native arrays', function(assert) {
compute({
assert,
computed: every('array', result => result === 1),
properties: {
array: [1, 2]
},
strictEqual: false
});
});
13 changes: 13 additions & 0 deletions tests/integration/array/filter-by-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,16 @@ test('composable: it filters array if found', function(assert) {
deepEqual: [{ test: 'val2' }]
});
});

test('it handles native arrays', function(assert) {
compute({
assert,
computed: filterBy('array', 'key', 'value'),
properties: {
array: [{ test: 'val1' }, { test: 'val2' }],
key: 'test',
value: 'val2'
},
deepEqual: [{ test: 'val2' }]
});
});
11 changes: 11 additions & 0 deletions tests/integration/array/filter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,14 @@ test('composable: it filters array if found', function(assert) {
deepEqual: [2]
});
});

test('it handles native arrays', function(assert) {
compute({
assert,
computed: filter('array', result => result === 2),
properties: {
array: [1, 2]
},
deepEqual: [2]
});
});
14 changes: 14 additions & 0 deletions tests/integration/array/find-by-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,17 @@ test('composable: it returns item if found', function(assert) {
strictEqual: expected
});
});

test('it handles native arrays', function(assert) {
let expected = { test: 'val2' };
compute({
assert,
computed: findBy('array', 'key', 'value'),
properties: {
array: [{ test: 'val1' }, expected],
key: 'test',
value: 'val2'
},
strictEqual: expected
});
});
11 changes: 11 additions & 0 deletions tests/integration/array/find-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,14 @@ test('composable: it returns item if found', function(assert) {
strictEqual: 2
});
});

test('it handles native arrays', function(assert) {
compute({
assert,
computed: find('array', result => result === 2),
properties: {
array: [1, 2]
},
strictEqual: 2
});
});
11 changes: 11 additions & 0 deletions tests/integration/array/first-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,14 @@ test('handles array changes', function(assert) {

assert.strictEqual(get(subject, 'computed'), 'test2');
});

test('it handles native arrays', function(assert) {
compute({
assert,
computed: first('array'),
properties: {
array: ['test1', 'test2']
},
strictEqual: 'test1'
});
});
27 changes: 27 additions & 0 deletions tests/integration/array/group-by-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,30 @@ test('it groups array by key and comparator', function(assert) {

assert.deepEqual(subject.get('computed'), expected);
});

test('it handles native arrays', function(assert) {
let item1 = { test: 1, name: 'foo' };
let item2 = { test: 2, name: 'bar' };
let item3 = { test: 3, name: 'foo' };

compute({
assert,
computed: groupBy('array', 'key'),
properties: {
array: [item1, item2, item3],
key: 'name'
},
deepEqual: [
{
key: 'name',
value: 'foo',
items: [item1, item3]
},
{
key: 'name',
value: 'bar',
items: [item2]
}
]
});
});
12 changes: 12 additions & 0 deletions tests/integration/array/includes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,15 @@ test('it handles nesting', function(assert) {
strictEqual: true
});
});

test('it handles native arrays', function(assert) {
compute({
assert,
computed: includes('array', 'source'),
properties: {
array: ['my value'],
source: 'my value'
},
strictEqual: true
});
});
13 changes: 13 additions & 0 deletions tests/integration/array/index-of-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,16 @@ test('composable: it calls indexOf on array', function(assert) {
strictEqual: 2
});
});

test('it handles native arrays', function(assert) {
compute({
assert,
computed: indexOf('array', 'value', 'fromIndex'),
properties: {
array: [1, 2, 1],
value: 1,
fromIndex: 1
},
strictEqual: 2
});
});
18 changes: 18 additions & 0 deletions tests/integration/array/invoke-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,21 @@ test('it responds to array property value changes', function(assert) {
'foo1-baz-val2'
]);
});

test('it handles native arrays', function(assert) {
array = [{
foo(arg = 'bar') {
return arg + '-eval';
}
}];

compute({
assert,
computed: invoke('array', 'methodName'),
properties: {
array,
methodName: 'foo'
},
deepEqual: ['bar-eval']
});
});
13 changes: 13 additions & 0 deletions tests/integration/array/is-any-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,16 @@ test('composable: it calls isAny on array', function(assert) {
strictEqual: true
});
});

test('it handles native arrays', function(assert) {
compute({
assert,
computed: isAny('array', 'key', 'value'),
properties: {
array: [{ test: 'val1' }, { test: 'val2' }],
key: 'test',
value: 'val2'
},
strictEqual: true
});
});
Loading

0 comments on commit 9bc537c

Please sign in to comment.