refactor: metadata -> posts
Alan Pearce alan@alanpearce.eu
Sun, 18 Jun 2017 12:35:49 +0200
3 files changed, 13 insertions(+), 18 deletions(-)
M src/index.js → src/index.js
@@ -10,14 +10,14 @@ const router = new Router() const view = require('koa-nunjucks-next') -const metadata = require('./modules/metadata.js') -const postMetadata = metadata.getFolderMetadata(process.env.POST_DIR) +const Posts = require('./modules/posts.js') +const posts = Posts.getFolder(process.env.POST_DIR) app.use(view(`${__dirname}/views`)) router.get('/', async function (ctx, next) { await ctx.render('index', { - posts: postMetadata + posts }) })
M src/modules/metadata.js → src/modules/posts.js
@@ -24,18 +24,15 @@ } return meta } -function getFileMetadata (filename) { +function get (filename) { return canonicaliseMetadata(matter.read(filename, options)) } -function getFolderMetadata (folder) { - return fs - .readdirSync(folder) - .map(f => path.resolve(folder, f)) - .map(getFileMetadata) +function getFolder (folder) { + return fs.readdirSync(folder).map(f => path.resolve(folder, f)).map(get) } module.exports = { - getFileMetadata, - getFolderMetadata + get, + getFolder }
M test/modules/metadata.test.js → test/modules/posts.test.js
@@ -1,9 +1,9 @@ const test = require('ava') const path = require('path') -const metadata = require('../../src/modules/metadata.js') +const posts = require('../../src/modules/posts.js') -test('getFileMetadata', t => { +test('get', t => { const expected = new Map( Object.entries({ title: 'This is a test', @@ -11,13 +11,11 @@ description: 'Test file', tags: ['a', 'b'] }) ) - const meta = metadata.getFileMetadata( - path.resolve(__dirname, '../data/testfile.md') - ) + const meta = posts.get(path.resolve(__dirname, '../data/testfile.md')) t.deepEqual(meta.data, expected) }) -test('getFolderMetadata', t => { +test('getFolder', t => { const expected = new Map( Object.entries({ title: 'This is a test', @@ -25,7 +23,7 @@ description: 'Test file', tags: ['a', 'b'] }) ) - const actual = metadata.getFolderMetadata(path.resolve(__dirname, '../data/')) + const actual = posts.getFolder(path.resolve(__dirname, '../data/')) t.true(Array.isArray(actual), 'must return an array') t.true(actual.length > 0, 'must return a non-empty array') t.is(actual[0].path, path.resolve(__dirname, '../data/testfile.md'))