Skip to content

Commit

Permalink
Normalize date format by converting all HH, Hh, hH to hh.
Browse files Browse the repository at this point in the history
  • Loading branch information
jansivans committed May 28, 2024
1 parent af0c0c0 commit be1ed97
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xlstream",
"version": "2.5.3",
"version": "2.5.4",
"description": "Turns XLSX into a readable stream.",
"main": "lib/index",
"types": "lib/index",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ function getTransform(formats: (string | number)[], strings: string[], dict?: IM
numFormat = numberFormat[numFormat];
}
if (typeof numFormat === 'string') {
numFormat = numFormat.replace(/[Hh]{2}/g, 'hh');
value = numfmt.format(numFormat, value);
} else {
value = ssf.format(numFormat, value);
Expand Down
47 changes: 47 additions & 0 deletions tests/__snapshots__/xlsx-stream.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,53 @@ Array [
]
`;

exports[`correctly formats Hh hours in date 1`] = `
Array [
Object {
"formatted": Object {
"arr": Array [
"Date",
],
"obj": Object {
"A": "Date",
},
},
"header": Array [],
"processedSheetSize": 1480,
"raw": Object {
"arr": Array [
"Date",
],
"obj": Object {
"A": "Date",
},
},
"totalSheetSize": 1480,
},
Object {
"formatted": Object {
"arr": Array [
"2024-05-25 10:56:55",
],
"obj": Object {
"A": "2024-05-25 10:56:55",
},
},
"header": Array [],
"processedSheetSize": 1480,
"raw": Object {
"arr": Array [
45437.456192129626,
],
"obj": Object {
"A": 45437.456192129626,
},
},
"totalSheetSize": 1480,
},
]
`;

exports[`correctly handles custom format 1`] = `
Array [
Object {
Expand Down
Binary file added tests/assets/incorrect-hours-format.xlsx
Binary file not shown.
13 changes: 13 additions & 0 deletions tests/xlsx-stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,17 @@ it('reads XLSX file with styles.xml tags prefixed with `x:` correctly', async (d
expect(data).toMatchSnapshot();
done();
})
});

it('correctly formats Hh hours in date', async (done) => {
const data: any = [];
const stream = await getXlsxStream({
filePath: './tests/assets/incorrect-hours-format.xlsx',
sheet: 0,
});
stream.on('data', x => data.push(x));
stream.on('end', () => {
expect(data).toMatchSnapshot();
done();
})
});

0 comments on commit be1ed97

Please sign in to comment.