alinea
Version:
[](https://npmjs.org/package/alinea) [](https://packagephobia.com/result?p=alinea)
60 lines (58 loc) • 1.64 kB
JavaScript
import "../chunks/chunk-U5RRZUYZ.js";
// src/core/Section.ts
import { Field } from "./Field.js";
import { createId } from "./Id.js";
import { assign, create, defineProperty, entries } from "./util/Objects.js";
var Section;
((Section2) => {
Section2.Data = Symbol.for("@alinea/Section.Data");
Section2.PREFIX = "@@@";
function provideView(view2, factory) {
return (...args) => {
const section2 = factory(...args);
section2[Section2.Data].view = view2;
return section2;
};
}
Section2.provideView = provideView;
function view(section2) {
return section2[Section2.Data].view;
}
Section2.view = view;
function definition(section2) {
return section2[Section2.Data].definition;
}
Section2.definition = definition;
function fields(section2) {
return section2[Section2.Data].fields;
}
Section2.fields = fields;
function isSection(value) {
return Boolean(value && value[Section2.Data]);
}
Section2.isSection = isSection;
})(Section || (Section = {}));
function section(data) {
const section2 = create(null);
const fields = create(null);
for (const [key, value] of entries(data.definition)) {
if (Field.isField(value)) {
defineProperty(section2, key, { value, enumerable: false });
fields[key] = value;
} else if (Section.isSection(value)) {
assign(fields, Section.fields(value));
}
}
section2[`${Section.PREFIX}${createId()}`] = section2;
if (!data.fields)
assign(data, { fields });
defineProperty(section2, Section.Data, {
value: data,
enumerable: false
});
return section2;
}
export {
Section,
section
};