@sinclair/typebox
Version:
Json Schema Type Builder with Static Type Resolution for TypeScript
98 lines (96 loc) • 4.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IndexFromPropertyKey = IndexFromPropertyKey;
exports.IndexFromPropertyKeys = IndexFromPropertyKeys;
exports.Index = Index;
const type_1 = require("../create/type");
const index_1 = require("../computed/index");
const index_2 = require("../literal/index");
const index_3 = require("../never/index");
const index_4 = require("../intersect/index");
const index_5 = require("../union/index");
// ------------------------------------------------------------------
// Infrastructure
// ------------------------------------------------------------------
const indexed_property_keys_1 = require("./indexed-property-keys");
const indexed_from_mapped_key_1 = require("./indexed-from-mapped-key");
const indexed_from_mapped_result_1 = require("./indexed-from-mapped-result");
// ------------------------------------------------------------------
// KindGuard
// ------------------------------------------------------------------
const kind_1 = require("../guard/kind");
const value_1 = require("../guard/value");
// prettier-ignore
function FromRest(types, key) {
return types.map(left => IndexFromPropertyKey(left, key));
}
// prettier-ignore
function FromIntersectRest(types) {
return types.filter(left => !(0, kind_1.IsNever)(left));
}
// prettier-ignore
function FromIntersect(types, key) {
return ((0, index_4.IntersectEvaluated)(FromIntersectRest(FromRest(types, key))));
}
// prettier-ignore
function FromUnionRest(types) {
return (types.some(L => (0, kind_1.IsNever)(L))
? []
: types);
}
// prettier-ignore
function FromUnion(types, key) {
return ((0, index_5.UnionEvaluated)(FromUnionRest(FromRest(types, key))));
}
// prettier-ignore
function FromTuple(types, key) {
return (key === '[number]' ? (0, index_5.UnionEvaluated)(types) :
key in types ? types[key] :
(0, index_3.Never)());
}
// prettier-ignore
function FromArray(type, key) {
// ... ?
return (key === '[number]' ? type : (0, index_3.Never)());
}
// prettier-ignore
function FromProperty(properties, key) {
return (key in properties ? properties[key] : (0, index_3.Never)());
}
// prettier-ignore
function IndexFromPropertyKey(type, key) {
return ((0, kind_1.IsIntersect)(type) ? FromIntersect(type.allOf, key) :
(0, kind_1.IsUnion)(type) ? FromUnion(type.anyOf, key) :
(0, kind_1.IsTuple)(type) ? FromTuple(type.items ?? [], key) :
(0, kind_1.IsArray)(type) ? FromArray(type.items, key) :
(0, kind_1.IsObject)(type) ? FromProperty(type.properties, key) :
(0, index_3.Never)());
}
// prettier-ignore
function IndexFromPropertyKeys(type, propertyKeys) {
return propertyKeys.map(left => IndexFromPropertyKey(type, left));
}
// prettier-ignore
function FromType(type, propertyKeys) {
const result = IndexFromPropertyKeys(type, propertyKeys);
return (0, index_5.UnionEvaluated)(result);
}
// prettier-ignore
function UnionFromPropertyKeys(propertyKeys) {
const result = propertyKeys.reduce((result, key) => (0, kind_1.IsLiteralValue)(key) ? [...result, (0, index_2.Literal)(key)] : result, []);
return (0, index_5.UnionEvaluated)(result);
}
/** `[Json]` Returns an Indexed property type for the given keys */
// prettier-ignore
function Index(type, key, options) {
const typeKey = (0, value_1.IsArray)(key) ? UnionFromPropertyKeys(key) : key;
const propertyKeys = (0, kind_1.IsSchema)(key) ? (0, indexed_property_keys_1.IndexPropertyKeys)(key) : key;
const isTypeRef = (0, kind_1.IsRef)(type);
const isKeyRef = (0, kind_1.IsRef)(key);
return ((0, kind_1.IsMappedResult)(key) ? (0, indexed_from_mapped_result_1.IndexFromMappedResult)(type, key, options) :
(0, kind_1.IsMappedKey)(key) ? (0, indexed_from_mapped_key_1.IndexFromMappedKey)(type, key, options) :
(isTypeRef && isKeyRef) ? (0, index_1.Computed)('Index', [type, typeKey], options) :
(!isTypeRef && isKeyRef) ? (0, index_1.Computed)('Index', [type, typeKey], options) :
(isTypeRef && !isKeyRef) ? (0, index_1.Computed)('Index', [type, typeKey], options) :
(0, type_1.CreateType)(FromType(type, propertyKeys), options));
}