-
Notifications
You must be signed in to change notification settings - Fork 0
Iterators
You can use the .each() utility function to call a callback function for each element of an iterable object. Built-in iterables are arrays, objects and mangler objects. You can break the iteration by returning false from the callback function. The callback function has the following signature:
function(key, value)The callback function is called for each array item, passing the parameters (index, item). For sparse arrays, undefined elements will be skipped.
sum = 0;
array = [1,2,3];
Mangler.each(array, function(index, item) {
sum += item;
});
// sum = 6The callback function is called for each of the object's own properties, passing the parameters (property_name, value). Due to the JavaScript language specifications, the exact order of property enumeration cannot be guaranteed.
sum = 0;
properties = [];
object = {
one: 1,
two: 2,
three: 3
};
Mangler.each(object, function(property, value) {
sum += value;
properties.push(property);
});
// sum = 6
// properties = ['one', 'two', 'three']Iterating through a mangler object is the same as iterating through its .items[] property. Just like most utility functions, .each() can also be called as a mangler object method, without the first parameter.
m = Mangler(...);
callback = function(k, v) {...};
// The following calls are equivalent:
Mangler.each(m, callback);
Mangler.each(m.items, callback);
m.each(callback);The list of iterable objects can be expanded by registering custom handler functions. The Mangler-natives module adds support for native typed arrays. If you want to make your own object instances iterable, read the advanced topic of Type handling.
With the .explore() function, you can recursively iterate all iterable objects and their iterable children. Returning false from the callback function will not break the iteration, but it will prevent recursive processing of the current iterable value, continuing with its sibling, skipping its children.
Mangler.explore(iterable, callback[, state])-
iterableis the root object to start from -
callbackis the function to call for all values found -
stateis an optional object to initialise the callback's parameter
Callback signature:
function(key, value[, state])-
keyandvalueare the same as the standard iterator's -
stateis an object containing information about the current step:-
state.$pathis a string describing thevalue's path from the root -
state.$parentis the parent object containing thekey/valuepair -
state.$parentPathis a string with the parent's path from the root
-
Given the following code:
data = [
{
child: { one: 1 }
},
{
children: [{ two: 2 }, { three: 3 }]
}
];
Mangler.explore(data, callback);it will result in the following calls to the callback function:
// key value state.$path
0 { child: ... } '[0]'
'child' { one: 1 } '[0].child'
'one' 1 '[0].child.one'
1 { children: ... } '[1]'
'children' [{ two: 2 }, { three: 3 }] '[1].children'
0 { two: 2 } '[1].children[0]'
'two' 2 '[1].children[0].two'
1 { three: 3 } '[1].children[1]'
'three' 3 '[1].children[1].three'For more examples, look at .explore() in the API reference.
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