Skip to content

Commit

Permalink
fix: avoid to array mutation (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
wozny1989 authored Jul 10, 2024
1 parent 34a5d4b commit 81ad5b1
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 23 deletions.
4 changes: 2 additions & 2 deletions ember-composable-helpers/src/helpers/find-by.ts
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 { get } from "@ember/object";
import asArray from '../utils/as-array.ts';

export function findBy<T>([byPath, value, array]: [keyof T, T[keyof T], T[]]) {
if (isEmpty(byPath)) {
return [];
}

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

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

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

return asArray(needles as T[]).every((needle) => {
return haystackAsEmberArray.includes(needle);
Expand Down
3 changes: 1 addition & 2 deletions ember-composable-helpers/src/helpers/next.ts
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.ts';
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.ts';
import asArray from '../utils/as-array.ts';

Expand All @@ -14,7 +13,7 @@ export function next<T>(currentValue: T, maybeArray: T[], useDeepEqual = false)
return;
}

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

export default helper(function<T>(params: [T, boolean | T[], T[]?]) {
Expand Down
16 changes: 8 additions & 8 deletions ember-composable-helpers/src/helpers/previous.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { helper } from '@ember/component/helper';
import getIndex from '../utils/get-index.ts';
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.ts';
import { helper } from "@ember/component/helper";
import getIndex from "../utils/get-index.ts";
import { isEmpty } from "@ember/utils";
import getValueArrayAndUseDeepEqualFromParams from "../-private/get-value-array-and-use-deep-equal-from-params.ts";

export function previous<T>(currentValue: T, array: T[], useDeepEqual = false) {
let currentIndex = getIndex(array, currentValue, useDeepEqual) as number;
Expand All @@ -11,11 +10,12 @@ export function previous<T>(currentValue: T, array: T[], useDeepEqual = false) {
return;
}

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

export default helper(function<T>(params: [T, boolean | T[], T[]?]) {
let { currentValue, array, useDeepEqual } = getValueArrayAndUseDeepEqualFromParams(params);
export default helper(function <T>(params: [T, boolean | T[], T[]?]) {
let { currentValue, array, useDeepEqual } =
getValueArrayAndUseDeepEqualFromParams(params);

return previous(currentValue, array as T[], useDeepEqual);
});
13 changes: 8 additions & 5 deletions ember-composable-helpers/src/utils/get-index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { A as emberArray } from '@ember/array';
import isEqual from '../utils/is-equal.ts';
import isEqual from "../utils/is-equal.ts";

export default function getIndex<T>(array: T[], currentValue: T, useDeepEqual: boolean) {
export default function getIndex<T>(
array: T[],
currentValue: T,
useDeepEqual: boolean
) {
let needle = currentValue;

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

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

return index >= 0 ? index : null;
}
1 change: 1 addition & 0 deletions ember-composable-helpers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"environment": ["ember-loose", "ember-template-imports"]
},
"compilerOptions": {
"target": "es2022",
"allowJs": true,
"declarationDir": "declarations",
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ module('Integration | Helper | {{includes}}', function(hooks) {

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

run(() => this.get('wishlist').removeObject(games[0]));
assert.dom().hasText('true', 'should render true');
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ module('Integration | Helper | {{includes}}', function(hooks) {

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

run(() => this.get('wishlist').removeObject(games[0]));
assert.dom().hasText('true', 'should render true');
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
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 81ad5b1

Please sign in to comment.