Collections for JS and TypeScript
Check out the tests for a more comprehesive doco.
This entire project is based around the API's of .NET and the way it does enumerables, collections and fluent LINQ.
If you are using TypeScript, the classes herein are generics. This means you should construct them with a type parameter to use them.
For example:
let myArray = [1, 2, 3, 4];
let myCollection = new Collection<number>(myArray);
If the type can be determined by the compiler (as in the example above) it is not neccessary to explicitly define the type - doing it will just make the code more readable.
All examples given in the documentation are JavaScript examples so that no-one gets confused.
Just use this as you need to - sometimes it wont be neccessary - the examples below will still work fine.
This is the base class for List
.
A collection is immutable meaning you cannot modify it once it is created. It only provides ways of iterating items and copying items to a new collection
You can create a new Collection from either a current Collection, an array or nothing.
let myCollection = new Collection();
let myArray = [1, 2, 3, 4];
let myCollection = new Collection(myArray);
let myArray = [1, 2, 3, 4];
let myCollection = new Collection(myArray);
let myCollection2 = new Collection(myCollection);
Returns to number of items in the Collection.
This will return a number greater than or equal to 0.
myCollection.length;
Copies the items from the current collection into another one.
This appends the items from one collection into another.
let myCollection = new Collection([1, 2, 3, 4]);
let myCollection2 = new Collection([-1, 0]);
// copy the items from myCollection into myCollection2
myCollection.copyTo(myCollection2);
// the contents of myCollection2 will now be: [-1, 0, 1, 2, 3, 4]
Iterates over each item in the collection, performing the callback on each item.
Return false
from your callback to break iteration. You do not have to
return anything if you do not want to break. You can return true
to
continue iteration if required.
let myCollection = new Collection([1, 2, 3, 4]);
let count = 0;
// iterate over each item, if the item is '3' then break.
myCollection.forEach((value, index) =>
{
if (value === 3)
{
return false;
}
count++;
});
// count will be equal to 2
Returns the item at the index or throws an exception if the index is out of bounds of the Collection.
myCollection.item(6);
Returns the contents of the collection as an array.
myCollection.toArray();
These methods will only work if you have included the module that they are in.
The module is the first part and the method is the second part in the docs
below using the pattern {module}::{method}
Returns an Enumerator for a collection.
let myCollection = new Collection([1, 2, 3, 4]);
let myCollectionEnumerator = myCollection.getEnumerator();
The enumerator class allows iteration of a collection or array.
An enumerator allows you to move incrementally through a collection or array
giving you the ability to peek
at the next element or move to it.
You can create a new Enumerator from either a current Collection or an array.
let myArray = [1, 2, 3, 4];
let myEnumerator = new Enumerator(myArray);
let myArray = [1, 2, 3, 4];
let myCollection = new Collection(myArray);
let myEnumerator = new Enumerator(myCollection);
Returns the current item in the enumeration.
This will throw an error if moveNext()
is not called before it.
This will throw an error if the pointer is invalid (out of bounds).
myEnumerator.current;
Increments the internal pointer of the Enumerator, essentially moving to the next element in the Enumerator.
Returns a boolean value of whether there is another item in the enumerator after this item.
Returns false
if there are no more elements in the enumerator.
let myCollection = new Collection([1, 2, 3, 4]);
let myEnumerator = new Enumerator(myCollection);
// iterate over each item, will break when there
// are no more items left.
while (myEnumerator.moveNext())
{
// do something here...
}
Returns the next item in the enumerator without incrementing the internal pointer.
Will throw an exception if there is no item to "peek" at.
let myCollection = new Collection([1, 2, 3, 4]);
let myEnumerator = new Enumerator(myCollection);
// check out the first item, it will return 1
let thisIsTheFirstElement = myEnumerator.peek();
// we can still iterate over the collection
while (myEnumerator.moveNext())
{
// do something here...
}
Moves the pointer back to the start of the enumerator as thought it were freshly created
You must call moveNext()
after calling this as it resets the entire
iteration.
let myCollection = new Collection([1, 2, 3, 4]);
let myEnumerator = new Enumerator(myCollection);
// iterate over the collection
while (myEnumerator.moveNext())
{
// do something here...
}
// reset the iteration
myEnumerator.reset();
// iterate over the collection again
while (myEnumerator.moveNext())
{
// do something else here...
}
A list is a level above a collection. It allows you to add, remove, insert and prepend items. Think of it as a mutable collection.
It inherits from Collection.
You can create a new List from either a current Collection, an array or nothing.
let myList = new List();
let myArray = [1, 2, 3, 4];
let myList = new List(myArray);
let myArray = [1, 2, 3, 4];
let myCollection = new Collection(myArray);
let myList = new List(myCollection);
Inherits all properties from Collection.
Inherits all methods from Collection.
Adds an item to the List.
let myList = new List();
myList.add(1);
Adds a array of items to the end of the List.
let myList = new List();
myList.addRange([1, 2, 3]);
Adds a collection of items to the end of the List.
let myList = new List();
let myCollection = new Collection([1, 2, 3]);
myList.addRange(myCollection);
Removes all items from the List.
let myList = new List([1, 2, 3]);
myList.clear();
Returns true if the List contains the item. Setting
isEquivilent
will compare each item as a JSON serilized object.
isEquivilent
will essentially compare each item as a string. So even if they
are not the exact same item, by reference, they can be 'equal'.
let myList = new List([1, 2, 3]);
myList.contains(2); // returns true
myList.contains(5); // returns false
Returns the item if it is in the List or
undefined
if it is not. SettingisEquivilent
will compare each item as a JSON serilized object.
isEquivilent
will essentially compare each item as a string. So even if they
are not the exact same item, by reference, they can be 'equal'.
let myList = new List([1, 2, 3]);
myList.find(2); // returns 2
myList.find(5); // returns undefined
Returns the index of an item if it is in the List or
undefined
if it is not. SettingisEquivilent
will compare each item as a JSON serilized object.
isEquivilent
will essentially compare each item as a string. So even if they
are not the exact same item, by reference, they can be 'equal'.
let myList = new List([1, 2, 3]);
myList.findIndex(2); // returns 1
myList.findIndex(5); // returns undefined
Inserts an item at the
index
and moves the subsequent items up 1 index.
let myList = new List([1, 2, 3]);
myList.insertAt(4, 2); // will now be [1, 2, 4, 3]
Inserts an item at the start and moves the subsequent items up 1 index.
Shorthand for myList.insertAt(obj, 0)
.
let myList = new List([1, 2, 3]);
myList.prepend(4); // will now be [4, 1, 2, 3]
Inserts an array of items at the start and moves the subsequent items up 1 index.
let myList = new List([1, 2, 3]);
myList.prependRange([4, 5, 6]); // will now be [4, 5, 6, 1, 2, 3]
Inserts a collection of items at the start and moves the subsequent items up 1 index.
let myList = new List([1, 2, 3]);
let myCollection = new Collection([4, 5, 6]);
myList.prependRange(myCollection); // will now be [4, 5, 6, 1, 2, 3]
Removes an item from the List.
let myList = new List([1, 2, 3]);
myList.remove(1); // will now be [2, 3]
Removes an item at the
index
from the List.
let myList = new List([1, 2, 3]);
myList.removeAt(2); // will now be [1, 2]
Sorts the List using the provided
comparer
.
The Enumerable class is mostly fluent by design and allows chaining of query-like methods to filter or aggregate an array or collection.
- It has been modelled off of the .NET Fluent LINQ extension methods.
- It inherits from Collection.
- As with collections, enumerables are immutable - Every method returns a new Enumerable if it returns a Enumerable
A Predicate is a delegate method that takes an item of the enumerable as the argument, and returns a boolean value.
This is used on the all
, any
and where
methods of Enumerable.
Predicate = (item: any) => boolean
You can create a new Enumerable from either a current Collection, an array or nothing.
let myList = new List();
let myArray = [1, 2, 3, 4];
let myEnumerable = new Enumerable(myArray);
let myArray = [1, 2, 3, 4];
let myCollection = new Collection(myArray);
let myEnumerable = new Enumerable(myCollection);
Inherits all properties from Collection.
Inherits all methods from Collection.
Returns
true
if all the items match thepredicate
.
Returns
true
if the Enumerable contains elements
if(myEnumerable.any())
{
// do something because the enumerable has elements
}
Returns
true
if any of the elements satisfy thepredicate
.
let myEnumerable = new ArrayEnumerable([1, 2, 3, 4]);
if(myEnumerable.any((i) => i == 2))
{
// the enumerable contains a '2'
}
Returns the sum of numbers designated by the
propertyName
.
Useful for getting totals of a dataset.
Returns the sum of numbers returned by the
selector
.
Useful for getting totals of a dataset.
Returns a Enumerable that subset of the items from 0 to
count
Returns a Enumerable of the items that match the predicate
Used to filter a dataset.