@jsonjoy.com/json-type
Version:
High-performance JSON Pointer implementation
117 lines • 3.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjType = exports.KeyOptType = exports.KeyType = void 0;
const tslib_1 = require("tslib");
const printTree_1 = require("tree-dump/lib/printTree");
const schema = tslib_1.__importStar(require("../../schema"));
const AbsType_1 = require("./AbsType");
class KeyType extends AbsType_1.AbsType {
constructor(key, val) {
super(schema.s.Key(key, schema.s.any));
this.key = key;
this.val = val;
this.optional = false;
}
getSchema() {
return {
...this.schema,
value: this.val.getSchema(),
};
}
getOptions() {
// biome-ignore lint: unused variables are intentional
const { kind, key, value, optional, ...options } = this.schema;
return options;
}
toStringTitle() {
return JSON.stringify(this.key);
}
toString(tab = '') {
return super.toString(tab) + (0, printTree_1.printTree)(tab + ' ', [(tab) => this.val.toString(tab)]);
}
}
exports.KeyType = KeyType;
class KeyOptType extends KeyType {
constructor(key, val) {
super(key, val);
this.key = key;
this.val = val;
this.optional = true;
this.schema = schema.s.KeyOpt(key, schema.s.any);
}
toStringTitle() {
return JSON.stringify(this.key) + '?';
}
}
exports.KeyOptType = KeyOptType;
class ObjType extends AbsType_1.AbsType {
constructor(keys) {
super(schema.s.obj);
this.keys = keys;
}
_key(field, options) {
if (options)
field.options(options);
field.system = this.system;
this.keys.push(field);
}
/**
* Adds a property to the object type.
* @param key The key of the property.
* @param value The value type of the property.
* @param options Optional schema options for the property.
* @returns A new object type with the added property.
*/
prop(key, value, options) {
this._key(new KeyType(key, value), options);
return this;
}
/**
* Adds an optional property to the object type.
* @param key The key of the property.
* @param value The value type of the property.
* @param options Optional schema options for the property.
* @returns A new object type with the added property.
*/
opt(key, value, options) {
this._key(new KeyOptType(key, value), options);
return this;
}
getSchema() {
return {
...this.schema,
keys: this.keys.map((f) => f.getSchema()),
};
}
getOptions() {
const { kind: _, keys: _fields, ...options } = this.schema;
return options;
}
getField(key) {
return this.keys.find((f) => f.key === key);
}
extend(o) {
const type = new ObjType([...this.keys, ...o.keys]);
type.system = this.system;
return type;
}
omit(key) {
const type = new ObjType(this.keys.filter((f) => f.key !== key));
type.system = this.system;
return type;
}
pick(key) {
const field = this.keys.find((f) => f.key === key);
if (!field)
throw new Error('FIELD_NOT_FOUND');
const type = new ObjType([field]);
type.system = this.system;
return type;
}
toString(tab = '') {
return (super.toString(tab) +
(0, printTree_1.printTree)(tab, this.keys.map((field) => (tab) => field.toString(tab))));
}
}
exports.ObjType = ObjType;
//# sourceMappingURL=ObjType.js.map