Home

Robert Biro edited this page Oct 13, 2016 · 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.

Features

  • Provides an extensive set of utility functions. Avoid re-writing boilerplate code all the time and concentrate on the task itself.
  • Can also be used as a container to do chained operations.
  • Uses the MongoDB query language.
  • Container exposes its internal array for easy integration with other frameworks, like AngularJS.

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>

Example

Here's a quick example of what you can do with the library:

data = [
    { name: 'Bill', score: 75, team: 'red' },
    { name: 'John', score: 25, team: 'red' },
    { name: 'Jack', score: 60, team: 'blue' }
];

m = Mangler(data);

// Get a list of names
list = m.extract('name');

// Find the first person whose name starts with J and with a score above 50
jack = m.first({ name: /^J/, score: { $gt: 50 } });

// Calculate the average score by team
results = m.aggregate('avg', { value: 'score', group: 'team' });

/*
results = {
    red: 50,
    blue: 60
}
*/

Learning Mangler.js

The next sections will walk you through the basic features of the library. For more thorough examples, have a look at the individual functions and methods in API reference.