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.

Transform

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.
  • "upper_": ALL_UPPERCASE_SNAKE_CASE
  • "lower_": all_lowercase_snake_case
  • "title": CapitalisedTitleCase
  • "camel": usualCamelCase
  • ".": dot.delimited.words

Deflate

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:

Mangler.deflate(o);

Result:

{
	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 });

Result:

{
	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' });

Result:

{
	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…

Inflate

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

Mangler.inflate(o);

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 });

Result:

{
	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.

Conclusion

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
Posted in Blog Tagged with: ,

Leave a Reply