Skip to content

Commit

Permalink
Export setByKey function (#296)
Browse files Browse the repository at this point in the history
* Export `setByKey` function

* eslint sort imports
  • Loading branch information
yhdgms1 committed Apr 24, 2024
1 parent 29ab745 commit aeacd88
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion deep-map/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { WritableAtom } from '../atom/index.js'
import type { AllPaths, BaseDeepMap, FromPath } from './path.js'

export { AllPaths, BaseDeepMap, FromPath, getPath, setPath } from './path.js'
export { AllPaths, BaseDeepMap, FromPath, getPath, setByKey, setPath } from './path.js'

export type DeepMapStore<T extends BaseDeepMap> = {
/**
Expand Down
2 changes: 1 addition & 1 deletion deep-map/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { atom } from '../atom/index.js'
import { getPath, setPath } from './path.js'

export { getPath, setPath } from './path.js'
export { getPath, setByKey, setPath } from './path.js'

export function deepMap(initial = {}) {
let $deepMap = atom(initial)
Expand Down
22 changes: 22 additions & 0 deletions deep-map/path.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,25 @@ export function setPath<T extends BaseDeepMap, K extends AllPaths<T>>(
path: K,
value: FromPath<T, K>
): T

/**
* Set a deep value by path. Initialized arrays with `undefined`
* if you set arbitrary length.
*
* ```
* import { setByKey } from 'nanostores'
*
* setByKey({ a: { b: { c: [] } } }, ['a', 'b', 'c', 1], 'hey')
* // Returns `{ a: { b: { c: [undefined, 'hey'] } } }`
* ```
*
* @param obj Any object.
* @param splittedKeys An array of keys representing the path to the value.
* @param value New value.
* @retunts The new object.
*/
export function setByKey<T extends BaseDeepMap>(
obj: T,
splittedKeys: PropertyKey[],
value: unknown
): T;
2 changes: 1 addition & 1 deletion deep-map/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function setPath(obj, path, value) {
return setByKey(obj != null ? obj : {}, getAllKeysFromPath(path), value)
}

function setByKey(obj, splittedKeys, value) {
export function setByKey(obj, splittedKeys, value) {
let key = splittedKeys[0]
ensureKey(obj, key, splittedKeys[1])
let copy = Array.isArray(obj) ? [...obj] : { ...obj }
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
DeepMapStore,
FromPath,
getPath,
setByKey,
setPath
} from './deep-map/index.js'
export { keepMount } from './keep-mount/index.js'
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { atom } from './atom/index.js'
export { clean, cleanStores } from './clean-stores/index.js'
export { batched, computed } from './computed/index.js'
export { deepMap, getPath, setPath } from './deep-map/index.js'
export { deepMap, getPath, setByKey, setPath } from './deep-map/index.js'
export { keepMount } from './keep-mount/index.js'
export {
onMount,
Expand Down

0 comments on commit aeacd88

Please sign in to comment.