UNPKG

roc

Version:

Build modern web applications easily

269 lines (226 loc) 19.8 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../../"> <title data-ice="title">src/documentation/generate-table.js | API Document</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> <script src="script/manual.js"></script> </head> <body class="layout-container" data-ice="rootContainer"> <header> <a href="./">Home</a> <a href="identifiers.html">Reference</a> <a href="source.html">Source</a> <a data-ice="repoURL" href="https://github.com/vgno/roc.git" class="repo-url-github">Repository</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div> </header> <nav class="navigation" data-ice="nav"><div> <ul> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">cli</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-execute">execute</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-buildCompleteConfig">buildCompleteConfig</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-generateCommandDocumentation">generateCommandDocumentation</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-generateCommandsDocumentation">generateCommandsDocumentation</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getMappings">getMappings</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getSuggestions">getSuggestions</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-parseArguments">parseArguments</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-parseOptions">parseOptions</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-runCli">runCli</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">commands</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-init">init</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">commands/helpers</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-validRocProject">validRocProject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-get">get</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getVersions">getVersions</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-defaultPrompt">defaultPrompt</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">configuration</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getApplicationConfig">getApplicationConfig</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-appendConfig">appendConfig</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-appendSettings">appendSettings</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getConfig">getConfig</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getSettings">getSettings</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-merge">merge</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">documentation</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-buildDocumentationObject">buildDocumentationObject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-generateTable">generateTable</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-addPadding">addPadding</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getDefaultValue">getDefaultValue</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-pad">pad</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-toCliFlag">toCliFlag</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-generateMarkdownDocumentation">generateMarkdownDocumentation</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-generateTextDocumentation">generateTextDocumentation</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">helpers</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-fileExists">fileExists</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getAbsolutePath">getAbsolutePath</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getPackageJson">getPackageJson</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getRocDependencies">getRocDependencies</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-error">error</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-errorLabel">errorLabel</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-important">important</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-importantLabel">importantLabel</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-ok">ok</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-okLabel">okLabel</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-warning">warning</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-warningLabel">warningLabel</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">typedef</div><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-infoObject">infoObject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocCommandObject">rocCommandObject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocConfig">rocConfig</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocDocumentationObject">rocDocumentationObject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocMapObject">rocMapObject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocMetaConfig">rocMetaConfig</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocMetaSettings">rocMetaSettings</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocMetaSettings">rocMetaSettings</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocOptions">rocOptions</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocSettings">rocSettings</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocTableHeader">rocTableHeader</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-rocTableSettings">rocTableSettings</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">validation</div><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isValid">isValid</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-throwError">throwError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-validate">validate</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-validateMightThrow">validateMightThrow</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isArray">isArray</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isArrayOrSingle">isArrayOrSingle</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isBoolean">isBoolean</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isInteger">isInteger</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isObject">isObject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isPath">isPath</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isString">isString</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-oneOf">oneOf</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-required">required</a></span></span></li> </ul> </div> </nav> <div class="content" data-ice="content"><h1 data-ice="title">src/documentation/generate-table.js</h1> <pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import &apos;source-map-support/register&apos;; import deepExtend from &apos;deep-extend&apos;; import { isUndefined } from &apos;lodash&apos;; import stripAnsi from &apos;strip-ansi&apos;; import { pad, addPadding } from &apos;./helpers&apos;; const defaultSettings = { groupTitleWrapper: (name) =&gt; name, cellDivider: &apos;|&apos;, rowWrapper: (input) =&gt; `|${input}|`, cellWrapper: (input) =&gt; ` ${input} `, header: true, compact: false }; /** * Creates a table based on a {@link rocDocumentationObject}. * * @param {rocDocumentationObject} initalDocumentationObject - The documentation object to create a table of. * @param {rocTableHeader} header - Header object to use. * @param {rocTableSettings} settings - The settings to use. * * @returns {string} - A table. */ export default function generateTable(initalDocumentationObject, header, settings) { settings = deepExtend({}, defaultSettings, settings); const headerLengths = createLengthObject(header, header, {}, true); const lengths = createLengthObject(initalDocumentationObject, header, headerLengths); const printTableCell = (key, element, renderer = (input) =&gt; input || &apos;&apos;, fill = false) =&gt; { if (fill) { return settings.cellWrapper(pad(lengths[key], &apos;-&apos;)); } const padding = isUndefined(header[key].padding) ? true : header[key].padding; const text = padding ? addPadding(renderer(element), lengths[key]) : renderer(element); return settings.cellWrapper(text); }; const printTableRow = (object, isHeader = false) =&gt; { return settings.rowWrapper(Object.keys(header).map((key) =&gt; { const { value, renderer } = getValueAndRenderer(isHeader, header[key], object[key]); return printTableCell(key, value, renderer); }).join(settings.cellDivider)); }; const printTableSplitter = () =&gt; { return settings.rowWrapper(Object.keys(header).map((key) =&gt; printTableCell(key, undefined, undefined, true) ).join(settings.cellDivider)); }; const printTableHead = (name, description, level = 0) =&gt; { const rows = []; rows.push(settings.groupTitleWrapper(name, level)); if (description) { rows.push(description); } // Add a new empty row rows.push(&apos;&apos;); if (settings.header) { rows.push(printTableRow(header, true)); rows.push(printTableSplitter()); } return rows; }; const recursiveHelper = (documentationObject = []) =&gt; { const spacing = settings.compact ? [] : [&apos;&apos;]; return documentationObject.map((group) =&gt; { let rows = []; const level = group.level || 0; if (level === 0 || !settings.compact) { rows = rows.concat(printTableHead(group.name, group.description, level)); } rows = rows.concat(group.objects.map((element) =&gt; printTableRow(element))); rows = rows.concat(recursiveHelper(group.children)); if (level === 0 &amp;&amp; settings.compact) { rows.push(&apos;&apos;); } return rows; }).reduce((a, b) =&gt; a.concat(b), spacing); }; return recursiveHelper(initalDocumentationObject).join(&apos;\n&apos;); } function createLengthObject(initalElements, header, initalLengths, isHeader = false) { const getObjectLength = (object, renderer = (input) =&gt; input) =&gt; { return stripAnsi(renderer(object)).length; }; const getLength = (newLength, currentLength = 0) =&gt; { return newLength &gt; currentLength ? newLength : currentLength; }; const recursiveHelper = (elements = [], lengths = {}) =&gt; { let newLengths = { ...lengths }; elements.forEach((element) =&gt; { element.objects.forEach((object) =&gt; { Object.keys(header).forEach((key) =&gt; { const { value, renderer } = getValueAndRenderer(isHeader, header[key], object[key]); if (value) { newLengths[key] = getLength(getObjectLength(value, renderer), newLengths[key]); } }); }); newLengths = recursiveHelper(element.children, newLengths); }); return newLengths; }; // Make sure the data matches the expected format if (!Array.isArray(initalElements)) { initalElements = [{objects: [initalElements], children: []}]; } return recursiveHelper(initalElements, initalLengths); } function getValueAndRenderer(isHeader, headerObject, object) { const value = isHeader ? object.name : object; const renderer = isHeader ? (input) =&gt; input : headerObject.renderer; return { value, renderer }; } </code></pre> </div> <footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.4.3)</span></a> </footer> <script src="script/search_index.js"></script> <script src="script/search.js"></script> <script src="script/pretty-print.js"></script> <script src="script/inherited-summary.js"></script> <script src="script/test-summary.js"></script> <script src="script/inner-link.js"></script> <script src="script/patch-for-local.js"></script> </body> </html>