Created
January 25, 2017 11:57
-
-
Save alexanderwallin/59d7f81ef6bbe17bde0bb607714c9e6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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