npm.io
0.1.0 • Published 9 years ago

spreadux

Licence
MIT
Version
0.1.0
Deps
0
Vulns
0
Weekly
0

Build Status

spreadux

Helps to avoid nightmares like this:

const entities = {
	...this.state.entities,
	ws: {
		...this.state.entities.users,
		[id]: {
			...this.state.entities.users[id],
			isChanging: isChanging
		}
	}
}

How it works

Consider a typical normalized data.

const state = {
	result: [1],
	entities: {
		subjects: {
			1: {
				id: 1,
				title: 'Example',
				path: '/example',
				isActive: true
			}
		}
	}
};

We want easily change any fields in any subject. So, spread helper will be:

const subjectSpread = spreadux(['entities', 'subjects', '[id]']);

Now, we can apply that helper to our normalized data:

const newState = subjectSpread(state, { isActive: false }, 1);

Result:

{
	result: [1],
	entities: {
		subjects: {
			1: {
				id: 1,
				title: 'Example',
				path: '/example',
				isActive: false
			}
		}
	}
}

Dependencies

None.

Keywords