objdeepset

1.0.3 • Public • Published

objDeepSet

install size dependency count

objDeepSet is a javascript immutable object deep setter.

  • 🚀 Lightweight.
  • ⚪️ Zero dependencies.
  • 💫 Works great with Redux.

⬇️ Import

const objDeepSet = require("objDeepSet");

🧭 Usage:

objDeepSet(object, key, value, options);
  • object (object) The original object you want to modify. It will be cloned internally unless you set mutate: true on options.
  • key (string | array) Key that will be modified. Examples:
    • "user"
    • "user.name"
    • ["user", "name"]
    • "" (this will point to the root of the object)
  • options (object)
    • merge (boolean) Default false. Merge (true) or replace (false) the object with the value.
    • mutate (boolean) Default false. Mutate (true) or clone (false) the original object.

Returns The new or modified object.


🔮 Examples:

let car: {
	id: "V-1234-AB",
	color: "Black",
	wheels: {
		"1": {
			status: "ok"
		},
		"2". {
			status: "ok"
		}
		"3". {
			status: "damaged",
			damage: {
				priority: 10,
				description: "puncture",
				needsReparation: true
			}
		}
		"4". {
			status: "ok"
		}
	}
}

let newObj = objDeepSet(car, "wheels.3.damage.priority", 9);


let dog = {
	name: "Woof",
	color: "brown",
	age: 5,
	legs: {
		"topLeft": "ok",
		"topRight": "ok",
		"bottomLeft": "ok",
		"bottomRight": "ok",
	}
};



let newDog = objDeepSet(dog, "legs.bottomLeft", "injury");
/*
OUTPUT: 

	{
		name: 'Woof',
		color: 'brown',
		age: 5,
		legs: {
			topLeft: 'ok',
			topRight: 'ok',
			bottomLeft: 'injury',
			bottomRight: 'ok'
		}
	}
*/



let newDog2 = objDeepSet(dog, "", {
	name: "Mike",
	color: "red",
});
/*
OUTPUT: 

	{
		name: 'Mike',
		color: 'red',
	}
*/



let newDog3 = objDeepSet(dog, "legs.bottomLeft", {
	feathers: 1564,
	wings: {
		left: 'ok',
		right: 'ok',
	}
});
console.log( newDog3 );
/*
OUTPUT: 

	{
		name: 'Woof',
		color: 'brown',
		age: 5,
		legs: {
			topLeft: 'ok',
			topRight: 'ok',
			bottomLeft: { feathers: 1564, wings: [Object] },
			bottomRight: 'ok'
		}
	}

*/

🌌 Redux implementation:

your_reducer.js


const userReducer = (
	state = {},
	action
) => {
	
	switch (action.type) {
		
		case "USER_SET": {
			
			const {merge = true} = action.options ?? {};
			
			let newstate = objSet(state, action.key, action.value, {
				merge,
			});
			
			return newstate;
		};
		
		default: return state;
		
	};
		

How you dispatch that action

// Login example
dispatch({type: "USER_SET", key: "username", value: "Mike"});

// Logout example
dispatch({type: "USER_SET", key: "", value: {}, options: {merge: false} });

Package Sidebar

Install

npm i objdeepset

Weekly Downloads

7

Version

1.0.3

License

ISC

Unpacked Size

7.15 kB

Total Files

3

Last publish

Collaborators

  • icaruk