-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Could we add a mapValues
implementation into the collection?
#8
Comments
Ok, I see the use case. Actually it's relatively trivial to implement with something like this: import { mapLimit, asyncWrap } from 'modern-async'
async function mapValuesLimit (obj, iteratee, queueOrConcurrency) {
iteratee = asyncWrap(iteratee)
return Object.fromEntries(await mapLimit(Object.entries(obj), async ([key, val]) => {
const nval = await iteratee(val, key, obj)
return [key, nval]
}))
}
async function mapValuesSeries(obj, iteratee) {
return mapValuesLimit(obj, iteratee, 1)
}
async function mapValues(obj, iteratee) {
return mapValuesLimit(obj, iteratee, Number.POSITIVE_INFINITY)
} I could consider adding it but I'm a bit reluctant. The problem is that modern-async does not actually provide any helpers regarding maps/objects. It only handles iterable/async iterables. If we wanted to provide helpers for object it would be necessary to think a lot more about it and consider adding a complete set of meaningful operations instead of just one (probably looking at libraries like lodash). |
Yeah I was actually a bit surprised that there weren't methods for dealing with objects, but I can understand if the idea behind modern-async was to provide a solid foundation for async code and allow consumers to expand the toolset locally. If that is the intention though, the README phrasing might need to be updated to set the correct expectations:
(🤞 that doesn't come across as edgy) |
No no, I think this is a good idea to expand the library in order to provide object-related helpers. It's just that there will be a need to find the time to do it. So it might come, but not soon. (Except if someone else want to do the job of identifying every useful functions that would be necessary, implementing them all and testing everything.) |
I was also excited to find this library, as the TS types on In the meantime, I am using a little utils file that uses lodash (which I already have installed) and this library. Posting here in case others find it useful import { asyncMap, Queue } from 'modern-async';
import { toPairs, fromPairs } from 'lodash-es';
type RecordKey = string | number | symbol;
export async function asyncMapKeys<OK extends RecordKey, OV, MK extends RecordKey>(
iterableObj: Record<OK, OV>,
iteratee: (value: OV, key: OK) => Promise<MK> | MK,
queueOrConcurrency?: Queue | number,
): Promise<Record<MK, OV>> {
const objAsPairs: Array<[OK, OV]> = toPairs(iterableObj) as any;
const mappedPairs = await asyncMap(objAsPairs, async ([key, value]) => {
return [await iteratee(value, key), value] as [MK, OV];
}, queueOrConcurrency);
return fromPairs(mappedPairs) as Record<MK, OV>;
}
export async function asyncMapValues<OK extends RecordKey, OV, MV>(
iterableObj: Record<OK, OV>,
iteratee: (value: OV, key: OK) => Promise<MV> | MV,
queueOrConcurrency?: Queue | number,
): Promise<Record<OK, MV>> {
const objAsPairs: Array<[OK, OV]> = toPairs(iterableObj) as any;
const mappedPairs = await asyncMap(objAsPairs, async ([key, value]) => {
return [key, await iteratee(value, key)] as [OK, MV];
}, queueOrConcurrency);
return fromPairs(mappedPairs) as Record<OK, MV>;
} |
Please explain the feature or improvement you would like:
I would love to see the addition of
mapValues
to the collection. This method would asynchronously transform the values of an object by applying the iteratee to each.Please describe the use case where you would need that feature (the general situation or type of program where that would be helpful):
I often use records to organize my data, such as a record of user IDs paired with an access token:
.mapValues
would be useful here if I'd like to make a request for each user using their relative access-key:This would be opposed to the manual implementation:
If you know another library similar to this one that already propose that feature please provide a link:
Async's mapValues
The text was updated successfully, but these errors were encountered: