nanostores
Version:
A tiny (294 bytes) state manager for React/Preact/Vue/Svelte with many atomic tree-shakable stores
31 lines (26 loc) • 795 B
JavaScript
import { atom } from '../atom/index.js'
import { warn } from '../warn/index.js'
import { getPath, setPath } from './path.js'
export { getPath, setByKey, setPath } from './path.js'
/* @__NO_SIDE_EFFECTS__ */
export const deepMap = (initial = {}) => {
if (process.env.NODE_ENV !== 'production') {
warn(
'Move to deepmap() from @nanostores/deepmap. ' +
'deepmap() will be removed in 2.0.'
)
}
let $deepMap = atom(initial)
$deepMap.setKey = (key, value) => {
if (getPath($deepMap.value, key) !== value) {
let oldValue = $deepMap.value
$deepMap.value = setPath($deepMap.value, key, value)
$deepMap.notify(oldValue, key)
}
}
return $deepMap
}
export function getKey(store, key) {
let value = store.get()
return getPath(value, key)
}