Skip to content
Robert Biro edited this page May 26, 2014 · 18 revisions

Mangler.js documentation

Mangler.js is a JavaScript library to transform, restructure, query, index and extract parts of your objects and JSON data, while attempting to keep object references intact. Read on for a brief introduction on installation and feature highlights, or jump right to the API reference page for all methods and properties.

Installation

To use this library, all you need to do is download mangler.js from the repository, and reference it in your HTML header before any code that uses it.

<!DOCTYPE html>
<html>
<head>
    <script src="mangler.js"></script>
	
	<!-- include optional mangler.js modules -->
    <script src="mangler-natives.js"></script>
	
	<!-- include other scripts using mangler.js -->
</head>
<body>
	<!-- content -->
</body>
</html>

Basics

The library will register the Mangler global. It is an object that contains the library's mangler functions that are used to manipulate your data. It also doubles as the Mangler() function, which creates and returns a mangler object.

The mangler object is mostly just an item container which stores data in its .items[ ] array property, which is a standard javascript array that is publicly accessible and can be manipulated directly. Most mangler object methods are just wrappers around the mangler functions using .items as input and/or output. Many functions return a reference to the mangler object itself for conveniently chaining multiple operations on the same data set.

Adding items

Calling Mangler() without a parameter will return an empty mangler object, which you can manually put items into via the .add() and .push() methods. The difference between them is when called with an array parameter, .add() will add all items in the array separately, while .push() will add the array itself.

var data1 = ['A', 'B', 'C'];
var data2 = ['D', 'E', 'F'];

var m;

m = Mangler().add(data1).add(data2);
console.log(m.items);

m = Mangler().push(data1).push(data2);
console.log(m.items);

The above code will output the following:

['A', 'B', 'C', 'D', 'E', 'F']
[['A', 'B', 'C'], ['D', 'E', 'F']]

Calling Mangler() with any parameter will automatically add those items to the collection with .add().

Further examples

This is only a brief introduction. For more advanced features, explore the methods and functions on the API reference page, or have a look at the highlighted features below:

.explore(callback, path, state)
Traverses all objects and arrays.

.extract(filter, options)
Extract parts of your data with path filters.

.flatten(options)
Flatten top-level objects by importing the properties of all nested objects.

.index(generator)
Builds a lookup object from the items array, using an object property or a custom key generator.

Learning Mangler.js

Optional Modules

Reference

Clone this wiki locally