500 byte library for Streams in JavaScript
Development Version Production Version
Stream.js is a library which adds Streams for Arrays. These Streams work in all browsers, even as far back as IE7. They're incredibly fast, and can support a multitude of functions.
Along with this compatibility, it also supports the new ES6 arrow syntax. Prototypes can be passed to it as well.
Streaming an unordered array of numbers into an array of just the even numbers, as strings with a decimal point:
new Stream ([5, 2, 3, 0])
.sort(function (o1, o2) { return o1 - o2 })
.filter(function (o) { return o % 2 })
.map(function (o) { return o + '.0' })
.toArray();
// => ["3.0","5.0"]
Sorting an unordered array of strings into an array of them sorted alphabetically and lowercase
new Stream(['Zebra', 'HoRse', 'Mammoth'])
.map(String.prototype.toLowerCase)
.sort(function(o1, o2) { return o1.charCodeAt(0) - o2.charCodeAt(0) })
.toArray();
// => ["horse","mammoth","zebra"]
Returns a new Stream object with the elements of the array
Converts the current Stream to an array. This does not modify the original array.
Iterates through all of the elements.
If all the objects in the Stream are numbers, then it will calculate the total sum.
Returns the amount of objects currently in the stream.
Returns the first object it finds. It will return null if there are no objects in the stream.
Accepts a Function which takes all of the elements, then returns new values for them.
Accepts a Function which accepts all of the elements, then returns true if the element should stay.
Accepts a function that sorts the stream. See Array.prototype.sort on what the array should be returning.
Uses == to check all of the elements and make sure they're distinct.
Creates a new copy of the stream, with all transformers. This does not deepcopy the original array elements.