/
githubmirror
/
immutable-js
Обзор
Документация
Войти
/
githubmirror
/
immutable-js
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
v4.3.2
src/methods/merge.js
48 строк
1 KB
Lee Byron
Upgrade Prettier (#1817)
18 июн 2021, 02:30
Не верифицирован
18 июн 2021, 02:30
c02d769
Код
Авторство
О чём код?
import { KeyedCollection } from '../Collection'; import { NOT_SET } from '../TrieUtils'; import { update } from '../functional/update'; export function merge(...iters) { return mergeIntoKeyedWith(this, iters); } export function mergeWith(merger, ...iters) { if (typeof merger !== 'function') { throw new TypeError('Invalid merger function: ' + merger); } return mergeIntoKeyedWith(this, iters, merger); } function mergeIntoKeyedWith(collection, collections, merger) { const iters = []; for (let ii = 0; ii < collections.length; ii++) { const collection = KeyedCollection(collections[ii]); if (collection.size !== 0) { iters.push(collection); } } if (iters.length === 0) { return collection; } if ( collection.toSeq().size === 0 && !collection.__ownerID && iters.length === 1 ) { return collection.constructor(iters[0]); } return collection.withMutations(collection => { const mergeIntoCollection = merger ? (value, key) => { update(collection, key, NOT_SET, oldVal => oldVal === NOT_SET ? value : merger(oldVal, value, key) ); } : (value, key) => { collection.set(key, value); }; for (let ii = 0; ii < iters.length; ii++) { iters[ii].forEach(mergeIntoCollection); } }); }