UNPKG

diffusion

Version:

Diffusion JavaScript client

188 lines (187 loc) 8.1 kB
"use strict"; /** * @module diffusion.datatypes.RecordV2 */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.MutableRecordModelImpl = void 0; var errors_1 = require("./../../../../errors/errors"); var abstract_record_model_1 = require("./../../../data/record/model/abstract-record-model"); var field_impl_1 = require("./../../../data/record/schema/field-impl"); /** * Implementation of {@link MutableRecordModel} */ var MutableRecordModelImpl = /** @class */ (function (_super) { __extends(MutableRecordModelImpl, _super); /** * Create a MutableRecordModelImpl * * @param recordV2Constructor a constructor function for creating {@link RecordV2} implementations * @param schema the schema that this model corresponds to */ function MutableRecordModelImpl(recordV2Constructor, schema) { var _this = _super.call(this, recordV2Constructor, schema) || this; _this.recordData = schema.createModel(); return _this; } /** * @inheritdoc */ MutableRecordModelImpl.prototype.model = function () { return this.recordData; }; /** * @inheritdoc */ MutableRecordModelImpl.prototype.set = function (recordName, recordIndexOrFieldNameOrValue, fieldNameOrValue, fieldIndex, value) { if (fieldIndex === void 0) { fieldIndex = 0; } var recordIndex; var fieldName; switch (arguments.length) { case 2: value = recordIndexOrFieldNameOrValue; var key = this.parseKey(recordName); recordName = key.recordName; recordIndex = key.recordIndex; fieldName = key.fieldName; fieldIndex = key.fieldIndex; break; case 3: value = fieldNameOrValue; fieldName = recordIndexOrFieldNameOrValue; recordIndex = 0; break; case 5: recordIndex = recordIndexOrFieldNameOrValue; fieldName = fieldNameOrValue; break; /* istanbul ignore next */ default: recordIndex = 0; fieldName = ''; } var schemaRecord = this.schema().getRecord(recordName); var record = this.getRecord(schemaRecord, recordIndex); var schemaField = schemaRecord.getField(fieldName); record[abstract_record_model_1.resolveFieldIndex(schemaField, record, fieldIndex)] = field_impl_1.normalise(schemaField, value); return this; }; /** * @inheritdoc */ MutableRecordModelImpl.prototype.add = function (recordName, recordIndexOrValue) { var values = []; for (var _i = 2; _i < arguments.length; _i++) { values[_i - 2] = arguments[_i]; } var schemaRecord = this.schema().getRecord(recordName); var record; if (typeof recordIndexOrValue === 'number') { record = this.getRecord(schemaRecord, recordIndexOrValue); } else { values.unshift(recordIndexOrValue); if (!schemaRecord.isVariable) { record = this.getRecord(schemaRecord, Math.max(schemaRecord.max - 1, 0)); } else if (this.recordData.length > schemaRecord.index) { record = this.recordData[this.recordData.length - 1]; } else { throw new errors_1.SchemaViolationError("Variable record '" + schemaRecord.name + "' has no occurrences to add a field to"); } } this.addValuesToRecord(schemaRecord, record, values); return this; }; /** * @inheritdoc */ MutableRecordModelImpl.prototype.addRecord = function () { var schemaRecord = this.schema().lastRecord(); if (!schemaRecord.isVariable) { throw new errors_1.SchemaViolationError("Record '" + schemaRecord.name + "' is not variable"); } var max = schemaRecord.max; if (max !== -1 && this.recordData.length - schemaRecord.index >= max) { throw new errors_1.SchemaViolationError("Record '" + schemaRecord.name + "' already has maximum number of occurrences"); } this.recordData.push(schemaRecord.createModel()); return this; }; /** * @inheritdoc */ MutableRecordModelImpl.prototype.removeRecord = function (index) { var schemaRecord = this.schema().lastRecord(); if (!schemaRecord.isVariable) { throw new errors_1.SchemaViolationError("Record '" + schemaRecord.name + "' is not variable"); } var actualIndex = abstract_record_model_1.resolveRecordIndex(schemaRecord, this.recordData, index); if (this.recordData.length - schemaRecord.index - 1 < schemaRecord.min) { throw new errors_1.SchemaViolationError("Removing an occurrence of record '" + schemaRecord.name + "' would " + "violate the minimum number of occurrences"); } this.recordData.splice(actualIndex, 1); return this; }; /** * @inheritdoc */ MutableRecordModelImpl.prototype.removeField = function (recordName, recordIndex, fieldIndex) { var schemaRecord = this.schema().getRecord(recordName); var record = this.getRecord(schemaRecord, recordIndex); var schemaField = schemaRecord.lastField(); if (!schemaField.isVariable) { throw new errors_1.SchemaViolationError("Field '" + schemaField.name + "' is not a variable field"); } var actualFieldIndex = abstract_record_model_1.resolveFieldIndex(schemaField, record, fieldIndex); if (record.length - schemaField.index - 1 < schemaField.min) { throw new errors_1.SchemaViolationError("Removing an occurrence of field '" + schemaField.name + "' would " + "violate the minimum number of occurrences"); } record.splice(actualFieldIndex, 1); return this; }; /** * Add values to the last field in a record. Values can only be added if the * record is variable * * @param schemaRecord the schema of the record * @param record the record's model data * @param values the values to add * @throws an error if the record is not variable */ MutableRecordModelImpl.prototype.addValuesToRecord = function (schemaRecord, record, values) { var schemaField = schemaRecord.lastField(); if (!schemaField.isVariable) { throw new errors_1.SchemaViolationError("Final field of record '" + schemaRecord.name + "' is not variable"); } var max = schemaField.max; if (max !== -1) { var current = record.length - schemaField.index; if (current + values.length > max) { throw new errors_1.SchemaViolationError('Adding values would exceed maximum number of field occurrences'); } } values.forEach(function (value) { record.push(field_impl_1.normalise(schemaField, value)); }); }; return MutableRecordModelImpl; }(abstract_record_model_1.AbstractRecordModel)); exports.MutableRecordModelImpl = MutableRecordModelImpl;