Basics - DarthJDG/Mangler.js GitHub Wiki

The library will register the Mangler global. It is an object that contains the library's utility functions. It also doubles as the Mangler() function, which creates and returns a mangler object instance.

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

The next sections will cover the functions and methods, this section simply explains how to use the mangler object as a data container.

Adding items

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

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

m = Mangler().add(data1, data2);
// m.items = ['A', 'B', 'C', 'D', 'E', 'F']

m = Mangler().push(data1, data2);
// m.items = ['A', 'B', 'C'], ['D', 'E', 'F'](/DarthJDG/Mangler.js/wiki/'A',-'B',-'C'],-['D',-'E',-'F')

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

Retrieving items

To read a single item by array index, you can either use the mangler object's .get() method, or simply read it from the public .items[] array property. The following commands both return the second item 'B':

m = Mangler(['A', 'B', 'C']);

item = m.get(1);
item = m.items[1];

Removing items

You can remove all items from the mangler object with the .clear() method, or truncate the array using .limit(). To remove a single item by index or value, use .remove() or .removeItem() respectively.

m = Mangler(['A', 'B', 'C', 'D', 'E']);

// Remove the fifth item, leaving: ['A', 'B', 'C', 'D']
m.remove(4);

// Remove 'B' from the array, leaving: ['A', 'C', 'D']
m.removeItem('B');

// Limit to 2 items: ['A', 'C']
m.limit(2);

// Clears the items array: []
m.clear();