@e2fyi/streams
Version:
Nodejs stream library for various use cases: e.g. Auto-tagging object streams, streaming to mongoDb via mongoose models, etc.
111 lines (89 loc) • 3.3 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: src/document_tagger.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: src/document_tagger.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>`use strict`;
const {Transform} = require('stream');
const defaultOpts = {objectMode: true};
/**
* Settings for DocumentTagger.
* @memberof module:@e2fyi/streams
* @alias DocumentTaggerSettings
* @typedef {Object} DocumentTaggerSettings
* @property {String} autoIncrement - The auto-increment field to tag onto the
* stream object.
* @property {Function|Object} mutate - Function or Object to mutate the stream.
* If an Object is provided, each stream object will be mutated with the
* `Object.assign(streamObj, mutateObj)`.
*/
/**
* Transform Object stream (objectMode=true) to tag with an autoIncrement id.
* An object or function can be optionally provided to mutate each object in
* the stream.
* @memberof module:@e2fyi/streams
* @extends stream.Transform
* @example
* const docTagger = new DocumentTagger({autoIncrement: 'id', mutate: { project: 'test' }});
* someReadableStreamFromArray([{text: 'abc'}, {text: 'efg'}])
* .pipe(docTagger)
* .pipe(process.stdout);
* // stdout >
* // {"text": "abc", "id": 0, "project": "test"}
* // {"text": "efg", "id": 1, "project": "test"}
*/
class DocumentTagger extends Transform {
/**
* Create a new DocumentTagger stream.
* @param {DocumentTaggerSettings} opts Settings for the stream.
*/
constructor(opts = defaultOpts) {
opts = Object.assign(opts, defaultOpts);
super(opts);
this._counter_ = 0;
this._autoIncrement_ = opts.autoIncrement;
if (opts.mutate) {
this._mutate_ =
typeof opts.mutate === 'function'
? opts.mutate
: d => Object.assign(d, opts.mutate);
}
}
_transform(chunk, encoding, callback) {
if (chunk) {
if (this._autoIncrement_ && this._counter_ >= 0)
chunk[this._autoIncrement_] = this._counter_++;
if (this._mutate_) chunk = this._mutate_(chunk) || chunk;
callback(null, chunk);
}
}
}
module.exports = DocumentTagger;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@e2fyi_streams.html">@e2fyi/streams</a></li></ul><h3>Classes</h3><ul><li><a href="module-@e2fyi_streams.DocumentTagger.html">DocumentTagger</a></li><li><a href="module-@e2fyi_streams.MongooseStream.html">MongooseStream</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.0</a> on Wed Dec 13 2017 18:11:13 GMT+0800 (+08)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>