How to use highland

Comprehensive highland code examples:

How to use highland.values:

62
63
64
65
66
67
68
69
70
71
  yield this._db.waitForWrites(next.arg(0));
}

// load/ripley state
ripley(db, dispatch, next) {
  const ripley = _.values(this._db.loadState('ripley'));
  console.log('Starting ripley of db : ' + db);
  ripley.toArray((actions) => {
    _(
      traverse(actions).reduce((acc, node) => {

How to use highland.isArray:

12
13
14
15
16
17
18
19
20
21
var isString = function(val) {
  return typeof val === 'string';
};

var arrayElement = function(val) {
  return _.isArray(val) ? val : [val];
};

// check if target contains all
// properties and values of source

How to use highland.concat:

180
181
182
183
184
185
186
187
188
189
function getLogItemInRange (segments, start, end) {
  var streams = segments
      .reverse()
      .map(function (seg) { return fs.createReadStream(findLog(seg.file)) })
  if (streams.length > 1) {
    streams = hl.concat.apply(this, streams)
  } else {
    streams = hl(streams[0])
  }
  return streams.pipe(

How to use highland.parallel:

16
17
18
19
20
21
22
23
24
25
26
  __.map(wrapPutRequest),
  __.batch(batch),
  __.map(defaultDocumentClient.wrapBatchWriteRequest(TableName)),
  __.map(batch => defaultDocumentClient.batchWrite(batch)),
  __.map(__),
  __.parallel(parallel) // TODO fix lint rules, we want a comma here.
)


/*
  eslint-disable unicorn/no-array-for-each, unicorn/no-null

How to use highland.doto:

191
192
193
194
195
196
197
198
199
200
// @ts-ignore
_.mergeWithLimit(config.workers),

// * verbose
// @ts-ignore
_.doto(() => {
        if (config.verbose) showProgress(config.recordType, config.recordsProcessed, config.requests);
}),

// * errors

How to use highland.append:

45
46
47
48
49
50
51
52
53
stream.cwd = options.cwd || process.cwd();

function create() {
    return _.pipeline(
        _.map(split),
        _.append(cache),
        _.flatten
    );
}

How to use highland.flip:

328
329
330
331
332
333
334
335
336
337
 */
var createEnrichmentStream = function(options) {
  return highland.pipeline(function(sourceStream) {
    options = options || {};
    var enrichWithProvidedOptions = highland.partial(
      highland.flip(enrich),
      options
    );
    return highland(sourceStream).flatMap(enrichWithProvidedOptions);
  });

How to use highland.extend:

112
113
114
115
116
117
118
119
120
121
    if (data === _.nil || !has(where, data)) {
      next();
      return;
    }
    args.forEach(function(obj) {
      push(null, _.extend(obj, data));
    });
    next();
  });
};

How to use highland.errors:

197
198
199
200
201
202
203
204
205
                if (config.verbose) showProgress(config.recordType, config.recordsProcessed, config.requests);
        }),

        // * errors
        // @ts-ignore
        _.errors((e) => {
                throw e;
        })
);

How to use highland.compact:

196
197
198
199
200
201
202
203
204
205
        parsed.body = body;
        parsed.error = e.toString();
      }
      return parsed;
    }),
    _h.compact()
  );
};

return _h.pipeline(

How to use highland.flatMap:

202
203
204
205
206
207
208
209
210
211
  );
};

return _h.pipeline(
  _h.batch(_this.batchSize),
  _h.flatMap(_h.wrapCallback(function(doc, callback) {
    _this.rl.getTokens(_this.quotaSize * _this.batchSize, () => callback(null, doc));
  })),
  _h.map(mapToMultipartRequest),
  _h.map(batch => _h(request(batch).pipe(processingPipeline(_this.filterErrors)))),

How to use highland.invoke:

173
174
175
176
177
178
179
180
181
182
  return parsed;
};

const processingPipeline = function(filterErrors) {
  return _h.pipeline(
    _h.invoke('toString', ['utf8']),
    _h.through(parseMultiPart()),
    _h.map(parseHttpResponse),
    _h.map(function(doc) {
      if (filterErrors && !(doc && doc.statusCode && parseInt(doc.statusCode, 10) === 200)) {

How to use highland.isStream:

21
22
23
24
25
26
27
28
29
30
                }
                next()
        }
}

if (s && _.isStream(s)) {
        return s.consume(consumer);
} else {
        return consumer;
}

How to use highland.through:

23
24
25
26
27
28
29
30
31
32
    endkey: '_design0',
    include_docs: true
  }, done);
}

