-
Notifications
You must be signed in to change notification settings - Fork 0
Home
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.
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 other scripts using mangler.js -->
</head>
<body>
<!-- content -->
</body>
</html>
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" utility functions 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.
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.
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(prop)
Returns items in an object keyed on the specified property for easy lookups.
Mangler.js - JavaScript object processing library
Copyright (C) 2014-2016
Project: http://codebin.co.uk/projects/mangler-js/
GitHub: https://github.com/DarthJDG/Mangler.js
- Home
- About the documentation
- Getting started
- Searching and querying
- Transforming data
- Advanced