throw errors when dash receives invalid args

This commit is contained in:
Floyd May 2019-05-06 08:51:49 -05:00
parent 58d5495920
commit fd29fe2f99
5 changed files with 59 additions and 13 deletions

View File

@ -4,6 +4,7 @@
- Fix links to pages within the document
- Add support for named destinations
- Throw errors when `dash(...)` is passed invalid lengths
### [v0.9.1] - 2019-04-30

View File

@ -145,7 +145,8 @@ The output of this example looks like this.
The `dash` method allows you to create non-continuous dashed lines. It takes a
length specifying how long each dash should be, as well as an optional hash
describing the additional properties `space` and `phase`.
describing the additional properties `space` and `phase`. Lengths must be positive
numbers; `dash` will throw if passed invalid lengths.
The `space` option defines the length of the space between each dash, and the `phase` option
defines the starting point of the sequence of dashes. By default the `space`

View File

@ -63,20 +63,18 @@ export default {
dash(length, options = {}) {
let phase;
if (length == null) {
return this;
const originalLength = length;
if (!Array.isArray(length)) {
length = [length, options.space || length];
}
if (Array.isArray(length)) {
length = length.map(v => number(v)).join(' ');
phase = options.phase || 0;
return this.addContent(`[${length}] ${number(phase)} d`);
} else {
const space = options.space != null ? options.space : length;
phase = options.phase || 0;
return this.addContent(
`[${number(length)} ${number(space)}] ${number(phase)} d`
);
const valid = length.every(x => Number.isFinite(x) && x > 0);
if(!valid) {
throw new Error(`dash(${JSON.stringify(originalLength)}, ${JSON.stringify(options)}) invalid, lengths must be numeric and greater than zero`);
}
length = length.map(v => number(v)).join(' ');
phase = options.phase || 0;
return this.addContent(`[${length}] ${number(phase)} d`);
},
undash() {

View File

@ -32,4 +32,5 @@ describe('PDFDocument', () => {
expect(fontSpy).not.toBeCalled();
});
});
});

View File

@ -111,5 +111,50 @@ describe('Vector Graphics', () => {
`endobj`
]);
});
describe('validation', () => {
test('length 1', () => {
const doc = new PDFDocument();
expect(() => doc.dash(1)).not.toThrow();
});
test('length 1.5', () => {
const doc = new PDFDocument();
expect(() => doc.dash(1.5)).not.toThrow();
});
test('length 0 throws', () => {
const doc = new PDFDocument();
expect(() => doc.dash(0)).toThrow('dash(0, {}) invalid, lengths must be numeric and greater than zero');
});
test('length -1 throws', () => {
const doc = new PDFDocument();
expect(() => doc.dash(-1)).toThrow('dash(-1, {}) invalid, lengths must be numeric and greater than zero');
});
test('length null throws', () => {
const doc = new PDFDocument();
expect(() => doc.dash(null)).toThrow('dash(null, {}) invalid, lengths must be numeric and greater than zero');
});
test('length array', () => {
const doc = new PDFDocument();
expect(() => doc.dash([2,3])).not.toThrow();
});
test('length array containing zeros throws', () => {
const doc = new PDFDocument();
expect(() => doc.dash([2, 0, 3])).toThrow('dash([2,0,3], {}) invalid, lengths must be numeric and greater than zero');
});
});
});
});