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

[maybe breaking] Stop using Ember.A() #435

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions addon/helpers/find-by.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { helper } from '@ember/component/helper';
import { isEmpty } from '@ember/utils';
import { A as emberArray } from '@ember/array';
import { get } from '@ember/object';
import asArray from '../utils/as-array';

export function findBy([byPath, value, array]) {
if (isEmpty(byPath)) {
return [];
}

return emberArray(asArray(array)).findBy(byPath, value);
return asArray(array).find((item) => {
return get(item, byPath) === value;
});
}

export default helper(findBy);
5 changes: 2 additions & 3 deletions addon/helpers/includes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { A as emberArray } from '@ember/array';
import { isArray as isEmberArray } from '@ember/array';
import { helper } from '@ember/component/helper';
import asArray from '../utils/as-array';
Expand All @@ -9,10 +8,10 @@ export function includes(needleOrNeedles, haystack) {
}

let needles = isEmberArray(needleOrNeedles) ? needleOrNeedles : [needleOrNeedles];
let haystackAsEmberArray = emberArray(asArray(haystack));
let haystackArray = asArray(haystack);

return asArray(needles).every((needle) => {
return haystackAsEmberArray.includes(needle);
return haystackArray.includes(needle);
});
}

Expand Down
4 changes: 2 additions & 2 deletions addon/helpers/next.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { helper } from '@ember/component/helper';
import getIndex from '../utils/get-index';
import { isEmpty } from '@ember/utils';
import { A as emberArray } from '@ember/array';
import getValueArrayAndUseDeepEqualFromParams from '../-private/get-value-array-and-use-deep-equal-from-params';
import asArray from '../utils/as-array';
import arrayAt from '../utils/array-at';

export function next(currentValue, maybeArray, useDeepEqual = false) {
let array = asArray(maybeArray);
Expand All @@ -14,7 +14,7 @@ export function next(currentValue, maybeArray, useDeepEqual = false) {
return;
}

return currentIndex === lastIndex ? currentValue : emberArray(array).objectAt(currentIndex + 1);
return currentIndex === lastIndex ? currentValue : arrayAt(array, currentIndex + 1);
}

export default helper(function(params) {
Expand Down
5 changes: 3 additions & 2 deletions addon/helpers/object-at.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { helper } from '@ember/component/helper';
import { A, isArray as isEmberArray } from '@ember/array';
import { isArray as isEmberArray } from '@ember/array';
import arrayAt from '../utils/array-at';

export function objectAt(index, array) {
if (!isEmberArray(array)) {
Expand All @@ -8,7 +9,7 @@ export function objectAt(index, array) {

index = parseInt(index, 10);

return A(array).objectAt(index);
return arrayAt(array, index);
}

export default helper(function([index, array]) {
Expand Down
4 changes: 2 additions & 2 deletions addon/helpers/previous.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { helper } from '@ember/component/helper';
import getIndex from '../utils/get-index';
import { isEmpty } from '@ember/utils';
import { A as emberArray } from '@ember/array';
import getValueArrayAndUseDeepEqualFromParams from '../-private/get-value-array-and-use-deep-equal-from-params';
import arrayAt from '../utils/array-at';

export function previous(currentValue, array, useDeepEqual = false) {
let currentIndex = getIndex(array, currentValue, useDeepEqual);
Expand All @@ -11,7 +11,7 @@ export function previous(currentValue, array, useDeepEqual = false) {
return;
}

return currentIndex === 0 ? currentValue : emberArray(array).objectAt(currentIndex - 1);
return currentIndex === 0 ? currentValue : arrayAt(array, currentIndex - 1);
}

export default helper(function(params) {
Expand Down
4 changes: 2 additions & 2 deletions addon/helpers/reverse.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { helper } from '@ember/component/helper';
import { A as emberArray, isArray as isEmberArray } from '@ember/array';
import { isArray as isEmberArray } from '@ember/array';

export function reverse([array]) {
if (!isEmberArray(array)) {
return [array];
}

return emberArray(array).slice(0).reverse();
return array.slice(0).reverse();
}

export default helper(reverse);
12 changes: 4 additions & 8 deletions addon/helpers/without.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { helper } from '@ember/component/helper';
import { A as emberArray, isArray as isEmberArray } from '@ember/array';

function contains(needle, haystack) {
return emberArray(haystack).includes(needle);
}
import { isArray as isEmberArray } from '@ember/array';

export function without(needle, haystack) {
if (!isEmberArray(haystack)) {
Expand All @@ -12,13 +8,13 @@ export function without(needle, haystack) {

if (isEmberArray(needle) && needle.length) {
return haystack.reduce((acc, val) => {
return contains(val, needle) ? acc : acc.concat(val);
return needle.includes(val) ? acc : acc.concat(val);
}, []);
}

return emberArray(haystack).without(needle);
return haystack.filter(val => val !== needle);
}

export default helper(function([needle, haystack]) {
return without(needle, haystack);
});
});
12 changes: 12 additions & 0 deletions addon/utils/array-at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Array.at isn't universally supported yet https://caniuse.com/?search=Array.at
// from https://github.com/tc39/proposal-relative-indexing-method#polyfill
export default function arrayAt(array, n) {
// ToInteger() abstract op
n = Math.trunc(n) || 0;
// Allow negative indexing from the end
if (n < 0) n += array.length;
// OOB access is guaranteed to return undefined
if (n < 0 || n >= array.length) return undefined;
// Otherwise, this is just normal property access
return array[n];
}
2 changes: 1 addition & 1 deletion tests/integration/helpers/includes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module('Integration | Helper | {{includes}}', function(hooks) {

assert.dom().hasText('true', 'should render true');

run(() => this.get('wishlist').removeObject(games[0]));
run(() => this.set('wishlist', games.slice(1)));

assert.dom().hasText('false', 'should render false');

Expand Down
19 changes: 19 additions & 0 deletions tests/unit/utils/array-at-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import arrayAt from 'ember-composable-helpers/utils/array-at';
import { module, test } from 'qunit';

module('Unit | Utility | array-at', function() {

test('it works', function(assert) {
const val = {};
assert.strictEqual(arrayAt([{}, {}, val], 2), val);
});

test('it works for negative values', function(assert) {
const val = {};
assert.strictEqual(arrayAt([{}, val, {}], -2), val);
});

test('missing value return undefined', function(assert) {
assert.deepEqual(arrayAt([], 3), undefined);
});
});