$ npm install js-streams
let js_stream = require('js-streams')
An object that wrapped a single value and provides checks to validate it's presence.
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 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
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
}
)
A wrapper for either an array or single value. Has method for filtering and finding values using Optional values as return value.
js_stream.Stream.forValue().get() // []
js_stream.Stream.forValue(1).get() // [1]
js_stream.Stream.forValue([1, 2, 3]).get() // [1, 2, 3]
js_stream.Stream.forValue().get() // []
js_stream.Stream.forValue(1).get() // [1]
js_stream.Stream.forValue([1, 2, 3]).get() // [1, 2, 3]
js_stream.Stream.forValue().isEmpty() // true
js_stream.Stream.forValue(1).isEmpty() // false
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]
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
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
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]
Calls a defined callback function on each element of the stream.
js_stream.Stream.forValue([1, 2])
.forEach(
function(v){ // do something }
)