mobily/ts-belt

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

Opened this issue · 5 comments

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! :)

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
	),
)

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

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

Ok, thanks. Feel free to close this.

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)