dbmaster-cli
Version:
Tool for converting tables between Fifa Soccer Games
37 lines (36 loc) • 1.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtendContractTransform = void 0;
const stream_1 = require("stream");
const fifa_config_1 = require("../fifa-config");
const generators_1 = require("../generators");
class ExtendContractTransform extends stream_1.Transform {
constructor(opts) {
super(opts);
this.opts = opts;
this.vb = new fifa_config_1.ValidationBuilder(this.opts.fields);
this.dg = new generators_1.DateGenerator(this.opts.refDate);
}
_transform(chunk, encoding, callback) {
let object = JSON.parse(chunk.toString());
object = this.updateField(object, 'contractvaliduntil', (v) => v < this.opts.refDate.getFullYear(), () => this.dg.contractValidUntil());
object = this.updateField(object, 'loandateend', (v) => v < this.opts.refDate.toFifaDate(), () => this.dg.loanDateEnd());
this.push(JSON.stringify(object));
callback();
}
_flush(callback) {
callback();
}
updateField(object, fieldName, condition, setValue) {
const field = this.opts.fields.find((f) => f.name === fieldName);
if (field) {
const value = object[field.name];
const { error } = this.vb.validationFn(field).validate(value);
if (error || condition) {
object[field.name] = setValue();
}
}
return object;
}
}
exports.ExtendContractTransform = ExtendContractTransform;