iteratez
A simple yet powerful iterator for simple and complex types. Out of the box you can iterate over arrays, objects, and trees, but you can provide iteration capabilites to your own code no matter how complex the type (dynamically calculated, etc).
The iterator is lazy, so you can chain where
/map
/reverse
/exclude
/intersect
/sorted
/unique
/duplicates
/take
/skip
/drop
/append
/prepend
/gt
/gte
/lt
/lte
s and iteration is not done until you empty
/has
/contains
/first
/last
/count
/list
/object
/reduce
/min
/max
/iterate
/erase
/overwrite
/extract
/etc.
Features
* = you can pass a where function in to test the items
- Array, object, and tree iterators out of the box.
- Stop the iterator at any time (and optionally return a result)
- If the underlying source supports it, remove an item.
- If the underlying source supports it, replace an item.
- * Check to see if an iterator is empty.
- * Count the number of items.
- * Get the first item.
- * Get the last item.
- * Convert the items into an array.
- * Convert the items into an object given a function to obtain a key from an item.
- Remove all items from the source that don't pass a test.
- Return a new iterator for all the items that pass a test.
- Reduce the values in the iterator down to a single value.
- Map the items in the iterator to a new iterator.
- Iterate over only the first
X
items. - Skip over the first
X
items. - Iterate over the items with a callback.
- Append one or more iterators to the end of the current one.
- Prepend one or more iterators to the beginning of the current one.
- Return an iterator that goes in reverse.
- Custom iterator.
You can see all of these features in the examples below.
Examples
The example is in Typescript, but iterator is available as iz.Iterate
in JS
import { Iterate } from 'iteratez';
let source = Iterate.array([1, 5, 7, 9, 10]);
let source = Iterate.object({
name: 'ClickerMonkey',
age: 30
});
let source = yourSource.yourIteratorGenerator();
source.iterate((item, iter) => {
if (someCondition(item)) {
iter.stop(42)
}
}).withResult((result) => {
});
source.iterate((item, iter) => {
if (someCondition(item)) {
iter.remove();
}
});
source.iterate((item, iter) => {
if (someCondition(item)) {
iter.replace(replacement);
}
});
let empty = source.empty();
let has = source.has();
let contains = source.contains(2);
let first = source.first();
let last = source.last();
let count = source.count();
let array = source.list();
let sameArray = source.list(existingArray);
let personById = source.object(item => item.id);
let sameObject = source.object(item => item.id, existingObject);
let min = source.min(Iterate.COMPARE_NUMBER);
let max = source.max(Iterate.COMPARE_NUMBER);
source.erase();
source.where(x => x.id).erase();
source.extract();
source.overwrite(42);
source.where(x => x > 34).overwrite(12);
source.where(x => x.age > 0);
source.map(x => x.name);
source.reverse();
source.exclude(anotherSource);
source.intersect(anotherSource);
source.sorted(comparator?);
source.unique(equality?);
source.duplicates(onlyOnce?);
source.take(10);
source.skip(5);
source.drop(3);
source.append(anotherSource);
source.prepend(anotherSource);
source.gt(value, comparator?);
source.gte(value, comparator?);
source.lt(value, comparator?);
source.lte(value, comparator?);
source.withComparator((a, b) => number);
source.withEquality((a, b) => boolean);
source.duplicates().has();
source.duplicates().erase();
source.where(x => x.age < 18).extract();
source.sorted().skip(5).take(10).list();
source.map<string>(
item => item.name,
(replaceWith, current, item) => {
item.name = replaceWith;
}
).iterate((name, iter) => {
iter.replace(name.toUpperCase());
});
source.iterate((item, iter) => {
});
interface Node<T> {
value: T
children?: Node[]
}
const treeIterator = Iterate.tree<Node<string>, string>(
node => node.value,
node => node.children,
(node, value) => node.value = value
);
const head: Node<string> = ...
const depthFirstList = treeIterator(head).list();
const breadthFirstList = treeIterator(head, false).list();
function getDateIterator (start: Date, max: number): Iterate<Date>
{
return new Iterate<Date>(iter =>
{
const curr = new Date(start.getTime());
for (let i = 0; i < max; i++)
{
const copy = new Date(curr.getTime());
switch (iter.act(copy))
{
case IterateAction.Stop:
return;
case IterateAction.Remove:
break;
case IterateAction.Replace:
break;
}
curr.setDate(curr.getDate() + 1);
}
});
}
const week5DaysFromNow = getDateIterator(new Date(), 100).skip(5).limit(7).list();
TODO