Licence
MIT
Version
1.0.4
Deps
0
Size
4 kB
Vulns
0
Weekly
0
DeprecatedThis package is deprecated
safe-spread
A helper function that updates only the existing keys of an object in JS, deeply merging values without adding new keys.
- Works recursively on nested objects.
- Ignores keys that don’t exist in the base object.
- Can be written in immutable mode (returns a new copy) or mutable mode (updates the original object in place).
Usage
import { safeSpread } from "safe-spread";
const base = {
a: 1,
b: 2,
c: {
d: 4,
e: 5,
},
};
const update = {
a: 10, // existing top-level key - will be updated
bNew: 99, // NOT existing in base - will be ignored
c: {
d: 40, // existing nested key - will be updated
f: 100, // NOT existing in base.c - will be ignored
},
};
const result = safeSpread(base, update);
console.log(result);
// {
// a: 10,
// b: 2,
// c: {
// d: 40,
// e: 5
// }
// }