Skip to content

Instantly share code, notes, and snippets.

@alexanderwallin
Created January 25, 2017 11:57
Show Gist options
  • Save alexanderwallin/59d7f81ef6bbe17bde0bb607714c9e6f to your computer and use it in GitHub Desktop.
Save alexanderwallin/59d7f81ef6bbe17bde0bb607714c9e6f to your computer and use it in GitHub Desktop.
/* eslint new-cap: 0 */
import { Iterable } from 'immutable'
/**
* Subtracts a set of Maps from the current Map.
*/
export default function mapSubtract(targetMap, ...iterators) {
if (iters.length === 0) {
return targetMap
}
const iterableHasKeyFilter = key => iterable => {
return Iterable.isKeyed(iterable) ? iterable.has(key) : iterable.includes(key)
}
const iters = iterators.map(iter => Iterable(iter))
return targetMap.withMutations(map => {
const iterator = targetMap.keys()
let step = iterator.next()
let key
while (!step.done) {
key = step.value
const hasKey = iters.some(iterableHasKeyFilter(key))
if (hasKey) {
map.delete(key)
}
step = iterator.next()
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment