Mangler.js transform, deflate and inflate

It is time for some new features, fixes and breaking changes to my Mangler.js object processing library. As I started using it in my projects myself and it seems pretty stable, it might even see an official release soon.


Formerly known as .toCase, it transforms strings between snake_case, camelCase and TitleCase. It no longer takes an array of tokens as input, but can transform a whole array of strings, or even the property names of a passed object. Option properties have also been renamed to transform for all functions that supported toCase.

// By default, auto-detects source transform
// These both return "FooBarBaz"
Mangler.transform('foo_bar_baz', 'title');
Mangler.transform('foo_barBaz', 'title');

// You can specify source and target transforms to avoid guess-work
// This example ignores the primary snake_case detection
// Returns "Foo_barBaz"
Mangler.transform('foo_barBaz', { from: 'camel', to: 'title' });

// When processing multiple strings, you can set an ignore list
var o = { one: 1, two: 2, three: 3 };
Mangler.transform(o, { to: 'upper_', ignore: ['two'] });
// returns o, which is now: { ONE: 1, two: 2, THREE: 3 }

It supports the following transformations:

  • "_": The default sNake_cAse transformation.
  • "lower_": all_lowercase_snake_case
  • "title": CapitalisedTitleCase
  • "camel": usualCamelCase
  • ".": dot.delimited.words


Formerly called .flatten(), this functionality is not new. It takes a JavaScript object and completely squashes its nested objects and arrays into a single level.

// Example object:

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

When called without options:



	user_id: "jsmith",
	user_name: "John Smith",
	user_roles_0: "Admin",
	user_roles_1: "User"

Optionally, it can be limited to only take a certain number of iterations from the root.

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


	user_id: "jsmith",
	user_name: "John Smith",
	user_roles: ["Admin", "User"],

By default it generates snake_case property names for deflated items, specify the transform option to use one of the above text transformations.

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


	userId: "jsmith",
	userName: "John Smith",
	userRoles0: "Admin",
	userRoles1: "User"

Object properties will be deflated in-place, clone the object first if you need the original structure, or reverse it with…


Reverses the effect of deflate. When called on the first two results above without options, it restores the original object.


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

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


	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.


As usual, you can call all the above methods on Mangler() object collections as well to apply them to all items, just leave out the first parameter.

If you have any feature suggestions, please let me know in the comments below.

Share Button
Tagged with: ,

Leave a Reply