Skip to content

Commit

Permalink
Optimize table minification
Browse files Browse the repository at this point in the history
  • Loading branch information
hollandjake committed Feb 14, 2025
1 parent 6a077b4 commit dafb604
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 53 deletions.
3 changes: 2 additions & 1 deletion lib/mixins/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export default {
},
/**
* @param {Table} [opts]
* @returns {PDFTable}
* @returns {PDFTable} returns the table object unless `data` is set,
* then it returns the underlying document
*/
table(opts) {
return new PDFTable(this, opts);
Expand Down
2 changes: 1 addition & 1 deletion lib/table/accessibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function accessibleCell(cell, rowStruct, callback) {
);
if (Headers.size) attributes.Headers = Array.from(Headers);

if (cell.backgroundColor !== undefined) {
if (cell.backgroundColor != null) {
attributes.BackgroundColor = this.document._normalizeColor(
cell.backgroundColor,
);
Expand Down
18 changes: 6 additions & 12 deletions lib/table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PDFTable {
*
* @param {Iterable<TableCell>} row - The cells to render
* @param {boolean} lastRow - Whether this row is the last row
* @returns {this}
* @returns {this} returns the table, unless lastRow is `true` then returns the `PDFDocument`
*/
row(row, lastRow = false) {
if (this._ended) {
Expand All @@ -42,24 +42,18 @@ class PDFTable {
// Transform row
row = normalizeRow.call(this, row, this._currRowIndex);
if (this._currRowIndex === 0) ensure.call(this, row);
const { newPage, toRender } = measure.call(
this,
row,
this._currRowIndex,
);
const { newPage, toRender } = measure.call(this, row, this._currRowIndex);
if (newPage) this.document.continueOnNewPage();
const yPos = renderRow.call(this, toRender, this._currRowIndex);

// Position document at base of new row
this.document.x = this._position.x;
this.document.y = yPos;

if (lastRow) {
return this.end();
} else {
this._currRowIndex++;
return this;
}
if (lastRow) return this.end();

this._currRowIndex++;
return this;
}

/**
Expand Down
14 changes: 6 additions & 8 deletions lib/table/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function normalizeTable() {
*/
export function normalizeText(text) {
// Parse out text
if (text !== null && text !== undefined) text = String(text);
if (text != null) text = `${text}`;
return text;
}

Expand Down Expand Up @@ -127,7 +127,7 @@ export function normalizeCell(cell, rowIndex, colIndex) {
(x) => x ?? 'black',
);
config.align =
config.align === undefined || typeof config.align === 'string'
config.align == null || typeof config.align === 'string'
? { x: config.align, y: config.align }
: config.align;
config.align.x = config.align.x ?? 'left';
Expand All @@ -141,9 +141,9 @@ export function normalizeCell(cell, rowIndex, colIndex) {
config.id = new String(config.id ?? `${this._id}-${rowIndex}-${colIndex}`);
config.type = config.type?.toUpperCase() === 'TH' ? 'TH' : 'TD';
config.scope = config.scope?.toLowerCase();
if (config.scope === "row") config.scope = "Row"
else if (config.scope === "both") config.scope = "Both"
else if (config.scope === "column") config.scope = "Column"
if (config.scope === 'row') config.scope = 'Row';
else if (config.scope === 'both') config.scope = 'Both';
else if (config.scope === 'column') config.scope = 'Column';

if (this.opts.debug !== undefined) config.debug = this.opts.debug;

Expand All @@ -168,9 +168,7 @@ export function normalizeRow(row, rowIndex) {
let colIndex = 0;
return row.map((cell) => {
// Ensure TableCell
if (cell === null || cell === undefined || typeof cell !== 'object') {
cell = { text: cell };
}
if (cell == null || typeof cell !== 'object') cell = { text: cell };
cell = definedProps(cell);

// Find the starting column of the cell
Expand Down
50 changes: 19 additions & 31 deletions lib/table/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function normalizedDefaultStyle(defaultStyleInternal) {
defaultStyle.border = normalizeSides(defaultStyle.border);
defaultStyle.borderColor = normalizeSides(defaultStyle.borderColor);
defaultStyle.align =
defaultStyle.align === undefined || typeof defaultStyle.align === 'string'
defaultStyle.align == null || typeof defaultStyle.align === 'string'
? { x: defaultStyle.align, y: defaultStyle.align }
: defaultStyle.align;

Expand All @@ -56,11 +56,7 @@ export function normalizedDefaultStyle(defaultStyleInternal) {
export function normalizedRowStyle(defaultRowStyle, rowStyleInternal, i) {
let rowStyle = rowStyleInternal(i);
// Force object form
if (
rowStyle === null ||
rowStyle === undefined ||
typeof rowStyle !== 'object'
) {
if (rowStyle == null || typeof rowStyle !== 'object') {
rowStyle = { height: rowStyle };
}
// Normalize
Expand All @@ -75,31 +71,31 @@ export function normalizedRowStyle(defaultRowStyle, rowStyleInternal, i) {
// Merge defaults
rowStyle = deepMerge(defaultRowStyle, rowStyle);

if (
rowStyle.height === null ||
rowStyle.height === undefined ||
rowStyle.height === 'auto'
) {
const document = this.document;
const page = document.page;
const contentHeight = page.contentHeight;

if (rowStyle.height == null || rowStyle.height === 'auto') {
rowStyle.height = 'auto';
} else {
rowStyle.height = this.document.sizeToPoint(
rowStyle.height = document.sizeToPoint(
rowStyle.height,
0,
this.document.page,
this.document.page.contentHeight,
page,
contentHeight,
);
}
rowStyle.minHeight = this.document.sizeToPoint(
rowStyle.minHeight = document.sizeToPoint(
rowStyle.minHeight,
0,
this.document.page,
this.document.page.contentHeight,
page,
contentHeight,
);
rowStyle.maxHeight = this.document.sizeToPoint(
rowStyle.maxHeight = document.sizeToPoint(
rowStyle.maxHeight,
0,
this.document.page,
this.document.page.contentHeight,
page,
contentHeight,
);

return definedProps(rowStyle);
Expand All @@ -119,30 +115,22 @@ export function normalizedRowStyle(defaultRowStyle, rowStyleInternal, i) {
export function normalizedColumnStyle(defaultColStyle, colStyleInternal, i) {
let colStyle = colStyleInternal(i);
// Force object form
if (
colStyle === null ||
colStyle === undefined ||
typeof colStyle !== 'object'
) {
if (colStyle == null || typeof colStyle !== 'object') {
colStyle = { width: colStyle };
}
// Normalize
colStyle.padding = normalizeSides(colStyle.padding);
colStyle.border = normalizeSides(colStyle.border);
colStyle.borderColor = normalizeSides(colStyle.borderColor);
colStyle.align =
colStyle.align === undefined || typeof colStyle.align === 'string'
colStyle.align == null || typeof colStyle.align === 'string'
? { x: colStyle.align, y: colStyle.align }
: colStyle.align;

// Merge defaults
colStyle = deepMerge(defaultColStyle, colStyle);

if (
colStyle.width === null ||
colStyle.width === undefined ||
colStyle.width === '*'
) {
if (colStyle.width == null || colStyle.width === '*') {
colStyle.width = '*';
} else {
colStyle.width = this.document.sizeToPoint(
Expand Down

0 comments on commit dafb604

Please sign in to comment.