Skip to content

Commit

Permalink
feat: infer sheet context based on provided schema & selected columns
Browse files Browse the repository at this point in the history
  • Loading branch information
ChronicStone committed Dec 4, 2023
1 parent 4820d7f commit f36b7c0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
Binary file modified example.xlsx
Binary file not shown.
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import xlsx, { type IColumn, type IJsonSheet, getWorksheetColumnWidths } from 'j
import type { CellStyle } from 'xlsx-js-style'
import XLSX from 'xlsx-js-style'
import { deepmerge } from 'deepmerge-ts'
import type { CellValue, Column, ColumnGroup, ExcelSchema, GenericObject, NestedPaths, Not, Sheet, TransformersMap, ValueTransformer } from './types'
import type { CellValue, Column, ColumnGroup, ExcelSchema, GenericObject, NestedPaths, Not, SchemaColumnKeys, Sheet, TransformersMap, ValueTransformer } from './types'
import { formatKey, getPropertyFromPath, getSheetCellKey } from './utils'

export class ExcelSchemaBuilder<
Expand Down Expand Up @@ -85,7 +85,7 @@ export class ExcelSchemaBuilder<
}

export class ExcelBuilder<UsedSheetKeys extends string = never> {
private sheets: Array<Sheet<any, ExcelSchema<any, any, any>>> = []
private sheets: Array<Sheet<any, ExcelSchema<any, any, any>, any, any>> = []

public static create(): ExcelBuilder {
return new ExcelBuilder()
Expand All @@ -95,9 +95,11 @@ export class ExcelBuilder<UsedSheetKeys extends string = never> {
Key extends string,
T extends GenericObject,
Schema extends ExcelSchema<T, any, string>,
ColKeys extends SchemaColumnKeys<Schema>,
SelectCols extends { [key in ColKeys]?: boolean } = {},
>(
key: Not<Key, UsedSheetKeys>,
sheet: Omit<Sheet<T, Schema>, 'sheetKey'>,
sheet: Omit<Sheet<T, Schema, ColKeys, SelectCols>, 'sheetKey'>,
): ExcelBuilder<UsedSheetKeys | Key> {
if (this.sheets.some(s => s.sheetKey === key))
throw new Error(`Sheet with key '${key}' already exists.`)
Expand Down
36 changes: 32 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export type TypeFromPath<T extends GenericObject, Path extends string> =
: never
: never

export type AllKeysMatch<T extends object, U> = {
[K in keyof T]: T[K] extends U ? true : false;
}[keyof T] extends true ? true : false

export type CellValue = string | number | boolean | null | undefined | Date

export type ValueTransformer = (value: any) => CellValue
Expand Down Expand Up @@ -109,12 +113,36 @@ export type SchemaColumnKeys<
export type Sheet<
T extends GenericObject,
Schema extends ExcelSchema<T, any, string, any>,
ColumnKeys extends SchemaColumnKeys<Schema>,
SelectColsMap extends { [key in ColumnKeys]?: boolean } | never,
SelectedCols extends string = ExtractSelectedColumns<ColumnKeys, SelectColsMap>,
ContextMap extends { [key: string]: any } = ExtractContextMap<Schema>,
SelectedContextMap extends ExtractSelectedContext<ContextMap, SelectedCols> = ExtractSelectedContext<ContextMap, SelectedCols>,
> = {
sheetKey: string
schema: Schema
data: T[]
select?: { [K in SchemaColumnKeys<Schema>]?: boolean }
select?: SelectColsMap
context?: {}
} & (Schema extends ExcelSchema<T, any, any, infer Ctx>
? keyof Ctx extends never ? {} : { context: Ctx }
: {})
} & (keyof SelectedContextMap extends never ? {} : { context: SelectedContextMap })

export type ExtractContextMap<
Schema extends ExcelSchema<any, any, string, any>,
> = Schema extends ExcelSchema<any, any, any, infer Ctx> ? Ctx : {}

export type ExtractSelectedColumns<
ColKeys extends string,
SelectCols extends { [key in ColKeys]?: boolean },
> = keyof SelectCols extends never ? ColKeys :
AllKeysMatch<SelectCols, false> extends true
? Exclude<ColKeys, keyof SelectCols>
: {
[K in ColKeys]: SelectCols[K] extends true ? K : never;
}[ColKeys]

export type ExtractSelectedContext<
ContextMap extends { [key: string]: any },
SelectedCols extends string,
> = {
[K in keyof ContextMap as K extends SelectedCols ? K : never]: ContextMap[K];
}
16 changes: 9 additions & 7 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,25 @@ describe('should', () => {
.sheet('sheet1', {
data: users,
schema: assessmentExport,
select: {
'group:org': true,
},
context: {
'group:org': organizations,

},

})
.sheet('sheet2', {
data: users,
schema: assessmentExport,
select: {
firstName: true,
lastName: true,
email: true,
// 'group:org': true,
'firstName': true,
'lastName': false,
'email': false,
'group:org': false,
},
context: {
'group:org': organizations,

// 'group:org': organizations,
},
})
.sheet('sheet3', {
Expand Down

0 comments on commit f36b7c0

Please sign in to comment.