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

feat(invert): add invert #125

Merged
merged 9 commits into from
Jul 5, 2024
Merged

feat(invert): add invert #125

merged 9 commits into from
Jul 5, 2024

Conversation

wondonghwi
Copy link
Contributor

close: #116

I added an invert function to the es-toolkit library. The invert function takes an object as input and returns a new object where the keys and values are swapped. This functionality is available in lodash but was not present in es-toolkit.

The invert function is particularly useful when you need to reverse the mapping of an object. For example, given an object with key-value pairs, the invert function will produce a new object where the original values become the keys and the original keys become the values.

스크린샷 2024-07-04 오전 1 45 37

For reference, you can see the lodash invert function here.

@wondonghwi wondonghwi requested a review from raon0211 as a code owner July 3, 2024 17:31
Copy link

vercel bot commented Jul 3, 2024

@wondonghwi is attempting to deploy a commit to the Toss Team on Vercel.

A member of the Team first needs to authorize it.

invertByLodash(object);
});

bench('Custom invert', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

custom is good, but what do you think about doing it with es-toolkit/invert ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hautest
Thank you for the good comments, I've made the corrections more clearly!

* invert({ a: Symbol('sym1'), b: Symbol('sym2') }); // { [Symbol('sym1')]: 'a', [Symbol('sym2')]: 'b' }
*/

export function invert<K extends string | number | symbol, V extends string | number | symbol>(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can use PropertyKey.
They're the same, but I think it's more readable.

Suggested change
export function invert<K extends string | number | symbol, V extends string | number | symbol>(
export function invert<K extends PropertyKey, V extends PropertyKey>(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jgjgill
I've made the modifications. Thank you for the good comments. I think it's more readable

src/object/invert.ts Outdated Show resolved Hide resolved
@raon0211 raon0211 merged commit 1918eec into toss:main Jul 5, 2024
6 of 7 checks passed
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.

Support for invert
4 participants