Releases: Byloth/core
v1.3.0
Minor release
Added new Aggregator
class
Aggregator<T>
is a class that extends SmartIterator<T>
and allows the user to perform some transformation over elements of type <T>
.
It allows you to group elements by a key of type <K>
, filter them, map them to new values and/or reduce them to other objects and eventually materialize them again into new T[][]
, Map<K, T[]>
or Record<K, T[]>
objects.
Improved SmartIterator
class
Added methods and functionalities to SmartIterator
objects: enumerate
, unique
& count
.
enumerate
takes its behavior from Python'senumerate
method: iterates over the originalSmartIterator<T>
returning a newSmartIterator<[number, T]>
containing the index and value pairs of the original iterator.unique
consumes the originalSmartIterator<T>
returning a newSmartIterator<T>
guaranteeing that it will only contain unique values.count
consumes the originalSmartIterator<T>
returning the number of elements it contains.
Added new Random
class
The previous random
utility function has been removed.
Now there's the new Random
class now with some static methods: Boolean
, Integer
, Decimal
and Choice
.
Boolean
generates a boolean value; optionally, you can specify the percentage of return of atrue
value.Integer
generates an integer value between two different values.Decimal
generates a decimal value between two different values.Choice
takes an array of elements and returns a random element from it.
Added a new utility function
Added the new enumerate
utility function that takes some elements of type T
and returns a new SmartIterator<[number, T]>
containing the index and value pairs of the original iterator.
v1.3.0-rc.2
Release Candidate
- Added platform-indipendent
Interval
andTimeout
return types.
v1.3.0.-rc.1
Release Candidate
- Removed
random
utility function while implemented a newRandom
class.
v1.2.1-rc.1
Release Candidate
- Improved
SmartIterator
class withcount
,enumerate
&unique
methods. - Added
enumerate
utility function. - Added
Aggregator
class. - Added
AggregatedIterator
class. - Added
ReducedIterator
class.
v1.2.0
Minor release
Added SmartIterator
class
SmartIterator
is a class that implements the Iterable
and Iterator
interface standards.
It can be used transparently as an Iterable
or an Iterator
plane object while still allowing you to call some functional-oriented methods that -until now- were reserved only for Array
obejcts:
filter
,map
&reduce
.forEach
.every
&some
.
Calling these methods consumes the SmartIterator
itself while it performs the operation and/or returns a result; the filter
and map
functions -of course- return a new SmartIterator
instance based on their predicate
argument.
Initialization
To instanciate a new SmartIterator
object, simply call the constructor
by passing an Iterable
or an Iterator
object or by defining a new GeneratorFunction
inline.
Here some examples...
... based on an Array
object:
const smartIteratorObj = new SmartIterator([0, 2, 4, 6, 8]);
... based on an Iterable
object:
const hashmapObj = new Map();
// [...]
const smartIteratorObj = new SmartIterator(hashmapObj.values());
... based on an Iterator
object:
function* fibonacci(start = 0) {
let a = start;
let b = 1;
while (true) {
yield a;
[a, b] = [b, a + b];
}
}
const iteratorObj = fibonacci(8);
const smartIteratorObj = new SmartIterator(iteratorObj);
... based on an inline GeneratorFunction
:
const smartIteratorObj = new SmartIterator(function* () {
for (let i = 0; i < 10; i +=1) {
yield i;
}
});
Convert to an Array
Whenever you need to materialize it into an Array
object, simply call the toArray
method.
Be careful of the infinite generators (e.g. Fibonacci's sequence)!
Support for next
with parameters, returns
& throws
methods
It also supports Iterator
objects with these types of non-nullish properties.
Objects with non-null next
parameters won't be Iterable
and using them in for-of
statements will return a TypeScript error (unlike standard Iterator
objects).
Improved support & Backward compatibility
Library functions like range
, unique
or zip
that were previously GeneratorFunction
objects, now return an instance of the new SmartIterator
class. This won't affect previous implementations.
v1.2.0-rc.3
Release Candidate
- Added
every
andsome
methods toSmartIterator
class.
v1.2.0-rc.2
Release Candidate
- Added
toArray
method onSmartIterator
objects.
v1.2.0-rc.1
Release Candidate
- Implemented class
SmartIterator
which is a normal JS iterator that allows you to usefilter
,map
,reduce
andforEach
functions on it. - Library functions that previously returned some sort of
Iterable
object, now return aSmartIterator
object.
v1.1.8
Patch release
- Added
average
function (also capable of weighted average). - Added
zip
function (inspired by the Python's one).