Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb838ea

Browse files
bno1Alexandru Șorodoc
andauthored
Fix parsing of boolean attributes (#1849)
Co-authored-by: Alexandru Șorodoc <[email protected]>
1 parent 33d833f commit fb838ea

File tree

6 files changed

+19
-30
lines changed

6 files changed

+19
-30
lines changed

lib/utils/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ const utils = {
163163
toIsoDateString(dt) {
164164
return dt.toIsoString().subsstr(0, 10);
165165
},
166+
167+
parseBoolean(value) {
168+
return value === true || value === 'true' || value === 1 || value === '1';
169+
},
166170
};
167171

168172
module.exports = utils;

lib/xlsx/xform/sheet/col-xform.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const utils = require('../../../utils/utils');
12
const BaseXform = require('../base-xform');
23

34
class ColXform extends BaseXform {
@@ -51,21 +52,16 @@ class ColXform extends BaseXform {
5152
if (node.attributes.style) {
5253
model.styleId = parseInt(node.attributes.style, 10);
5354
}
54-
if (
55-
node.attributes.hidden === true ||
56-
node.attributes.hidden === 'true' ||
57-
node.attributes.hidden === 1 ||
58-
node.attributes.hidden === '1'
59-
) {
55+
if (utils.parseBoolean(node.attributes.hidden)) {
6056
model.hidden = true;
6157
}
62-
if (node.attributes.bestFit) {
58+
if (utils.parseBoolean(node.attributes.bestFit)) {
6359
model.bestFit = true;
6460
}
6561
if (node.attributes.outlineLevel) {
6662
model.outlineLevel = parseInt(node.attributes.outlineLevel, 10);
6763
}
68-
if (node.attributes.collapsed) {
64+
if (utils.parseBoolean(node.attributes.collapsed)) {
6965
model.collapsed = true;
7066
}
7167
return true;

lib/xlsx/xform/sheet/data-validations-xform.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,11 @@ function assign(definedName, attributes, name, defaultValue) {
1212
definedName[name] = defaultValue;
1313
}
1414
}
15-
function parseBool(value) {
16-
switch (value) {
17-
case '1':
18-
case 'true':
19-
return true;
20-
default:
21-
return false;
22-
}
23-
}
15+
2416
function assignBool(definedName, attributes, name, defaultValue) {
2517
const value = attributes[name];
2618
if (value !== undefined) {
27-
definedName[name] = parseBool(value);
19+
definedName[name] = utils.parseBoolean(value);
2820
} else if (defaultValue !== undefined) {
2921
definedName[name] = defaultValue;
3022
}

lib/xlsx/xform/sheet/row-xform.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const BaseXform = require('../base-xform');
2+
const utils = require('../../../utils/utils');
23

34
const CellXform = require('./cell-xform');
45

@@ -79,15 +80,10 @@ class RowXform extends BaseXform {
7980
if (node.attributes.s) {
8081
model.styleId = parseInt(node.attributes.s, 10);
8182
}
82-
if (
83-
node.attributes.hidden === true ||
84-
node.attributes.hidden === 'true' ||
85-
node.attributes.hidden === 1 ||
86-
node.attributes.hidden === '1'
87-
) {
83+
if (utils.parseBoolean(node.attributes.hidden)) {
8884
model.hidden = true;
8985
}
90-
if (node.attributes.bestFit) {
86+
if (utils.parseBoolean(node.attributes.bestFit)) {
9187
model.bestFit = true;
9288
}
9389
if (node.attributes.ht) {
@@ -96,7 +92,7 @@ class RowXform extends BaseXform {
9692
if (node.attributes.outlineLevel) {
9793
model.outlineLevel = parseInt(node.attributes.outlineLevel, 10);
9894
}
99-
if (node.attributes.collapsed) {
95+
if (utils.parseBoolean(node.attributes.collapsed)) {
10096
model.collapsed = true;
10197
}
10298
return true;

lib/xlsx/xform/style/alignment-xform.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ class AlignmentXform extends BaseXform {
145145
'vertical',
146146
node.attributes.vertical === 'center' ? 'middle' : node.attributes.vertical
147147
);
148-
add(node.attributes.wrapText, 'wrapText', !!node.attributes.wrapText);
149-
add(node.attributes.shrinkToFit, 'shrinkToFit', !!node.attributes.shrinkToFit);
148+
add(node.attributes.wrapText, 'wrapText', utils.parseBoolean(node.attributes.wrapText));
149+
add(node.attributes.shrinkToFit, 'shrinkToFit', utils.parseBoolean(node.attributes.shrinkToFit));
150150
add(node.attributes.indent, 'indent', parseInt(node.attributes.indent, 10));
151151
add(
152152
node.attributes.textRotation,

lib/xlsx/xform/style/border-xform.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable max-classes-per-file */
22
const BaseXform = require('../base-xform');
3+
const utils = require('../../../utils/utils');
34

45
const ColorXform = require('./color-xform');
56

@@ -156,8 +157,8 @@ class BorderXform extends BaseXform {
156157
switch (node.name) {
157158
case 'border':
158159
this.reset();
159-
this.diagonalUp = !!node.attributes.diagonalUp;
160-
this.diagonalDown = !!node.attributes.diagonalDown;
160+
this.diagonalUp = utils.parseBoolean(node.attributes.diagonalUp);
161+
this.diagonalDown = utils.parseBoolean(node.attributes.diagonalDown);
161162
return true;
162163
default:
163164
this.parser = this.map[node.name];

0 commit comments

Comments
 (0)