Mangler.inflate() - DarthJDG/Mangler.js GitHub Wiki

Turns a flat object into nested objects and arrays.

Mangler.inflate(object[, options])
Parameter Type Default Description
object Object The object to inflate.
options Object An object containing optional method modifiers.

Returns

Returns the inflated object.

Options

Property Type Default Description
limit Number 0 Limits the depth of processing. Defaults to 0 to completely inflate the object.
transform String '_' Any transformation supported by the to option of Mangler.transform() to change the case of the inflated property names to. Defaults to snake_case.
from String '_' Any transformation supported by Mangler.tokenize() to split the property names into tokens. Defaults to snake_case.

Reverses the effect of deflate. It splits object property names into tokens and builds nested objects or arrays depending on whether the token is numeric or not.

o = {
	user_id: "jsmith",
	user_name: "John Smith",
	user_roles_0: "Admin",
	user_roles_1: "User"
};

Mangler.inflate(o);

/*
	o = {
		user: {
			id: 'jsmith',
			name: 'John Smith',
			roles: ['Admin', 'User']
		}
	}
*/

With the limit option, you can limit processing to a certain number of levels, starting from the last word of the property name:

o = {
	user_id: "jsmith",
	user_name: "John Smith",
	user_roles_0: "Admin",
	user_roles_1: "User"
};

Mangler.inflate(o, { limit: 1 });

/*
	o = {
		user: {
			id: "jsmith"
			name: "John Smith"
		},
		user_roles: ["Admin", "User"]
	}
*/

You can also use the transform option to run the resulting property names through .transform(), and the from option to change how words are detected in property names. Both of these options default to snake_case.

To process multiple objects at the same time, wrap them in a mangler object and call .inflate() on the collection.

⚠️ **GitHub.com Fallback** ⚠️