Skip to content

Commit 41b9bf8

Browse files
committed
improvements on the margin param
1 parent 31fe573 commit 41b9bf8

File tree

6 files changed

+84
-43
lines changed

6 files changed

+84
-43
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name: CI
22

33
on:
44
push:
5+
branches:
6+
- "master"
57
paths-ignore:
68
- "**.md"
79
pull_request:

src/config.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import { PaperFormat, PDFMargin } from "puppeteer";
88

99
export default class Config {
10-
static defaultUnit: string = "px";
1110
static defaultFormat: PaperFormat = "A4";
1211
static formatList: PaperFormat[] = [
1312
"A0",
@@ -131,9 +130,16 @@ export default class Config {
131130
break;
132131
case "margin":
133132
if (typeof params[param] === "string") {
134-
this.margin = new Margin().fromString(params[param]);
133+
this.margin = Margin.fromString(params[param]);
135134
} else if (params[param] instanceof Margin) {
136135
this.margin = params[param];
136+
} else if (Margin.isImplements(params[param])) {
137+
this.margin = new Margin(
138+
params[param].top,
139+
params[param].right,
140+
params[param].bottom,
141+
params[param].left
142+
);
137143
}
138144
break;
139145
}
@@ -143,11 +149,16 @@ export default class Config {
143149

144150
export class Margin implements PDFMargin {
145151
top?: string | number;
152+
right?: string | number;
146153
bottom?: string | number;
147154
left?: string | number;
148-
right?: string | number;
149155

150-
constructor(top?: number, right?: number, bottom?: number, left?: number) {
156+
constructor(
157+
top?: string | number,
158+
right?: string | number,
159+
bottom?: string | number,
160+
left?: string | number
161+
) {
151162
this.top = top ?? 0;
152163
this.right = right ?? 0;
153164
this.bottom = bottom ?? 0;
@@ -165,27 +176,24 @@ export class Margin implements PDFMargin {
165176
}
166177
}
167178

168-
fromString(margins: string): Margin {
179+
static fromString(margins: string): Margin {
169180
const _margins = margins.split(" ", 4);
170181
if (_margins.length === 1) {
171-
return new Margin(parseInt(_margins[0]));
182+
return new Margin(_margins[0]);
183+
} else if (_margins.length === 2) {
184+
return new Margin(_margins[0], _margins[1], _margins[0], _margins[1]);
172185
} else if (_margins.length === 4) {
173-
return new Margin(
174-
parseInt(_margins[0]),
175-
parseInt(_margins[1]),
176-
parseInt(_margins[2]),
177-
parseInt(_margins[3])
178-
);
186+
return new Margin(_margins[0], _margins[1], _margins[2], _margins[3]);
179187
}
180188
return new Margin();
181189
}
182190

183-
toUnits(): PDFMargin {
184-
return {
185-
top: (this.top ?? 0) + Config.defaultUnit,
186-
right: (this.right ?? 0) + Config.defaultUnit,
187-
bottom: (this.bottom ?? 0) + Config.defaultUnit,
188-
left: (this.left ?? 0) + Config.defaultUnit,
189-
};
191+
static isImplements(obj: any): boolean {
192+
return (
193+
["string", "number", "undefined"].includes(typeof obj.top) &&
194+
["string", "number", "undefined"].includes(typeof obj.right) &&
195+
["string", "number", "undefined"].includes(typeof obj.bottom) &&
196+
["string", "number", "undefined"].includes(typeof obj.left)
197+
);
190198
}
191199
}

src/generator/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default async (config: Config) => {
4949
height: config.height,
5050
scale: config.scale,
5151
landscape: config.landscape,
52-
margin: config.margin.toUnits(),
52+
margin: config.margin,
5353
});
5454

5555
// Close the browser instance

src/rest-api/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default async (req: Request, res: Response) => {
4444
return;
4545
}
4646
if (process.env.NODE_ENV === "development") {
47-
console.log(error);
47+
console.error(error);
4848
}
4949
res.status(500).json({
5050
error: "SERVER_ERROR",

src/rest-api/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import generate from "./generate";
1010
export const route: Express = express();
1111
route.use(express.json());
1212
route.use((err: Error, _req: Request, res: Response, next: NextFunction) => {
13+
if (err !== undefined && process.env.NODE_ENV === "development") {
14+
console.error(err);
15+
}
1316
if (err instanceof SyntaxError) {
1417
res
1518
.status(400)

test/config.margins.test.ts

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,83 @@ import assert from "assert";
22
import Config, { Margin } from "../src/config";
33

44
describe("Margins", function () {
5-
describe("Default margins", function () {
5+
describe("Defaults", function () {
66
it("Should be zero", function () {
77
const config = new Config();
88
assert.equal(config.margin.top, 0);
99
assert.equal(config.margin.right, 0);
1010
assert.equal(config.margin.bottom, 0);
1111
assert.equal(config.margin.left, 0);
1212
});
13-
it("getMargins() returns as expected", function () {
14-
const config = new Config();
15-
assert.equal(config.margin.toUnits().top, "0px");
16-
assert.equal(config.margin.toUnits().right, "0px");
17-
assert.equal(config.margin.toUnits().bottom, "0px");
18-
assert.equal(config.margin.toUnits().left, "0px");
19-
});
2013
});
2114

2215
describe("Set margins", function () {
2316
it("Should set all four margins", function () {
24-
const config = new Config();
2517
const margin = new Margin(2);
2618
assert.equal(margin.top, 2);
2719
assert.equal(margin.right, 2);
2820
assert.equal(margin.bottom, 2);
2921
assert.equal(margin.left, 2);
3022
});
3123
it("Should set all four margins with fromString()", function () {
32-
const config = new Config();
33-
const margin = new Margin().fromString("2");
34-
assert.equal(margin.top, 2);
35-
assert.equal(margin.right, 2);
36-
assert.equal(margin.bottom, 2);
37-
assert.equal(margin.left, 2);
24+
const margin = Margin.fromString("2px");
25+
assert.equal(margin.top, "2px");
26+
assert.equal(margin.right, "2px");
27+
assert.equal(margin.bottom, "2px");
28+
assert.equal(margin.left, "2px");
3829
});
3930
it("Should set specific margins", function () {
40-
const config = new Config();
4131
const margin = new Margin(2, 0, 10, 9);
4232
assert.equal(margin.top, 2);
4333
assert.equal(margin.right, 0);
4434
assert.equal(margin.bottom, 10);
4535
assert.equal(margin.left, 9);
4636
});
4737
it("Should set specific margins with fromString()", function () {
48-
const config = new Config();
49-
const margin = new Margin().fromString("2 0 10 9");
50-
assert.equal(margin.top, 2);
51-
assert.equal(margin.right, 0);
52-
assert.equal(margin.bottom, 10);
53-
assert.equal(margin.left, 9);
38+
const margin = Margin.fromString("2 0 10 9");
39+
assert.equal(margin.top, "2");
40+
assert.equal(margin.right, "0");
41+
assert.equal(margin.bottom, "10");
42+
assert.equal(margin.left, "9");
43+
});
44+
it("Should set specific margins with fromString()", function () {
45+
const margin = Margin.fromString("0 100");
46+
assert.equal(margin.top, "0");
47+
assert.equal(margin.right, "100");
48+
assert.equal(margin.bottom, "0");
49+
assert.equal(margin.left, "100");
50+
});
51+
it("Should set with Config", function () {
52+
const config = new Config({ margin: new Margin(1, 2, 3, 4) });
53+
assert.equal(config.margin.top, 1);
54+
assert.equal(config.margin.right, 2);
55+
assert.equal(config.margin.bottom, 3);
56+
assert.equal(config.margin.left, 4);
57+
});
58+
it("Should set with an object", function () {
59+
const config = new Config({ margin: { top: 1, left: 2, right: 3 } });
60+
assert.equal(config.margin.top, 1);
61+
assert.equal(config.margin.left, 2);
62+
assert.equal(config.margin.right, 3);
63+
assert.equal(config.margin.bottom, 0);
64+
});
65+
});
66+
67+
describe("isImplements()", function () {
68+
it("Valid object", function () {
69+
const obj = {
70+
top: 100,
71+
right: undefined,
72+
bottom: "100",
73+
};
74+
assert.equal(Margin.isImplements(obj), true);
75+
});
76+
it("Invalid object", function () {
77+
const obj = {
78+
top: {},
79+
right: false,
80+
};
81+
assert.equal(Margin.isImplements(obj), false);
5482
});
5583
});
5684
});

0 commit comments

Comments
 (0)