Files
NapCatQQ/.pnpm-store/v3/files/74/67b3bc66c010c101859442b7061290e2dc36c61dc258641ed74388b1f4329dfe805038128b332e6ec5e08bfc350091913e3712ac943531afd8505d5eb357b8
TanTan e311ca3275
Build NapCat Artifacts / Windows Loader Test (push) Canceled after 0s
Build NapCat Artifacts / Type Check & Test (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Framework (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Shell (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

74 lines
2.7 KiB
Plaintext

import { SetUnionMany, SetIntersectMany } from '../sets/index.mjs';
// ------------------------------------------------------------------
// TypeGuard
// ------------------------------------------------------------------
import { IsIntersect, IsUnion, IsTuple, IsArray, IsObject, IsRecord } from '../guard/kind.mjs';
// prettier-ignore
function FromRest(types) {
const result = [];
for (const L of types)
result.push(KeyOfPropertyKeys(L));
return result;
}
// prettier-ignore
function FromIntersect(types) {
const propertyKeysArray = FromRest(types);
const propertyKeys = SetUnionMany(propertyKeysArray);
return propertyKeys;
}
// prettier-ignore
function FromUnion(types) {
const propertyKeysArray = FromRest(types);
const propertyKeys = SetIntersectMany(propertyKeysArray);
return propertyKeys;
}
// prettier-ignore
function FromTuple(types) {
return types.map((_, indexer) => indexer.toString());
}
// prettier-ignore
function FromArray(_) {
return (['[number]']);
}
// prettier-ignore
function FromProperties(T) {
return (globalThis.Object.getOwnPropertyNames(T));
}
// ------------------------------------------------------------------
// FromPatternProperties
// ------------------------------------------------------------------
// prettier-ignore
function FromPatternProperties(patternProperties) {
if (!includePatternProperties)
return [];
const patternPropertyKeys = globalThis.Object.getOwnPropertyNames(patternProperties);
return patternPropertyKeys.map(key => {
return (key[0] === '^' && key[key.length - 1] === '$')
? key.slice(1, key.length - 1)
: key;
});
}
/** Returns a tuple of PropertyKeys derived from the given TSchema. */
// prettier-ignore
export function KeyOfPropertyKeys(type) {
return (IsIntersect(type) ? FromIntersect(type.allOf) :
IsUnion(type) ? FromUnion(type.anyOf) :
IsTuple(type) ? FromTuple(type.items ?? []) :
IsArray(type) ? FromArray(type.items) :
IsObject(type) ? FromProperties(type.properties) :
IsRecord(type) ? FromPatternProperties(type.patternProperties) :
[]);
}
// ----------------------------------------------------------------
// KeyOfPattern
// ----------------------------------------------------------------
let includePatternProperties = false;
/** Returns a regular expression pattern derived from the given TSchema */
export function KeyOfPattern(schema) {
includePatternProperties = true;
const keys = KeyOfPropertyKeys(schema);
includePatternProperties = false;
const pattern = keys.map((key) => `(${key})`);
return `^(${pattern.join('|')})$`;
}