MCPcopy Create free account
hub / github.com/instantdb/instant / insertInMutative

Function insertInMutative

client/packages/core/src/utils/object.js:100–123  ·  view source on GitHub ↗
(obj, path, value)

Source from the content-addressed store, hash-verified

98 * - for arrays: inserts the value at the specified index, instead of replacing it
99 */
100export function insertInMutative(obj, path, value) {
101 if (!obj) {
102 return;
103 }
104 if (path.length === 0) {
105 return;
106 }
107
108 let current = obj || {};
109 for (let i = 0; i < path.length - 1; i++) {
110 const key = path[i];
111 if (!(key in current) || typeof current[key] !== 'object') {
112 current[key] = typeof path[i + 1] === 'number' ? [] : {};
113 }
114 current = current[key];
115 }
116
117 const key = path[path.length - 1];
118 if (Array.isArray(current) && typeof key === 'number') {
119 current.splice(key, 0, value);
120 } else {
121 current[key] = value;
122 }
123}
124
125export function assocInMutative(obj, path, value) {
126 if (!obj) {

Callers 2

_patchPresencePeersMethod · 0.90
object.test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected