Skip to content

Commit

Permalink
Merge pull request #83 from cjxe/main
Browse files Browse the repository at this point in the history
fix: type `null` is not assignable to type `IContent`
  • Loading branch information
LuisEnMarroquin committed Aug 24, 2023
2 parents daeee56 + 95825e8 commit c243fef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/main-library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-as-xlsx",
"version": "2.5.3",
"version": "2.5.4",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
20 changes: 20 additions & 0 deletions packages/main-library/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,25 @@ describe("json-as-xlsx", () => {
expect(booksSheet.A3.v).toBe("Git")
expect(booksSheet.B3.v).toBe("Robert")
})

it("should allow null value in content", () => {
const sheets = [
{
sheet: "Users",
columns: [{ label: "IP", value: "metadata.ip" }],
content: [
{ name: "Martin", metadata: { ip: null } },
{ name: "Robert", metadata: { ip: null } },
],
},
]
const buffer = jsonxlsx(sheets, settings)
const workBook = readBufferWorkBook(buffer)
const workSheet = workBook.Sheets.Users

expect(workSheet.A1.v).toBe("IP")
expect(workSheet.A2.v).toBe("")
expect(workSheet.A3.v).toBe("")
})
})
})
8 changes: 4 additions & 4 deletions packages/main-library/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { utils, WorkBook, WorkSheet, write, writeFile, WritingOptions } from "xl

export interface IColumn {
label: string
value: string | ((value: IContent) => string | number | boolean | Date | IContent)
value: string | ((value: IContent) => string | number | boolean | Date | IContent | null)
format?: string
}

Expand All @@ -25,7 +25,7 @@ export interface ISettings {
}

export interface IJsonSheetRow {
[key: string]: string | number | boolean | Date | IContent
[key: string]: string | number | boolean | Date | IContent | null
}

export interface IWorksheetColumnWidth {
Expand All @@ -44,7 +44,7 @@ export const getContentProperty = (content: IContent, property: string): string
return value ?? ""
}

if (value === undefined || typeof value === "string" || typeof value === "boolean" || typeof value === "number" || value instanceof Date) {
if (value === undefined || value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number" || value instanceof Date) {
return ""
}

Expand Down Expand Up @@ -213,4 +213,4 @@ module.exports = xlsx
module.exports.getContentProperty = getContentProperty
module.exports.getJsonSheetRow = getJsonSheetRow
module.exports.getWorksheetColumnWidths = getWorksheetColumnWidths
module.exports.utils = utils
module.exports.utils = utils

0 comments on commit c243fef

Please sign in to comment.