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

Search for multiple object properties in alfy.matches and alfy.inputMatches #175

Closed
kalaschnik opened this issue Dec 6, 2023 · 6 comments

Comments

@kalaschnik
Copy link
Contributor

Assume I got the follwing workflow from the README.md:

import alfy from 'alfy';

const data = await alfy.fetch('https://jsonplaceholder.typicode.com/posts');

const items = alfy.inputMatches(data, 'title').map((element) => ({
	title: element.title,
	subtitle: element.body,
	arg: element.id,
}));

alfy.output(items);

How would I search for title and the body of the response? For example, I want to be able to search for quia et suscipit and get one result (i.e., the body of first element in the array)

Is this already implemented or do I need to write my own matching function?

@kalaschnik kalaschnik changed the title How to search for multiple object properties in alfy. matches and alfy Search for multiple object properties in alfy. matches and alfy.inputMatches Dec 6, 2023
@kalaschnik kalaschnik changed the title Search for multiple object properties in alfy. matches and alfy.inputMatches Search for multiple object properties in alfy.matches and alfy.inputMatches Dec 6, 2023
@kalaschnik
Copy link
Contributor Author

kalaschnik commented Dec 7, 2023

I should have read the README.md more carefully. I made my example to work using includes as such:

import alfy from 'alfy';

const data = await alfy.fetch('https://jsonplaceholder.typicode.com/posts');

const items = alfy
  .inputMatches(
    data,
    (item, input) =>
      item.title.toLowerCase().includes(input) ||
      item.body.toLowerCase().includes(input)
  )
  .map((element) => ({
    title: element.title,
    subtitle: element.body,
    arg: element.id,
  }));

alfy.output(items);

@kalaschnik
Copy link
Contributor Author

kalaschnik commented Dec 7, 2023

In my specific example, I noticed that some properties may contain null on which toLowerCase() would fail. It makes sense to add optional chaining for such properties

import alfy from 'alfy';

const data = await alfy.fetch('https://jsonplaceholder.typicode.com/posts');

const items = alfy
  .inputMatches(
    data,
    (item, input) =>
      item.title?.toLowerCase().includes(input) ||
      item.body?.toLowerCase().includes(input)
  )
  .map((element) => ({
    title: element.title,
    subtitle: element.body,
    arg: element.id,
  }));

alfy.output(items);

@kalaschnik
Copy link
Contributor Author

Is this little example something worth adding to the README.md? Happy to propose a PR.

@sindresorhus
Copy link
Owner

The problem with supporting multiple properties like this:

const items = alfy.inputMatches(data, ['title', 'body']).map((element) => ({
	title: element.title,
	subtitle: element.body,
	arg: element.id,
}));

is that it's not immediately clear whether either or both properties have to match.

@sindresorhus
Copy link
Owner

PR welcome for adding the tip to the readme.

@kalaschnik
Copy link
Contributor Author

The problem with supporting multiple properties like this:

const items = alfy.inputMatches(data, ['title', 'body']).map((element) => ({
	title: element.title,
	subtitle: element.body,
	arg: element.id,
}));

is that it's not immediately clear whether either or both properties have to match.

Good point. I was about to propose extending your dot.prop’s getProperty function, but that feels too over-engineered in the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants