-
Notifications
You must be signed in to change notification settings - Fork 323
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
Conversation
@wondonghwi is attempting to deploy a commit to the Toss Team on Vercel. A member of the Team first needs to authorize it. |
benchmarks/invert.bench.ts
Outdated
invertByLodash(object); | ||
}); | ||
|
||
bench('Custom invert', () => { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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!
src/object/invert.ts
Outdated
* 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>( |
There was a problem hiding this comment.
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.
export function invert<K extends string | number | symbol, V extends string | number | symbol>( | |
export function invert<K extends PropertyKey, V extends PropertyKey>( |
There was a problem hiding this comment.
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
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.
For reference, you can see the lodash invert function here.