@configurator/ravendb
Version:
RavenDB client for Node.js
46 lines • 2.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectKeyCaseTransformStream = void 0;
const stream = require("readable-stream");
const ObjectUtil_1 = require("../../../Utility/ObjectUtil");
const TypeUtil_1 = require("../../../Utility/TypeUtil");
const DEFAULT_OBJECT_KEY_CASE_TRANSFORM_OPTS = {
arrayRecursive: true,
recursive: true
};
class ObjectKeyCaseTransformStream extends stream.Transform {
constructor(_opts) {
super({ objectMode: true });
this._opts = _opts;
this._getIgnorePaths = () => this._ignorePaths;
this._opts = Object.assign({}, DEFAULT_OBJECT_KEY_CASE_TRANSFORM_OPTS, this._opts);
ObjectKeyCaseTransformStream._validateOpts(_opts);
if (typeof _opts.extractIgnorePaths === "function") {
this._getIgnorePaths = _opts.extractIgnorePaths;
}
this._handleKeyValue = _opts.handleKeyValue;
}
_transform(chunk, enc, callback) {
let entry = this._handleKeyValue ? chunk["value"] : chunk;
const key = chunk["key"];
if (TypeUtil_1.TypeUtil.isPrimitive(entry) || TypeUtil_1.TypeUtil.isNullOrUndefined(entry)) {
return callback(null, chunk);
}
const ignorePaths = this._getIgnorePaths(entry);
const opts = Object.assign({}, this._opts);
opts.ignorePaths = [...new Set((opts.ignorePaths || [])
.concat(ignorePaths || []))];
entry = ObjectUtil_1.ObjectUtil.transformObjectKeys(entry, opts);
const data = this._handleKeyValue
? { key, value: entry }
: entry;
callback(null, data);
}
static _validateOpts(opts) {
if (opts.defaultTransform && !ObjectUtil_1.ObjectUtil[opts.defaultTransform]) {
throw new Error(`Unknown key casing convention: ${opts.defaultTransform}`);
}
}
}
exports.ObjectKeyCaseTransformStream = ObjectKeyCaseTransformStream;
//# sourceMappingURL=ObjectKeyCaseTransformStream.js.map