add a Map method #20

Closed
BrianHung wants to merge 2 commits from map-method into master
BrianHung commented 2024-05-14 20:58:59 +02:00 (Migrated from github.com)

The current way to iterate through an OrderedMap and update multiple values at once while maintaining order is

const map = OrderedMap
let copy = map
map.forEach((key, value) => {
  const [newValue, newKey] = f(key, value)
  copy = copy.update(key, newValue, newKey)
})

But because update creates a new OrderedMap for each key and internally uses find each time, it's a bit cumbersome to use. map fixes this by only creating the new OrderedMap at the end.

Alternatively, if the constructor for OrderedMap was made public by modifying the lib types, this code could just easily exist outside this lib.

My specific use case is conditionally adding attrs to an existing ProseMirror schema nodes, which involves mapping and maintaining order.

Thanks!

The current way to iterate through an OrderedMap and update multiple values at once while maintaining order is ```js const map = OrderedMap let copy = map map.forEach((key, value) => { const [newValue, newKey] = f(key, value) copy = copy.update(key, newValue, newKey) }) ``` But because `update` creates a new `OrderedMap` for each key and internally uses `find` each time, it's a bit cumbersome to use. `map` fixes this by only creating the new `OrderedMap` at the end. Alternatively, if the constructor for `OrderedMap` was made public by modifying the lib types, this code could just easily exist outside this lib. My specific use case is conditionally adding attrs to an existing ProseMirror schema nodes, which involves mapping and maintaining order. Thanks!
marijnh commented 2024-05-15 10:41:06 +02:00 (Migrated from github.com)

I don't think this is a common enough pattern to warrant its own method. Since you're probably only doing the mapping once, to build up a schema, creating a new copy of the object for every property shouldn't be a big issue.

I don't think this is a common enough pattern to warrant its own method. Since you're probably only doing the mapping once, to build up a schema, creating a new copy of the object for every property shouldn't be a big issue.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
marijn/orderedmap!20
No description provided.