-
Notifications
You must be signed in to change notification settings - Fork 0
/
emt-sequence-example.js
47 lines (40 loc) · 1.66 KB
/
emt-sequence-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var log = console.log
// , buffer = require( 'buffer' )
, fs = require( 'fs' )
, input = fs.createReadStream( './example/sample' )
, Sequence = require( '../lib/filters/emitters/sequence' )
// items
, i = 1024
// range [0-8]
, r = 9
, seq = new Sequence( i, r )
, onFeed = function ( bytes, used_ratio ) {
log( ' :feed, need other %d bytes, consumed: %d%', bytes, ( used_ratio * 100 ).toFixed( 2 ) );
}
, onFart = function ( result, used_ratio ) {
log( ' :fart %d bytes, consumed: %d%, buffer:', result.length, ( used_ratio * 100 ).toFixed( 2 ), result );
log();
}
, onRead = function () {
var data = null
;
if ( data = input.read() ) seq.parse( data );
}
;
/*
* For random sequences, how many bytes of random data will be consumed, depends primarly
* on the number of bits used to represent the selected range, for example, the selected
* range is 9 (0-8), the next power of 2 is 16, then we expect to discard 16-9=7 items,
* on the average, using 9/16 or ~56.25% of random values to produce requested results;
* it implies that we expect to consume ~2 bytes of random data for every byte of result.
*
* Try to use a power of 2 for range, like 8 or 16, use also 15, 17 and see the different
* results for random data consumption.
* See also notes in filters/emitters/sequence.js.
*/
log( '\n- created new sequence with %d items, within range [0,%d]', i, r - 1 );
seq.on( 'feed', onFeed );
seq.on( 'fart', onFart );
log( '- read random data from input source to build random result..' );
// buffer.INSPECT_MAX_BYTES = Infinity;
input.on( 'readable', onRead );