Skip to content

Added Record toEntries, fromEntries #1552

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

Merged
merged 8 commits into from
Dec 14, 2021
Merged

Conversation

bravely
Copy link
Contributor

@bravely bravely commented Jul 31, 2021

  • Added Record.toEntries and Record.fromEntries, to facilitate a common need of folks coming over from 'normal' JS.

@mlegenhausen
Copy link
Collaborator

toEntries is toReadonlyArray https://github.com/gcanti/fp-ts/blob/master/src/ReadonlyRecord.ts#L136-L138

fromEntries is fromFoldable

import * as RR from 'fp-ts/ReadonlyRecord'
import * as RA from 'fp-ts/ReadonlyArray'
import * as S from 'fp-ts/Semigroup'

RR.fromFoldable(S.last<string>(), RA.Foldable)([['a', 'b'], ['c', 'd'], ['a', 'e']])
// { a: 'e', c: 'd' }

@bravely
Copy link
Contributor Author

bravely commented Aug 2, 2021

@mlegenhausen Didn't realize toEntries was toArray/toReadonlyArray, that works.

The other though, I feel like there should be a convenience for, especially considering we could easily auto-type it. Using fromEntries would also imply exactly how it works- that last-instance-of-key wins, etc.

Assuming we did go with that name, I also think having an alias for toArray/toReadonlyArray of toEntries or entries would be helpful.

@mlegenhausen
Copy link
Collaborator

@bravely sorry overlooked that you just defined an alias for fromFoldable.

If you could adjust your PR so the toEntries and fromEntries are defined in ReadonlyRecord and just reexported in Record the last decision lies by @gcanti.

IMHO it would be a good addition especially for people who are new fp-ts programmers and for me RR.fromFoldable(S.last<A>(), RA.Foldable) is the most used configuration.

@bravely
Copy link
Contributor Author

bravely commented Aug 3, 2021

@mlegenhausen I appreciate the support! I matched the feature in ReadonlyRecord, however I couldn't simply alias the result because of the nature of the readonly parameters. So I made them effectively match, with ReadonlyRecord creating a ReadonlyArray and Record creating an Array.

@mlegenhausen
Copy link
Collaborator

Looks good for me could you please add @since 2.12.0 to your comments?

@bravely
Copy link
Contributor Author

bravely commented Sep 26, 2021

@mlegenhausen Apologies for how long it took, but I've added those now.

@mlegenhausen
Copy link
Collaborator

@bravely please check the CI result. In this case you need to run npm run fix-prettier.

@bravely
Copy link
Contributor Author

bravely commented Sep 30, 2021

@mlegenhausen Should be good to go now!

@steida
Copy link
Contributor

steida commented Nov 20, 2021

I just realized I need such helpers. @gcanti Can you please merge it? Thank you.

@gcanti gcanti added this to the 2.12 milestone Nov 22, 2021
@gcanti gcanti changed the base branch from master to 2.12 November 22, 2021 10:30
@@ -29,6 +30,8 @@ import { TraversableWithIndex1 } from './TraversableWithIndex'
import { Unfoldable, Unfoldable1 } from './Unfoldable'
import { PipeableWilt1, PipeableWither1, wiltDefault, Witherable1, witherDefault } from './Witherable'

type Semigroup<A> = Se.Semigroup<A>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bravely I believe this can be replaced by

import Semigroup = Se.Semigroup

src/Record.ts Outdated
@@ -30,6 +31,8 @@ import { TraversableWithIndex1 } from './TraversableWithIndex'
import { Unfoldable, Unfoldable1 } from './Unfoldable'
import { PipeableWilt1, PipeableWither1, wiltDefault, Witherable1, witherDefault } from './Witherable'

type Semigroup<A> = Se.Semigroup<A>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem

Verified

This commit was signed with the committer’s verified signature.
equirosa Eduardo Quirós
@bravely
Copy link
Contributor Author

bravely commented Dec 14, 2021

@gcanti Updated!

@gcanti gcanti merged commit a34667f into gcanti:2.12 Dec 14, 2021
@gcanti
Copy link
Owner

gcanti commented Dec 14, 2021

Thank you @bravely 👍

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

Successfully merging this pull request may close these issues.

None yet

4 participants