model-layer
Version:
81 lines • 2.65 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CollectionType = exports.MakeCollectionType = void 0;
const Type_1 = require("./Type");
const Collection_1 = __importDefault(require("../Collection"));
const utils_1 = require("../utils");
const errors_1 = require("../errors");
function MakeCollectionType(params) {
return {
...params,
type: "collection"
};
}
exports.MakeCollectionType = MakeCollectionType;
MakeCollectionType.isTypeHelper = true;
class CollectionType extends Type_1.Type {
constructor(params) {
super(params);
this.nullAsEmpty = !!params.nullAsEmpty;
this.Collection = params.Collection;
}
static prepareDescription(description) {
const isCollection = (typeof description.type === "function" &&
description.type.prototype instanceof Collection_1.default);
if (!isCollection) {
return;
}
const CustomCollection = description.type;
description.type = "collection";
description.Collection = CustomCollection;
}
prepare(value, key) {
if (value == null) {
if (this.nullAsEmpty) {
value = [];
}
else {
return null;
}
}
const CustomCollection = this.Collection;
const className = CustomCollection.name;
if (value instanceof CustomCollection) {
return value;
}
if (Array.isArray(value)) {
value = new CustomCollection(value);
return value;
}
const valueAsString = utils_1.invalidValuesAsString(value);
throw new errors_1.InvalidCollectionError({
className,
key,
invalidValue: valueAsString
});
}
typeAsString() {
return "collection " + this.Collection.name;
}
toJSON(collection, stack) {
if (stack.includes(collection)) {
throw new errors_1.CircularStructureToJSONError({});
}
stack.push(collection);
return collection.toJSON(stack);
}
clone(collection, stack) {
return collection.clone(stack);
}
equal(selfCollection, otherCollection, stack) {
if (selfCollection == null) {
return otherCollection === null;
}
return selfCollection.equal(otherCollection, stack);
}
}
exports.CollectionType = CollectionType;
//# sourceMappingURL=CollectionType.js.map