Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 13 additions & 5 deletions src/main/ts/ingrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type TIngridResponse = Record<string, string[]>[]

export type TIngridParseOpts = Partial<{
format: 'unix' | 'win'
debug: boolean
}>

export type TIngridParse = (input: string) => TIngridResponse
Expand Down Expand Up @@ -128,14 +129,19 @@ const gridToData = (grid: string[][][]): TIngridResponse => {
}

// eslint-disable-next-line sonarjs/cognitive-complexity
export const parseWinGrid = (input: string): TIngridResponse => {
const _lines = input.split(/\r?\n/)
export const parseWinGrid = (input: string, debug = false): TIngridResponse => {
const _lines = input.split(/\r*\n+/)
const lines = _lines.filter(Boolean)
const headline = lines.shift()!
const headers = headline.split(/\s+/)
const headers = headline.split(/\s+/)//.map
const hl = headers.length
const ll = headline.length

if (debug) {
console.log('Headers:', headers)
console.log('Line lengths:', lines.map(l => l.length))
}

if (lines.every(l => ll / l.length < 2)) {
const spaces = Array
.from({ length: ll })
Expand All @@ -149,6 +155,8 @@ export const parseWinGrid = (input: string): TIngridResponse => {
}, [0])
const data: TIngridResponse = []

debug && console.log('Borders:', borders)

for (const line of lines) {
const props: [string, [string]][] = []
for (const i in headers) {
Expand Down Expand Up @@ -204,9 +212,9 @@ const parsers = {
win: parseWinGrid
}

export const parse = (input: string, {format = 'unix'}: TIngridParseOpts = {}) => {
export const parse = (input: string, {format = 'unix', debug = false}: TIngridParseOpts = {}) => {
const parser = parsers[format]
if (!parser) throw new Error(`unsupported format: ${format}`)

return parser(input)
return parser(input, debug)
}
Loading