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

Turns nested objects and arrays into a flat object.

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

Returns

Returns the deflated object.

Options

Property Type Default Description
limit Number 0 Limits the depth of processing. Defaults to 0 to completely flatten the object.
transform String '_' Any transformation supported by the to option of Mangler.transform() to change the case of the flattened property names to. Defaults to snake_case.

The Mangler.deflate() method flattens all nested objects and arrays of the object passed.

Example

o = {
	name: 'John Smith',
	user: {
		name: 'admin',
		role: 'Administrator',
		groups: ['root', 'all']
	}
};

Mangler.deflate(o);

/*
	o = {
		name: 'John Smith',
		user_name: 'admin',
		user_role: 'Administrator',
		user_groups_0: 'root',
		user_groups_1: 'all'
	}
*/

Changing the case of property names

The transform option lets you change how flattened property names are put together. See the Mangler.transform() function's to option for available types.

Note that it will only apply to newly created property names, existing properties will not be renamed. This behaviour can be seen on the top-level name property in the example below, which stays all lowercase:

o = {
	name: 'John Smith',
	user: {
		name: 'admin',
		role: 'Administrator',
		groups: ['root', 'all']
	}
};

Mangler.deflate(o, { transform: 'title' });

/*
	o = {
		name: 'John Smith',
		UserName: 'admin',
		UserRole: 'Administrator',
		UserGroups0: 'root',
		UserGroups1: 'all'
	};
*/

Limiting the depth of processing

By default the object is completely flattened. You can limit the number of iterations by the limit option. Using the same data as above, you might not want to flatten the groups array:

o = {
	name: 'John Smith',
	user: {
		name: 'admin',
		role: 'Administrator',
		groups: ['root', 'all']
	}
};

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

/*
	o = {
		name: 'John Smith',
		user_name: 'admin',
		user_role: 'Administrator',
		user_groups: ['root', 'all']
	}
*/

In the example above, processing stops after one level is unwrapped, leaving the groups array intact.

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