
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
sequences is a collection of utilities for working with lazily-evaluated
sequences of data.
Working with lazily-evaluated sequences can be more efficient than other JS alternatives, as it allows you to minimize intermediate memory allocations where possible, and only perform operations on elements that contribute to your final result.
// temporary arrays are created by filter and map,
let val = [-3, -2, -1, 0, 1, 2, 3]
.filter((val) => val > 0)
.map((val) => val + 1)
.reduce((sum, val) => sum + val, 0);
// no temporary arrays
let val2 = FromArray([-3, -2, -1, 0, 1, 2, 3])
.pipe(Filter, (val) => val > 0)
.pipe(Map, (val) => val + 1)
.pipe(Reduce, (sum, val) => sum + val, 0)
.read();
ES6 Iterators require a temporary { done, value } object to be allocated with every call, which reduces performace.
Sequence().read() accepts a 'recycle' parameter when called, which allows you to re-use previously allocated values, to minimize allocations.
Sequence().read() returns the next result or the terminator Sequence().END, which can simplify control flow compared to having an "independent" control channel (the iterator done parameter).
objectKind: global namespace
object
SequenceSequenceSequenceSequenceSequenceSequenceSequenceSequenceSequenceSequenceSequenceSequenceSequenceSequenceSequenceSequenceArraySequenceSequenceSequenceSequenceSequenceSequenceSequenceSequenceIteratorSequenceSequenceSequenceobject
SequenceSequenceSequenceSequenceobject
SequenceSequenceSequenceSequenceSequenceKind: static class of sequences
Sequence is the base sequence class.
it should always be subclassed.
sequence constructors should never require new.
read is the core method of a sequence. read should return the next value in the sequence. if there are no more values to read, read should return Sequence.END
Kind: instance method of Sequence
Params
// this
let seq = From(1, 2, 3, 4);
seq = Slice(seq, 0, 10);
seq = Assert(seq, (val, i) => Number.isInteger(val));
seq = ToArray(seq);
// is equivalent to this
const seq = From(1, 2, 3, 4)
.pipe(Slice, 0, 10)
.pipe(Assert, (val, i) => Number.isInteger(val))
.pipe(ToArray);
pipe is a utility method to wrap one sequence in another.
Kind: instance method of Sequence
Params
* - any number of additional args to pass into sequenceConstructorSequence
let Assert = require('sequences/Assert');
let From = require('sequences/From');
let ToArray = require('sequences/ToArray');
let isInteger = (val) => Number.isInteger(val);
// val is [ 1, 2, 3, 4 ]
let val = From(1, 2, 3, 4)
.pipe(Assert, isInteger)
.pipe(ToArray)
.read();
// throws an assertion error
let val2 = From(1, 2, 3, "4")
.pipe(Assert, isInteger)
.pipe(ToArray)
.read();
Assert is a sequence wrapper that builds a sequence to run an assertion against every value in the sequence
Kind: static property of sequences
Params
Sequence - a source sequencefunction - an assertion functionfunction - an error builder functionSequence let Count = require('sequences/Count');
let Slice = require('sequences/Slice');
let Concat = require('sequences/Concat');
// res is '0 - 1 - 2 - 3 - 4':
let res = Count()
.pipe(Slice, 0, 5)
.pipe(Concat, ' - ')
.read();
Concat performs string concatenation of all elements in a sequence
Kind: static property of sequences
Params
Sequence - a source sequencestring - an optional separator string, to be placed inSequence
let Count = require('sequences/Count');
let Slice = require('sequences/Slice');
let ToArray = require('sequences/ToArray');
// val is [ 0, 1, 2, 3 ]
let val = Count()
.pipe(Slice, 0, 4)
.pipe(ToArray)
.read();
Count is a sequence constructor that builds a sequence that counts integers upward
Count never terminates, so make sure to add a terminating sequence like a Slice somewhere after it.
Kind: static property of sequences
Params
number - the number to start counting fromSequence let Count = require('sequences/Count');
let Deduplicate = require('sequences/Deduplicate');
let Map = require('sequences/Map');
let Slice = require('sequences/Slice');
let ToArray = require('sequences/ToArray');
// res is [0, 1, 2, 3]:
let res = Count()
.pipe(Slice, 0, 50)
.pipe(Map, (val) => val % 4)
.pipe(Deduplicate)
.pipe(ToArray)
.read();
Deduplicate removes duplicates from a sequence, while maintaining sequence order
NOTE: Deduplicate uses a Set to track already-seen elements,
so it can potentially use a large amount of memory
Kind: static property of sequences
Params
Sequence - a source sequencefunction - an optional projection function, that you can use to deduplicate based off some part of valuesSequence let Count = require('sequences/Count');
let Default = require('sequences/Default');
let Filter = require('sequences/Filter');
let Slice = require('sequences/Slice');
// res is [0, 10, 20, 30, 40]:
let res = Count()
.pipe(Slice, 0, 50)
// filter out everything, so the sequence returns END
.pipe(Filter, (val) => val > 9999)
.pipe(Default, 0)
.read(); // returns 0
Default provides a default return value to the sequence, if the sequence terminates without returning any value
Kind: static property of sequences
Params
Sequence - a source sequencefunction - the default valueSequence let Count = require('sequences/Count');
let Drain = require('sequences/Drain');
let Slice = require('sequences/Slice');
// returns sequence terminator
Count()
.pipe(Slice, 0, 4)
.pipe(Drain)
.read();
Drain is a sequence constructor wraps a source sequence, and when read is called it reads the entire sequence and throws it away.
Useful for sequences with side-effects.
Kind: static property of sequences
Params
Sequence - the source sequence to drainSequence let Count = require('sequences/Count');
let Drain = require('sequences/Drain');
let Each = require('sequences/Each');
let Slice = require('sequences/Slice');
// should log:
// element 0 is 1
// element 1 is 2
// element 2 is 3
Count()
.pipe(Slice, 1, 4)
.pipe(Each, (val, i) => console.log(`element ${i} is ${val}`))
.pipe(Drain)
.read();
Each takes a function, and called it once per every element in a sequence.
Useful for logging, or performing other side-effects.
Kind: static property of sequences
Params
Sequence - the source sequence to drainfunction - a function to get called for each valueSequence let Count = require('sequences/Count');
let Filter = require('sequences/Filter');
let Slice = require('sequences/Slice');
let ToArray = require('sequences/ToArray');
// res is [0, 10, 20, 30, 40]:
let res = Count()
.pipe(Slice, 0, 50)
.pipe(Filter, (val, i) => (val % 10 === 0))
.pipe(ToArray)
.read();
Filter removes some items from a sequence.
Kind: static property of sequences
Params
Sequence - a source sequencefunction - a filter functionSequence let From = require('sequences/From');
let Flatten = require('sequences/Flatten');
let ToArray = require('sequences/ToArray');
// res is [1, 2, 3, 4, 5, 6]:
let res = From(1, [], [2, 3], From(4, 5, 6))
.pipe(Flatten)
.pipe(ToArray)
.read();
Flatten 'flattens' a sequence of "collections" into a sequence of elements.
right now, Flatten supports flattening sequences and array-like objects.
Anything else will be passed through without modification.
Kind: static property of sequences
Params
Sequence - a sequence of arraysSequence let FromArray = require('sequences/FromArray');
let ToArray = require('sequences/ToArray');
// res is [1, 2, 3]:
let res = FromArray([ 1, 2, 3 ])
.pipe(ToArray)
.read();
FromArray builds a sequence from an array.
Kind: static property of sequences
Params
array - values to return in the sequence, in orderSequence let From = require('sequences/From');
let FromBlocks = require('sequences/FromBlocks');
let ToArray = require('sequences/ToArray');
// res is [1, 2, 3, 4, 5, 6]:
let res = From([ 1, 2, 3 ], [4, 5, 6])
.pipe(FromBlocks)
.pipe(ToArray)
.read();
FromBlocks 'flattens' a sequence of arrays into a sequence of elements.
FromBlocks is a legacy alias for Flatten
Kind: static property of sequences
Params
Sequence - a sequence of arraysSequence
FromIterator builds a sequence from an iterator
Kind: static property of sequences
Params
Iterator - iterator to convert into a sequenceSequence let From = require('sequences/From');
let ToArray = require('sequences/ToArray');
// res is [1, 2, 3]:
let res = From(1, 2, 3)
.pipe(ToArray)
.read();
From builds a sequence from its arguments.
Kind: static property of sequences
Params
* - values to return in the sequence, in orderSequence let FromObject = require('sequences/FromObject');
let ToArray = require('sequences/ToArray');
// res is [{ key: 'a', value: 1 }, { key: 'b', value: 2 }]:
let res = FromObject({ a: 1, b: 2 })
.pipe(ToArray)
.read();
FromObject builds a sequence of key-value pairs from an object.
Kind: static property of sequences
Params
object - object from which to return a sequence of key-value pairsSequence
FromSet builds a sequence from a Set
Kind: static property of sequences
Params
Set - set to convert into a sequenceSequence let From = require('sequences/From');
let Group = require('sequences/Group');
let ToArray = require('sequences/ToArray');
// res is [ [1, 2, 3], [4, 5, 6] ]:
let res = From(1, 2, 3, 4, 5, 6)
.pipe(Group, 3)
.pipe(ToArray)
.read();
Group converts a sequence into a sequence of 'blocks' (fixed-size arrays of the elements)
Kind: static property of sequences
Params
Sequence - the source sequencenumber - the size of blocks to emitArray let Join = require('sequences/Join');
let ToArray = require('sequences/ToArray');
// res is [ [1, 4], [1, 5], [1, 6], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6] ]:
let res = Join([1, 2, 3], [4, 5, 6])
.pipe(ToArray)
.read();
Join converts two arrays into a number (first array size * second array size) of pairs (arrays of two items)
Kind: static property of sequences
Params
Array - first arrayArray - second arraySequence let Count = require('sequences/Count');
let Map = require('sequences/Map');
let Slice = require('sequences/Slice');
let ToArray = require('sequences/ToArray');
// res is [1, 2, 3]:
let res = Count()
.pipe(Slice, 0, 4)
.pipe(Map, (val, i) => val + 1)
.pipe(ToArray)
.read();
Map transforms each element in a sequence.
Kind: static property of sequences
Params
Sequence - a source sequencefunction - a map functionSequence let Count = require('sequences/Count');
let Reduce = require('sequences/Reduce');
let Slice = require('sequences/Slice');
// res is 6:
let res = Count()
.pipe(Slice, 0, 4)
.pipe(Reduce, (state, val, i) => state + val)
.read();
Reduce 'reduces' a sequence of elements to a single result.
Kind: static property of sequences
Params
Sequence - a source sequencefunction - a reduce function* - the initial value of the stateSequence let From = require('sequences/From');
let Replace = require('sequences/Replace');
let ToArray = require('sequences/ToArray');
// res is [1, 1, 2, 2, 3, 3]:
let res = From(1, -1, 2, -2, 3, -3)
.pipe(Replace, (val) => val < 0, (val) => -val)
.pipe(ToArray)
.read();
Replace allows you to replace some elements in a sequence dynamically.
It acts like a mapping with a pre-selector choosing which elements to map
Kind: static property of sequences
Params
Sequence - a source sequencefunction - the selector function, that chooses which elements to replacefunction - the mapper function, that replaces the elementsSequence let Count = require('sequences/Count');
let Slice = require('sequences/Slice');
let ToArray = require('sequences/ToArray');
// res is [1, 2, 3]:
let res = Count()
.pipe(Slice, 0, 4)
.pipe(ToArray)
.read();
Slice 'slices' out a piece of a sequence to use
Kind: static property of sequences
Params
Sequence - a source sequenceinteger - the index to start from (inclusive)integer - the index to end at (exclusive)Sequence let From = require('sequences/From');
let Sort = require('sequences/Sort');
let ToArray = require('sequences/ToArray');
// res is [1, 2, 3]:
let res = From(3, 2, 1)
.pipe(Sort)
.pipe(ToArray)
.read();
Sort sorts a sequence inline.
NOTE: Sort must buffer all values in the sequence for sorting, so it has a space complexity of O(N)
Kind: static property of sequences
Params
Sequence - the source sequenceSequence let From = require('sequences/From');
let Splice = require('sequences/Splice');
let ToArray = require('sequences/ToArray');
// res is [1, 2, 3, 4, 5, 6]:
let res = Splice(From(1, 2, 3), From(4, 5, 6))
.pipe(ToArray)
.read();
Splice 'splices' several sequences together, concatenating them into a single sequence
Kind: static property of sequences
Params
Sequence - the source sequencesSequence let From = require('sequences/From');
let ToArray = require('sequences/ToArray');
// res is [1, 2, 3]:
let res = From(1, 2, 3)
.pipe(ToArray)
.read();
ToArray converts a sequence into an array.
NOTE: ToArray will always return exactly once. If the source sequence is empty,
ToArray will return an empty array.
Kind: static property of sequences
Params
Sequence - the source sequenceSequence let From = require('sequences/From');
let ToBlocks = require('sequences/ToBlocks');
let ToArray = require('sequences/ToArray');
// res is [ [1, 2, 3], [4, 5, 6] ]:
let res = From(1, 2, 3, 4, 5, 6)
.pipe(ToBlocks, 3)
.pipe(ToArray)
.read();
ToBlocks converts a sequence into a sequence of 'blocks' (fixed-size arrays of the elements)
ToBlocks is a legacy alias for Group
Kind: static property of sequences
Params
Sequence - the source sequencenumber - the size of blocks to emitIterator let From = require('sequences/From');
let Map = require('sequences/Map');
let ToIterator = require('sequences/ToIterator');
// res is [1, 2, 3]:
let res = From(1, 2, 3)
.pipe(Map, (x) => x + 1)
.pipe(ToIterator);
console.log(res.next()); // { value: 2, done: false }
ToIterator converts a sequence into an iterator, if you need one
Kind: static property of sequences
Params
Sequence - the source sequenceSequence let From = require('sequences/From');
let ToObject = require('sequences/ToObject');
// res is { a: 1, b: 2 }:
let res = From({ key: 'a', value: 1 }, { key: 'b', value: 2 })
.pipe(ToObject)
.read();
ToObject converts a sequence into an object
The sequence must be a sequence of key-value pairs,
structured as an object with a 'key' and a 'value' property.
NOTE: ToObject will always return exactly once. If the source sequence is empty,
ToObject will return an empty object.
Kind: static property of sequences
Params
Sequence - the source sequenceSequence
ToSet converts a sequence into a Set
NOTE: ToSet will always return exactly once. If the source sequence is empty,
ToSet will return an empty Set.
Kind: static property of sequences
Params
Sequence - the source sequenceSequence
let From = require('sequences/From');
let ToArray = require('sequences/ToArray');
let Window = require('sequences/Window');
let isInteger = (val) => Number.isInteger(val);
// val is [ [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ] ]
let val = From(1, 2, 3, 4, 5)
.pipe(Window, 3)
.pipe(ToArray)
.read();
// val2 is [ [ 1 ], [ 1, 2 ], [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ], [ 4, 5 ], [ 5 ] ]
let val2 = From(1, 2, 3, 4, 5)
.pipe(Window, 3, true)
.pipe(ToArray)
.read();
Window is a sequence wrapper that returns a fixed-length sliding window of a source sequence
Kind: static property of sequences
Params
Sequence - a source sequencenumber - the size of the window bufferboolean - allow edges (a not-full buffer)
let Zip = require('sequences/Zip');
let From = require('sequences/From');
let ToArray = require('sequences/ToArray');
// res is [ [1, 4], [2, 5], [3, 6] ]:
let res = Zip([1, 2, 3], [4, 5, 6])
.pipe(ToArray)
.read();
// Zip takes in sequences or arrays as sources
// res is [ [1, 4], [2, 5], [3, 6] ]:
let res = Zip([1, 2, 3], From(4, 5, 6))
.pipe(ToArray)
.read();
// the zipped sequence will be the length of the _longest_ source
// if any source sequences end early, their result will be undefined
// res is [ [1, 4], [2, 5], [undefined, 6] ]:
let res = Zip([1, 2], [4, 5, 6])
.pipe(ToArray)
.read();
// if you need to clip your results to the shortest sequence, use Zip.Short
// res is [ [1, 4], [2, 5] ]:
let res = Zip.Short([1, 2], [4, 5, 6])
.pipe(ToArray)
.read();
Zip combines any number of arrays or sequences into a single sequence of tuples of elements at the same index
Kind: static property of sequences
objectKind: static namespace of sequences
object
SequenceSequenceSequenceSequenceSequence
let FromHex = require('sequences/bytes/FromHex');
*
FromHex converts a hex string into a Sequence of bytes
Kind: static property of bytes
Params
stringSequence // res is [1, 2, 3, 4, 5, 6]:
let res = From([ 1, 2, 3 ], [4, 5, 6])
.pipe(FromWords)
.pipe(ToArray)
.read();
FromWords 'flattens' a sequence of words (32 bit integers) into a sequence of elements.
Kind: static property of bytes
Returns: Sequence - - a sequence of bytes
Params
Sequence - a sequence of wordsboolean - an optional parameter to set the byte order, default trueSequence // res is '000102':
let res = From(1, 2, 3)
.pipe(ToHex)
.read();
ToHex converts a sequence into an array.
NOTE: ToHex will always return exactly once. If the source sequence is empty,
ToHex will return an empty string.
Kind: static property of bytes
Params
Sequence - the source sequenceSequence // res is [1, 2, 3, 4, 5, 6]:
let res = From([ 1, 2, 3 ], [4, 5, 6])
.pipe(ToWords)
.pipe(ToArray)
.read();
ToWords 'flattens' a sequence of words (32 bit integers) into a sequence of elements.
Kind: static property of bytes
Returns: Sequence - - a sequence of bytes
Params
Sequence - a sequence of wordsboolean - an optional parameter to set the byte order, default trueobjectKind: static namespace of sequences
object
SequenceSequenceSequenceSequenceSequenceSequence
let RandomBoolean = require('sequences/random/RandomBoolean');
*
RandomBoolean is a Sequence pseudo-random number generator that returns a random boolean.
Kind: static property of random
Params
number - an optional 32 bit seedSequence
let RandomInteger = require('sequences/random/RandomInteger');
*
RandomInteger is a Sequence pseudo-random number generator that returns a random int between min and max, inclusive.
RandomInteger returns in the range [0, 1] by default.
RandomInteger has 32 bits of precision.
Kind: static property of random
Params
number - the minimum possible integer to returnnumber - the maximum possible integer to returnnumber - an optional 32 bit seedSequence
let Random = require('sequences/random/Random');
*
Random is a Sequence pseudo-random number generator that returns a random number between min and max, inclusive.
Random returns in the range [0, 1] by default.
Random has 32 bits of precision.
Kind: static property of random
Params
numbernumbernumber - an optional 32 bit seedSequence
let RandomSelection = require('sequences/random/RandomSelection');
*
RandomSelection is a Sequence generator that returns a random relection from the choices.
Kind: static property of random
Params
Array - the selection choicesnumber - an optional 32 bit seedSequence
let XORShift32 = require('sequences/random/XORShift32');
*
XORShift32 is a Sequence implementation of the XORShift32 PRNG algorithm
Kind: static property of random
Params
number - an optional 32 bit seedFAQs
A set of tools for quickly processing data in a streaming way
The npm package sequences receives a total of 44 weekly downloads. As such, sequences popularity was classified as not popular.
We found that sequences demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.