/
githubmirror
/
immutable-js
Обзор
Документация
Войти
/
githubmirror
/
immutable-js
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
v4.3.3
src/functional/updateIn.js
68 строк
2 KB
Lee Byron
Upgrade Prettier (#1817)
18 июн 2021, 02:30
Не верифицирован
18 июн 2021, 02:30
c02d769
Код
Авторство
О чём код?
import { isImmutable } from '../predicates/isImmutable'; import coerceKeyPath from '../utils/coerceKeyPath'; import isDataStructure from '../utils/isDataStructure'; import quoteString from '../utils/quoteString'; import { NOT_SET } from '../TrieUtils'; import { emptyMap } from '../Map'; import { get } from './get'; import { remove } from './remove'; import { set } from './set'; export function updateIn(collection, keyPath, notSetValue, updater) { if (!updater) { updater = notSetValue; notSetValue = undefined; } const updatedValue = updateInDeeply( isImmutable(collection), collection, coerceKeyPath(keyPath), 0, notSetValue, updater ); return updatedValue === NOT_SET ? notSetValue : updatedValue; } function updateInDeeply( inImmutable, existing, keyPath, i, notSetValue, updater ) { const wasNotSet = existing === NOT_SET; if (i === keyPath.length) { const existingValue = wasNotSet ? notSetValue : existing; const newValue = updater(existingValue); return newValue === existingValue ? existing : newValue; } if (!wasNotSet && !isDataStructure(existing)) { throw new TypeError( 'Cannot update within non-data-structure value in path [' + keyPath.slice(0, i).map(quoteString) + ']: ' + existing ); } const key = keyPath[i]; const nextExisting = wasNotSet ? NOT_SET : get(existing, key, NOT_SET); const nextUpdated = updateInDeeply( nextExisting === NOT_SET ? inImmutable : isImmutable(nextExisting), nextExisting, keyPath, i + 1, notSetValue, updater ); return nextUpdated === nextExisting ? existing : nextUpdated === NOT_SET ? remove(existing, key) : set( wasNotSet ? (inImmutable ? emptyMap() : {}) : existing, key, nextUpdated ); }