succulent
Version:
Powerful and easy runtime type checking
32 lines • 1.16 kB
JavaScript
import { Schema } from "../schema";
export function $Map(keySchemaBase, valueSchemaBase) {
const keySchema = Schema.from(keySchemaBase);
const valueSchema = Schema.from(valueSchemaBase);
const keyTypeName = keySchema.displayName;
const valueTypeName = valueSchema.displayName;
return new Schema((x) => {
if (!(x instanceof Map)) {
return false;
}
for (const [key, value] of x) {
keySchema.check(key);
valueSchema.check(value);
}
// If we made it through the whole map, and nothing failed, then everything passed!
return true;
}, { displayName: `Map<${keyTypeName}, ${valueTypeName}>` });
}
export function $Set(schemaBase) {
const schema = Schema.from(schemaBase);
return new Schema((x) => {
if (!(x instanceof Set)) {
return false;
}
for (const key of x) {
schema.check(key);
}
// If we made it through the whole set, and nothing failed, then everything passed!
return true;
}, { displayName: `Set<${schema.displayName}>` });
}
//# sourceMappingURL=collections.js.map