@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
38 lines (37 loc) • 1.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformMapSimple = void 0;
const stream_1 = require("stream");
const js_lib_1 = require("@naturalcycles/js-lib");
/**
* Simplest version of `transformMap`.
* errorMode: IMMEDIATE
* Sync mode.
* Has 0 options to configure.
* If you need any configuration - use transformMap or transformMapSync.
* Sync (not async) version of transformMap.
* Supposedly faster, for cases when async is not needed.
*/
function transformMapSimple(mapper, opt = {}) {
let index = -1;
const { errorMode = js_lib_1.ErrorMode.THROW_IMMEDIATELY, logger = console } = opt;
return new stream_1.Transform({
objectMode: true,
transform(chunk, _, cb) {
try {
cb(null, mapper(chunk, ++index));
}
catch (err) {
logger.error(err);
if (errorMode === js_lib_1.ErrorMode.SUPPRESS) {
cb(); // suppress the error
}
else {
// Emit the error
cb(err);
}
}
},
});
}
exports.transformMapSimple = transformMapSimple;