Skip to content

Commit

Permalink
tableAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
orthagonal committed Jan 19, 2018
1 parent 886f840 commit c4f61f9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ const os = require('os');

const register = (server, pluginOptions) => {
const tableToHtml = (table, options) => {
const tableAttributes = options && options.tableAttributes ? ` ${options.tableAttributes}` : '';
const header = `<tr><th>${table[0].join('</th><th>')}</th></tr>${os.EOL}`;
const rows = table.slice(1).reduce((tableString, n) => `${tableString}<tr><td>${n.join('</td><td>')}</td></tr>${os.EOL}`, '');
return `<table>${os.EOL}${header}${rows}</table>`;
return `<table${tableAttributes}>${os.EOL}${header}${rows}</table>`;
};

server.ext('onRequest', (request, h) => {
Expand All @@ -23,7 +24,7 @@ const register = (server, pluginOptions) => {
if (request.headers.accept === 'text/html') {
const routeOptions = request.route.settings.plugins['hapi-transform-table'] || {};
const options = Object.assign({}, pluginOptions, routeOptions);
return h.response(tableToHtml(jsonToTable(response.source, options)));
return h.response(tableToHtml(jsonToTable(response.source, options), options));
}
return h.continue;
});
Expand Down
6 changes: 6 additions & 0 deletions tests/output4.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<table class="blah">
<tr><th>car</th><th>price</th><th>colors.length</th></tr>
<tr><td>Audi</td><td>40000</td><td>2</td></tr>
<tr><td>BMW</td><td>35000</td><td>3</td></tr>
<tr><td>Porsche</td><td>60000</td><td>1</td></tr>
</table>
43 changes: 43 additions & 0 deletions tests/test.html.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,46 @@ tap.test('will pass config options to json-to-table', async(t) => {
await server.stop();
t.end();
});

tap.test('be able to pass in tableAttributes', async(t) => {
const server = await new Hapi.Server({ port: 8080 });
server.route({
method: 'get',
path: '/path1',
config: {
plugins: {
'hapi-transform-table': {
includeCollectionLength: true,
tableAttributes: 'class="blah"'
}
}
},
handler(request, h) {
return [
{
car: 'Audi',
price: 40000,
colors: ['blue', 'black']
}, {
car: 'BMW',
price: 35000,
colors: ['magenta', 'muave', 'cyan']
}, {
car: 'Porsche',
price: 60000,
colors: ['lime']
}
];
}
});
await server.register({ plugin, options: { excludeSubArrays: true } });
await server.start();
const tableResponse = await server.inject({
method: 'get',
url: '/path1.html'
});
t.equal(tableResponse.statusCode, 200, 'returns HTTP OK');
t.equal(tableResponse.result, fs.readFileSync(path.join(__dirname, 'output4.html'), 'utf-8'), 'produces correct HTML output');
await server.stop();
t.end();
});

0 comments on commit c4f61f9

Please sign in to comment.