UNPKG

@bookbox/generator-js

Version:

Bookbox generator for js code

83 lines (82 loc) 2.61 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPureSchema = getPureSchema; exports.getBookSchema = getBookSchema; const defaultApi_1 = require("./defaultApi"); function isBookResult(x) { return typeof x === "object" && x !== null && "schema" in x; } /** * Составление дерева с учётом границ */ function getPureSchema(schema) { const result = []; for (const item of schema) { if (typeof item === "string") { result.push(item); } else if (typeof item === "number") { result.push(`${item}`); } else if (item === null) { result.push(""); } else if (typeof item === "boolean") { result.push(`${+item}`); } else if (isBookResult(item)) { // вложенные книги result.push(...getPureSchema(item.schema)); } else if ((typeof item === "object" && item.prototype && item instanceof Proxy) || typeof item === "function") { result.push(...getPureSchema([item()])); } else if ("__start" in item) { // текущая область result.push({ name: item.__start, props: item.props, marker: 'start', children: [], }); } else if ("__end" in item) { result.push({ name: item.__end, props: item.props, marker: 'end', children: [], }); } else { item.children = getPureSchema(item.children); result.push(item); } } return result; } function getBookSchema(_a) { var { api = defaultApi_1.defaultBookApi } = _a, params = __rest(_a, ["api"]); let rawSchema; if ("rawSchema" in params) { rawSchema = params.rawSchema; } else { rawSchema = params.book(api).schema; } return { schema: getPureSchema(rawSchema) }; }