Skip to content

Commit

Permalink
fix: avoid to array mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
wozny1989 committed Jun 24, 2024
1 parent 9d8001d commit 96dd3cf
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 17 deletions.
10 changes: 5 additions & 5 deletions ember-composable-helpers/src/helpers/find-by.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { helper } from '@ember/component/helper';
import { isEmpty } from '@ember/utils';
import { A as emberArray } from '@ember/array';
import asArray from '../utils/as-array';
import { helper } from "@ember/component/helper";
import { isEmpty } from "@ember/utils";
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) => get(item, byPath) === value);
}

export default helper(findBy);
3 changes: 1 addition & 2 deletions ember-composable-helpers/src/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,7 +8,7 @@ export function includes(needleOrNeedles, haystack) {
}

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

return asArray(needles).every((needle) => {
return haystackAsEmberArray.includes(needle);
Expand Down
3 changes: 1 addition & 2 deletions ember-composable-helpers/src/helpers/next.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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';

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

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

export default helper(function(params) {
Expand Down
3 changes: 1 addition & 2 deletions ember-composable-helpers/src/helpers/previous.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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';

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

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

export default helper(function(params) {
Expand Down
7 changes: 3 additions & 4 deletions ember-composable-helpers/src/utils/get-index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { A as emberArray } from '@ember/array';
import isEqual from '../utils/is-equal';
import isEqual from "../utils/is-equal";

export default function getIndex(array, currentValue, useDeepEqual) {
let needle = currentValue;

if (useDeepEqual) {
needle = emberArray(array).find((object) => {
needle = array.find((object) => {
return isEqual(object, currentValue, useDeepEqual);
});
}

let index = emberArray(array).indexOf(needle);
let index = (array || []).indexOf(needle);

return index >= 0 ? index : null;
}
1 change: 1 addition & 0 deletions test-app/tests/integration/helpers/find-by-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module('Integration | Helper | {{find-by}}', function(hooks) {
`);

assert.dom().hasText('b', 'b is shown');
assert.true(!this.array.removeObject, 'should not extend the array with additional functions')
});

test('It recomputes the filter if array changes', async function(assert) {
Expand Down
10 changes: 8 additions & 2 deletions test-app/tests/integration/helpers/includes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ module('Integration | Helper | {{includes}}', function(hooks) {
await render(hbs`{{includes this.selectedGame this.wishlist}}`);

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

run(() => this.get('wishlist').removeObject(games[0]));
assert.true(!this.wishlist.removeObject, 'should not extend the array with additional functions')

run(() =>
this.set(
"wishlist",
this.wishlist.filter((game) => game !== games[0])
)
);

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

Expand Down

0 comments on commit 96dd3cf

Please sign in to comment.