json-timeline
Version:
Generate Interactive Timelines stored in JSON Advents with Tags.
106 lines (85 loc) • 3.54 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: functions/loadAdvents.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: functions/loadAdvents.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* @module loadAdvents
*/
const fs = require('fs');
const validateSchema = require('../schema/validateSchema');
/**
* @function
* @name loadAdvents
* @description Load and Validate Advents from Directory
* @param {string} dirPath Path to Advents Directory
* @returns {Object[]} Array of Valid Advents
* @throws An Error if Invalid Advents exist, printing the File Names
*/
function loadAdvents(dirPath) {
// Process JSON Files
const { valid, invalid, tags } = fs.readdirSync(dirPath)
// Filter *.advent.js(on)? Files
.filter(name => (
fs.lstatSync(`${dirPath}/${name}`).isFile() &&
(name.endsWith('.advent.js') || name.endsWith('.advent.json'))
))
// Load Advents
.map(name => ({
name,
data: require(`${dirPath}/${name}`)
}))
// Group Valid and Invalid Advents
.reduce((advents, { name, data }) => {
const validatedData = validateSchema(data);
if (validatedData !== false) {
advents.valid.push(validatedData);
for (const tag of validatedData.tags) {
if (!advents.tags.has(tag)) {
advents.tags.set(tag, true);
}
}
} else {
advents.invalid.push(name);
}
return advents;
}, { valid: [], invalid: [], tags: new Map() });
// Log Invalid Advents
if (invalid.length !== 0) {
throw new Error(
`Invalid Advents in "${dirPath}"\n` +
invalid.map(name => `[ERROR]: - ${dirPath}/${name}`).join('\n')
);
}
// Return Valid Advents
return { advents: valid, tags };
}
module.exports = loadAdvents;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-advents.html">advents</a></li><li><a href="module-compareDates.html">compareDates</a></li><li><a href="module-groupAdvents.html">groupAdvents</a></li><li><a href="module-index.html">index</a></li><li><a href="module-isType.html">isType</a></li><li><a href="module-json-timeline.html">json-timeline</a></li><li><a href="module-loadAdvents.html">loadAdvents</a></li><li><a href="module-renderAdvents.html">renderAdvents</a></li><li><a href="module-schemas.html">schemas</a></li><li><a href="module-sortAdvents.html">sortAdvents</a></li><li><a href="module-validateSchema.html">validateSchema</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a> on Mon Dec 14 2020 23:14:56 GMT-0500 (Eastern Standard Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>