Skip to content

Commit

Permalink
Make sam header parser same as bam-js
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Feb 20, 2024
1 parent aee975e commit 8c4b94c
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/sam.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
export type HeaderDataItem = {
tag: string
data: Array<{ tag: string; value: string }>
}

export function parseHeaderText(text: string): HeaderDataItem[] {
export function parseHeaderText(text: string) {
const lines = text.split(/\r?\n/)
const data: HeaderDataItem[] = []
lines.forEach(line => {
const data: { tag: string; data: { tag: string; value: string }[] }[] = []
for (const line of lines) {
const [tag, ...fields] = line.split(/\t/)
const parsedFields = fields.map(f => {
const [fieldTag, value] = f.split(':', 2)
return { tag: fieldTag, value }
})
if (tag) {
data.push({ tag: tag.substr(1), data: parsedFields })
data.push({
tag: tag.slice(1),
data: fields.map(f => {
const r = f.indexOf(':')
const fieldTag = f.slice(0, r)
const value = f.slice(r + 1)
return { tag: fieldTag, value }
}),
})
}
})
}
return data
}

0 comments on commit 8c4b94c

Please sign in to comment.