Skip to content

Commit

Permalink
Allow users to specify their own template
Browse files Browse the repository at this point in the history
  • Loading branch information
d4nyll committed Aug 25, 2017
1 parent 575bd07 commit 848fcc3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import ItemSchema from './schema/item.json';

import * as errorStrings from './errors.js';

const _getTemplateUrl = function (name) {
return `./templates/${name}.html`;
const _getTemplateUrl = function _getTemplateUrl(template) {
// If the template is a alphanumeric string,
// we assume it's using a pre-defined template
// and we return the path to it
// Otherwise, we treat it as a user-provided path,
// and simply return that
return /^[a-zA-Z0-9]+$/.test(template) ? `./templates/${template}.html` : template;
}

const Revoice = {};
Expand Down Expand Up @@ -40,6 +45,7 @@ Revoice.getTemplate = function (template = Revoice.DEFAULT_TEMPLATE) {

Revoice.validateInvoiceDataObject = function (data = {}) {
const ajv = new Ajv({
// check all rules collecting all errors, the default is to return after the first error
allErrors: true,
schemas: [InvoiceSchema, ItemSchema],
});
Expand Down
11 changes: 11 additions & 0 deletions test/sample/templates/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div class="content-container">
<h1>Test String</h1>
<p>Q8lF9cWGBRay89xiYv5lOSjE13kJI09pGXrq5hYNdo48DBxHH5NlnIMjt111RGT</p>
</div>
</body>
</html>
3 changes: 3 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ describe('Revoice', function() {
it('should throw an error when the template could not be found', function() {
return expect(Revoice.getTemplate(randomstring.generate())).to.eventually.be.rejectedWith(Error, "Template not found");
});
it('should allow user to specify their own template path', function() {
return expect(Revoice.getTemplate('./test/sample/templates/test.html')).to.eventually.have.string('Q8lF9cWGBRay89xiYv5lOSjE13kJI09pGXrq5hYNdo48DBxHH5NlnIMjt111RGT');
});
});
describe('#validateInvoiceDataObject()', function() {
it('should exists', function() {
Expand Down

0 comments on commit 848fcc3

Please sign in to comment.