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

fix: make filterBy and mapBy generic, allow any function for pick #32

Merged
merged 2 commits into from
Oct 30, 2024
Merged
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: 5 additions & 1 deletion ember-composable-helpers/src/helpers/filter-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { get } from '@ember/object';
import isEqual from '../utils/is-equal.ts';
import asArray from '../utils/as-array.ts';

export function filterBy<T>([byPath, value, array]: [string, T | T[] | undefined, T[]]) {
export function filterBy<T extends object, K extends keyof T>([
byPath,
value,
array,
]: [K, T[K] | T[] | undefined, T[]]) {

let isPresent = true;
if (!isEmberArray(array) && isEmberArray(value)) {
Expand Down
4 changes: 2 additions & 2 deletions ember-composable-helpers/src/helpers/map-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { get } from '@ember/object';
import { isEmpty } from '@ember/utils';
import asArray from '../utils/as-array.ts';

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

return asArray(array).map(item => get(item as object, byPath as string));
return asArray(array).map(item => get(item, byPath));
}

export default helper(mapBy);
2 changes: 1 addition & 1 deletion ember-composable-helpers/src/helpers/pick.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { helper } from '@ember/component/helper';
import { get } from '@ember/object';

export function pick([path, action]: [string, (value: unknown) => void]) {
export function pick([path, action]: [string, (...args: any[]) => void]) {
return function(event: Event) {
let value = get(event, path);

Expand Down
Loading