return _.through(function(source) {
  var sourceEnded = false;

  var target = _(function(push, done) {
    // on db create, or update

How to use highland.pairs:

171
172
173
174
175
176
177
178
179
}

alternateFilters = alternateFilters || [];

var getPairStream = function() {
  return highland.pairs(args).filter(function(pair) {
    return selectedKeys.indexOf(pair[0]) > -1;
  });
};

How to use highland.mergeWithLimit:

187
188
189
190
191
192
193
194
195
196
        return flush(batch, config);
}),

// * concurrency
// @ts-ignore
_.mergeWithLimit(config.workers),

// * verbose
// @ts-ignore
_.doto(() => {

How to use highland.curry:

How to use highland.batch:

172
173
174
175
176
177
178
179
180
181
        return data;
}),

// * batch for # of items
// @ts-ignore
_.batch(config.recordsPerBatch),

// * batch for req size
// @ts-ignore
_.consume(chunkForSize(config)),

How to use highland.drop:

34
35
36
37
38
39
40
41
42
43
  constructor(count) {
    this._count = count;
  }

  apply() {
    return h.drop(this._count || 0);
  }
};

class Filter {

How to use highland.split:

61
62
63
64
65
66
67
68
69
70
  }
};

class Split {
  apply() {
    return h.split();
  }
};

class ParseJSON extends Map {

How to use highland.otherwise:

55
56
57
58
59
60
61
62
63
64
    this._ys = ys;
  }


  apply() {
    return h.otherwise(this._ys);
  }
};

class Split {

How to use highland.collect:

24
25
26
27
28
29
30
31
32
33
  }
};

class Collect {
  apply() {
    return h.collect();
  }
};

class Drop {

How to use highland.consume:

21
22
23
24
25
26
27
28
29
30
      p(x).then((m) => {
        push(null, m)
      })
    }
  }
  return H.consume(ep)
}

H((push, next) => {
  console.log('*** GENERATOR ***', genc++)

How to use highland.compose:

49
50
51
52
53
54
55
56
57
58
  .flatMap($.compose($, pause.waitFor(pauser))) // pause _before_ the map
  .map($.add(5))
  .each($.partial($.log, "str1 end"));

/*
    $.compose($, pause.waitFor(pauser)) ==

    function(x) {
      return $(pause.waitFor(pauser)(x))
    }

How to use highland.filter:

44
45
46
47
48
49
50
51
52
53
  constructor(fn) {
    this._fn = fn;
  }

  apply() {
    return h.filter(this._fn);
  }
};

class Otherwise {

How to use highland.flatten:

188
189
190
191
192
193
194
195
196
197
// Cap the number of instances for all activities
// Example: concurrency=1 says only 1 instance of an activity at a time.
const concurrency = options.concurrency || DEFAULT_CONCURRENCY;

// Create a stream that represents the results
let input = $.flatten(source);

const results = options.pipeline
    ? pipelined(input, this.activities, concurrency)
    : oneAtATime(input, this.activities, concurrency);

How to use highland.streamifyAll:

0
1
2
3
4
5
6
7
8
9
10
11
'use strict';
const R = require('ramda');
const hl = require('highland');
const path = require('path');
let cfn = require('cfn').class;
let fs = hl.streamifyAll(require('fs'));


let log = console.log; // eslint-disable-line no-console


let resolveAbsolutePath = p => path.resolve(process.cwd(), p);

How to use highland.fromError:

134
135
136
137
138
139
140
141
142
143
},

async 'rejects on failure'() {

    const error = new Error('oh no!');
    patch(this.tap, 'streamMessages', () => Highland.fromError(error));

    const startPromise = this.start();

    try {

How to use highland.of:

8
9
10
11
12
13
14
15
16
17
18
19


let resolveAbsolutePath = p => path.resolve(process.cwd(), p);


let resolveTemplate = template => {
    if (!/https:\/\//.test(template)) {
        return hl.of(template)
            .map(resolveAbsolutePath)
            .flatMap(fs.statStream);
    } else {
        return hl.of(1);

How to use highland.map:

7
8
9
10
11
12
13
14
15
16
  constructor(fn) {
    this._fn = fn;
  }

  apply() {
    return h.map(this._fn);
  }
};

class DoTo extends Map {