@public-ui/components
Version:
Contains all web components that belong to KoliBri - The accessible HTML-Standard.
71 lines (70 loc) • 2.33 kB
JavaScript
/*!
* KoliBri - The accessible HTML-Standard
*/
import { cloneDeep, isObject } from "lodash-es";
import { Log } from "../../../schema";
function safeStringify(value) {
try {
return JSON.stringify(value);
}
catch (_a) {
return '[unserializable]';
}
}
export function createPropDefinition(propName, defaultValue, normalize, validate = () => true) {
return {
propName,
getDefaultValue: isObject(defaultValue) ? () => cloneDeep(defaultValue) : () => defaultValue,
normalize,
validate,
apply(value, callback) {
if (value === undefined || value === null) {
if (this.validate(defaultValue)) {
callback(defaultValue);
}
else {
throw new Error(`Default value ${safeStringify(defaultValue)} is invalid for prop definition '${propName}'.`);
}
return;
}
try {
const normalized = this.normalize(value);
if (this.validate(normalized)) {
callback(normalized);
}
}
catch (e) {
Log.debug(e);
}
},
};
}
export function createDependentPropDefinition(propName, defaultValue, normalize, validate = () => true) {
return {
propName,
getDefaultValue: isObject(defaultValue) ? () => cloneDeep(defaultValue) : () => defaultValue,
normalize,
validate,
apply(value, callback, deps) {
if (value === undefined || value === null) {
if (this.validate(defaultValue, deps)) {
callback(defaultValue);
}
else {
throw new Error(`Default value ${safeStringify(defaultValue)} is invalid for prop definition '${propName}' with dependencies ${safeStringify(deps)}.`);
}
return;
}
try {
const normalized = this.normalize(value, deps);
if (this.validate(normalized, deps)) {
callback(normalized);
}
}
catch (e) {
Log.debug(e);
}
},
};
}
//# sourceMappingURL=factory.js.map