Skip to content
Robert Biro edited this page Mar 11, 2014 · 18 revisions

Mangler.js documentation

DOCUMENTATION IS UNDER CONSTRUCTION, EXPECT LOTS OF MISSING PAGES!

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.

<html>
<head>
    <script src="mangler.js"></script>
</head>
<body>
</body>
</html>

Usage

The library will register the global Mangler() function, which is the main entry point of the library and is used to create a mangler object that is used to process your data. As well as a function, the Mangler global doubles as an object as well, containing a collection of useful "static" methods that don't need a mangler object.

Calling Mangler() without a parameter will return an empty mangler object, which you can manually put items into via the .add() and .push() methods. Most methods of a mangler object will return a reference to itself, making it easy to chain commands together.

More often than not however, you'll want to pass your object directly to Mangler() to fill it with data right away:

var data = { id: "001", name: "John Smith" };
var m = Mangler(data);

To access you data contained in a mangler object, read its .items[ ] property.

console.log(m.items);

The above command will yield the following in the browser's console:

[{ id: "001", name: "John Smith" }]

Note that .items is always an array, even if you pass only a single object.

Adding items

As mentioned above, there are two commands to add items to a mangler object: .add() and .push(). The difference between the two commands is in the handling of array parameters. The .add() method 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"]]

Most methods use .add() internally by default, including the main Mangler() function, but where it's needed you'll be able to change this behaviour with optional parameters per method call.

Learning Mangler.js

Optional Modules

Reference

Clone this wiki locally