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

What is the recommended way of testing the value in an object? #80

Open
tibotiber opened this issue May 17, 2023 · 5 comments
Open

What is the recommended way of testing the value in an object? #80

tibotiber opened this issue May 17, 2023 · 5 comments

Comments

@tibotiber
Copy link

When using A.find/filter/reject, it is often required to do things like item => item.key === 'value'. Is there a better way to write such tests with ts-belt? Something similar to propEq in ramda.

And while i'm here, thanks for this amazing lib! :)

@bartektelec
Copy link

I think you could combine D.get with F.equals if you really need to. Personally I try to keep it as simple as possible and just write the cb functions like in the example you provided, but there are cases where D.get makes sense - it returns an Option so in case your object (dict) for some reason doesn't have the property in runtime then you can add some additional logic to handle the None option.

Doing the unsafe get is just more code and doesn't bring you any benefits that I know of

pipe(
	collection,
	A.filter(
		flow(
			D.getUnsafe('key'),
			F.equals('value')
		)
	),
	A.reject(
		d => d.id === 0
	),
)

@tibotiber
Copy link
Author

Yeah, it is way easier to write the callback. Would there be any value in having something like propEq in your opinion?

@bartektelec
Copy link

I'm not sure if by testing you meant writing tests, but Jest and Vitest have a matcher for it toHaveProperty.

Personally I find very little value in grabbing properties by a string, because it only works at root level of the object and if I want to grab a nested value I will again use a predicate.
Maybe there is something that I am missing

@tibotiber
Copy link
Author

Ok, thanks. Feel free to close this.

@JUSTIVE
Copy link

JUSTIVE commented Jan 23, 2024

for the code above, our team uses like

pipe(
  collection,
  A.filter(({key})=>key === 'value' ),
  A.reject(({id})=>id === 0)
)

In my opinion, sometimes, using just pure js' operator gives more readability, since pipe and flows add more unnecessary depth to the code. (hope the pipeline operator comes to the js)

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

3 participants