From be1ed976b838c4e5112043777aca0b07829e1a99 Mon Sep 17 00:00:00 2001 From: Yan Ivan Evdokimov Date: Tue, 28 May 2024 13:28:59 +0300 Subject: [PATCH] Normalize date format by converting all HH, Hh, hH to hh. --- package.json | 2 +- src/index.ts | 1 + tests/__snapshots__/xlsx-stream.spec.ts.snap | 47 +++++++++++++++++++ tests/assets/incorrect-hours-format.xlsx | Bin 0 -> 8511 bytes tests/xlsx-stream.spec.ts | 13 +++++ 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/assets/incorrect-hours-format.xlsx diff --git a/package.json b/package.json index c9f8f42..d38b8d8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 6a5d6da..4db905f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); diff --git a/tests/__snapshots__/xlsx-stream.spec.ts.snap b/tests/__snapshots__/xlsx-stream.spec.ts.snap index 2b91f0d..4e20a4c 100644 --- a/tests/__snapshots__/xlsx-stream.spec.ts.snap +++ b/tests/__snapshots__/xlsx-stream.spec.ts.snap @@ -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 { diff --git a/tests/assets/incorrect-hours-format.xlsx b/tests/assets/incorrect-hours-format.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..e35050720a8d2ded06bad08f17d536c9770b9b4a GIT binary patch literal 8511 zcmb7JbyU>b79K$998j7e1ZjpY0YRj@B&ACjy1S8*ZULoJx|EPc8U#T~x(5)X>kaze z6>q*fYaP~)KhC$lb9S7)zpWsRa0?Fr03gE_1;AiC%}gE+0EoH;0N?^h09uc%tsIQ4 z9CTG&ZH(=8m|Y;2swyY|gdz6<;PrBJK?A_uI)Lr?4pI^)Yt;(I4m^r?3+~p6BY2x% z82Ua_sP}HcH-Oaxin}i$u863$JM9W8s&V;#vc)bdjXR%*jULQYacMmgUDTly>rgP7 zqc4rnid*o!eEB5=U6Qw?*jXWz=~a^4o6^pG2%t|LT84+ASeFPaLevZl#G0#9I-8(h z7_W;GR+X*>xU!SX_Vn<>i}N~?8L)W;XqGS<%M;JeQitd%8ezTbe4&bZ$B#3f@=b5` z$7b4-bcHgu13g1{%sS!jtbjLr+tavbn}tWLUUQS zkk9p85_>#kjpxe7Og5KyR6tUk@fZ|!pSJbtkw>IN-T(rz@ICv}B_v9%49{n4x$^#k ztqJSiY$xyvFx#NQ>;eG1R&P-M{_)>Gh%npe+8JBgvwSxPQ!V=)|>BKVA8RZl!2$C+M#oMq9T+MZe|_I5u=_Hy zHdL^)w)r<#ZYThJ?FtJHWCQ>J`zD0Gjm$KoW^R9W zV&g>3X0S_LH&>3$f6ul=UVijJ5F(XUHlv0My=eb*U-muaMMSfhPlAv_Z0`335f+4n zJwy#?D~A5*%dY9VQqydwz}`v|FrIstMsG7GlmGhhnsL2FOoke_>|!yTT`KrM5?74bw zU9e@aubT|^u;b|z(K%7C|H@gW`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TAo0O`!c&AXqSL8GW^(yH(IPuvdzQn zoop9F{nM5=^=W>}PFo`VkOVZ@aVC6zt6{5JsmS-VY{+5TmfOtdmGXJ}(q|4HiwlC< zag$Fx?rJiY_?M{gG}f^s?6GWPz$dt>(>ZP~yA>(rC#KH$eY6@wbu1mBuVQ#wJp*yK zDw*{(A`+*YFi#N38!5O$|9Fsr=MMjC-6H%#xq?xL!hX#zbHS7;BDJPtoh0t%>I;^C zd?Ej>BJV30m$(H}<@^WYSga+JO*|TPJtOdJ-8w@iAU2dx?`aQJvgL>x8 zRlB}*1qc^6jy>AV*AwxR_4KUv>J+xOq8t|oYIMZHcaeZ{?k(Dkssub2Y_6h+NMV;r z3!mI_47O8EIAZJPd?h%H$6rJa-C}xf5_$}R4%IZaK)}{iZi0psA$b(u4>|Z8yHPRg zy7|Zlr%Ds5R;R9ir;N!)>%d)u6XBnH$X<|Kum+7lO9VxBS-*2m=?e>O@58K-MGLAMe6$qo zKXp*S>w|;wAb^gY_c-d|>No>KX0De8NqFHtj^b&wQWVW#yJP)ojGtNpQjaz*IvdbD z#b&(5U50`!Nsy(u8qH+>SbObTZ7r}-)f-dl?wHm&p~aXtoPsnQJQ>2zL;gCFgNZ$` zA58WEWcyx!{po=2_KyMG?>ed^5b_+4jtA_-WbLFfBb$O#N6a3e-rMvvCQ9*{CVnMX9G7;fiU&v zHN>yb+&B5H`~bR{XH-@Gj@^$#|511R zAG>RREJFFt?uH8acQpUp5ix(UH6Pk{#A}+r3289Q|HC1FmCWH$}C7zJoui z8aL4uF~2jusd2y{|3u&TyJ-B?;ZcC!A^+EY{eS2GwfVZqQ06Aw%~tI1j{fJy`jsaG z;dh?@E^++&1m5InbrbHUqy8Ux { + 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(); + }) }); \ No newline at end of file