js-streams
v1.0.3
Published
**An implementation of Java 8 stream API of Java Script.**
Readme
Install
$ npm install js-streamsImport
let js_stream = require('js-streams')Optional
An object that wrapped a single value and provides checks to validate it's presence.
#isPresent()
Check if the value is present.
js_stream.Optional.empty().isPresent() // false
js_stream.Optional.forValue(undefined).isPresent() // false
js_stream.Optional.forValue(5).isPresent() // true
js_stream.Optional.forValue(null).isPresent() // false
js_stream.Optional.forNullableValue(null).isPresent() // true#get()
Get the wrapped value. Throws an exception if isPresent() returns False.
js_stream.Optional.empty().get() // throws Error
js_stream.Optional.forValue(5).get() // 5
js_stream.Optional.forValue(js_stream.Optional.forValue(5)).get() // 5#ifPresent(function, function)
A method to consume the internal value if it is present.
js_stream.Optional
.forValue(5)
.ifPresent(
function(value) {
// value is 5
}
)Seconds callback is invoked when the value is not present.
js_stream.Optional
.forValue(5)
.ifPresent(
null,
function() {
// no value present
}
)Stream
A wrapper for either an array or single value. Has method for filtering and finding values using Optional values as return value.
Create a stream
js_stream.Stream.forValue().get() // []
js_stream.Stream.forValue(1).get() // [1]
js_stream.Stream.forValue([1, 2, 3]).get() // [1, 2, 3]Create a stream
js_stream.Stream.forValue().get() // []
js_stream.Stream.forValue(1).get() // [1]
js_stream.Stream.forValue([1, 2, 3]).get() // [1, 2, 3]#isEmpty()
js_stream.Stream.forValue().isEmpty() // true
js_stream.Stream.forValue(1).isEmpty() // false#filter()
Filter the contents of the stream using a predicate function and returns a new stream with the results.
js_stream.Stream.forValue([1, 2]).filter( function(v){return v==2}).get() // [2]#first()
Returns an Optional value of the first entry in the stream or an empty Optional if the stream was empty.
js_stream.Stream.forValue([1, 2]).first().isPresent() // true
js_stream.Stream.forValue([1, 2]).first().get() // 1
js_stream.Stream.forValue([]).first().isPresent() // false#find()
Returns an Optional value of the first match found for the supplied predicate.
js_stream.Stream.forValue([1, 2])
.find(
function(v){ return v==2 }
)
.isPresent() // true
js_stream.Stream.forValue([1, 2])
.find(
function(v){ return v==2; }
)
.get() // 2
js_stream.Stream.forValue([1])
.find(
function(v){return v==2}
)
.isPresent() // false#map()
Calls a defined callback function on each element of an stream, and returns an Stream that contains the results.
js_stream.Stream.forValue([1, 2])
.map(
function(v){ return v*2 }
)
.get() // [2, 4]#forEach()
Calls a defined callback function on each element of the stream.
js_stream.Stream.forValue([1, 2])
.forEach(
function(v){ // do something }
)