Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update header detection logic #340

Merged
merged 4 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/spotty-months-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@flatfile/plugin-xlsx-extractor': minor
'@flatfile/util-extractor': patch
---

Improved header detection options.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

188 changes: 188 additions & 0 deletions plugins/xlsx-extractor/src/header.detection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import stream from 'stream'

export const ROWS_TO_SEARCH_FOR_HEADER = 10

interface DefaultOptions {
algorithm: 'default'
rowsToSearch?: number
}

interface ExplicitHeadersOptions {
algorithm: 'explicitHeaders'
headers: string[]
skip?: number
}

interface SpecificRowsOptions {
algorithm: 'specificRows'
rowNumbers: number[]
skip?: number
}

interface NewfangledOptions {
algorithm: 'newfangled'
}

export type GetHeadersOptions =
| DefaultOptions
| ExplicitHeadersOptions
| SpecificRowsOptions
| NewfangledOptions

interface GetHeadersResult {
header: string[]
skip: number
}

// Takes a datastream (representing a CSV) and returns the header row and the number of rows to skip
export abstract class Headerizer {
constructor() {}
abstract getHeaders(dataStream: stream.Readable): Promise<GetHeadersResult>

static create(options: GetHeadersOptions): Headerizer {
switch (options.algorithm) {
case 'explicitHeaders':
return new ExplicitHeaders(options)
case 'specificRows':
return new SpecificRows(options)
case 'newfangled':
throw new Error('Not implemented')
default:
return new OriginalDetector(options)
}
}
}

export const countNonEmptyCells = (row: string[]): number => {
return row.filter((cell) => cell.trim() !== '').length
}

// This is the original / default implementation of detectHeader.
// It looks at the first `rowsToSearch` rows and takes the row
// with the most non-empty cells as the header, preferring the earliest
// such row in the case of a tie.
class OriginalDetector extends Headerizer {
private rowsToSearch: number

constructor(private options: DefaultOptions) {
super()
this.rowsToSearch = options.rowsToSearch || ROWS_TO_SEARCH_FOR_HEADER
}

async getHeaders(dataStream: stream.Readable): Promise<GetHeadersResult> {
let currentRow = 0
let skip = 0
let header: string[] = []

// This is the original implementation of detectHeader
const detector = new stream.Writable({
objectMode: true,
write: (row, encoding, callback) => {
currentRow++
if (currentRow >= this.rowsToSearch) {
dataStream.destroy()
}
if (countNonEmptyCells(row) > countNonEmptyCells(header)) {
header = row
skip = currentRow
}
callback()
},
})

dataStream.pipe(detector, { end: true })

return new Promise((resolve, reject) => {
detector.on('finish', () => {
resolve({ header, skip })
})
dataStream.on('close', () => {
resolve({ header, skip })
})
dataStream.on('error', (error) => {
reject(error)
})
})
}
}

// This implementation simply returns an explicit list of headers
// it was provided with.
class ExplicitHeaders extends Headerizer {
headers: string[]
constructor(private readonly options: ExplicitHeadersOptions) {
super()

if (!options.headers || options.headers.length === 0) {
throw new Error('ExplicitHeaders requires at least one header')
}
}

async getHeaders(dataStream: stream.Readable): Promise<GetHeadersResult> {
return {
header: this.options.headers,
skip: this.options.skip || 0,
}
}
}

// This implementation looks at specific rows and combines them into a single header.
// For example, if you knew that the header was in the third row, you could pass it
// { rowNumbers: [2] }
class SpecificRows extends Headerizer {
constructor(private readonly options: SpecificRowsOptions) {
super()

if (!options.rowNumbers || options.rowNumbers.length === 0) {
throw new Error('SpecificRows requires at least one row number')
}
}

async getHeaders(dataStream: stream.Readable): Promise<GetHeadersResult> {
let currentRow = 0
let maxRow = Math.max(...this.options.rowNumbers)
let header: string[] = []

const detector = new stream.Writable({
objectMode: true,
write: (row, encoding, callback) => {
if (currentRow > maxRow) {
dataStream.destroy()
} else if (this.options.rowNumbers.includes(currentRow)) {
if (header.length === 0) {
// This is the first header row we've seen, so just remember it
header = row
} else {
for (let i = 0; i < header.length; i++) {
if (header[i] === '') {
header[i] = row[i].trim()
} else {
header[i] = `${header[i].trim()} ${row[i].trim()}`
}
}
}
}
currentRow++
callback()
},
})

dataStream.pipe(detector, { end: true })

// If we have an explicit skip, use it, otherwise skip past the last header row
const skip = this.options.skip ?? maxRow + 1

// TODO: this logic is duplicated, factor it out?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a bunch of the getHeaders logic is shared - wonder if we abstract out just the logical changes that are different here instead of just the return Promise?

return new Promise((resolve, reject) => {
detector.on('finish', () => {
resolve({ header, skip })
})
dataStream.on('close', () => {
resolve({ header, skip })
})
dataStream.on('error', (error) => {
reject(error)
})
})
}
}
2 changes: 2 additions & 0 deletions plugins/xlsx-extractor/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Extractor } from '@flatfile/util-extractor'
import { GetHeadersOptions } from './header.detection'
import { parseBuffer } from './parser'

/**
Expand All @@ -15,6 +16,7 @@ export interface ExcelExtractorOptions {
readonly rawNumbers?: boolean
readonly chunkSize?: number
readonly parallel?: number
readonly headerDetectionOptions?: GetHeadersOptions
readonly debug?: boolean
}

Expand Down
16 changes: 10 additions & 6 deletions plugins/xlsx-extractor/src/parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { parseBuffer } from './parser'
import { WorkbookCapture } from '@flatfile/util-extractor'
import * as fs from 'fs'
import * as path from 'path'
import { parseBuffer } from './parser'

describe('parser', () => {
const buffer: Buffer = fs.readFileSync(
path.join(__dirname, '../ref/test-basic.xlsx')
)
test('Excel to WorkbookCapture', () => {
expect(parseBuffer(buffer).Departments).toEqual({
let capture: WorkbookCapture
beforeAll(async () => {
capture = await parseBuffer(buffer)
})
test('Excel to WorkbookCapture', async () => {
expect(capture.Departments).toEqual({
headers: ['Code', 'Details', 'BranchName', 'Tenant'],
required: { Code: true, Details: false, BranchName: true, Tenant: true },
data: [
Expand All @@ -27,9 +32,8 @@ describe('parser', () => {
})
})

describe('test-basic.xlsx', function () {
const capture = parseBuffer(buffer)
test('finds all the sheet names', () => {
describe('test-basic.xlsx', () => {
test('finds all the sheet names', async () => {
expect(Object.keys(capture)).toEqual([
'Departments',
'Clients',
Expand Down
Loading
Loading