Skip to content

Commit

Permalink
feat: Fixed Trig serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Dexagod committed Feb 12, 2024
1 parent 8689a20 commit 407454e
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/trig/trigUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,35 @@ export type TrigPackage = rdf.Quad[]
export type TrigPackageString = TrigString

export async function serializeTrigPackageToTrigString (content: TrigPackage): Promise<TrigString> {
return new Writer({ format: 'application/trig' }).quadsToString(content)
return await new Promise((resolve, reject) => {
const writer = new Writer({ format: 'application/trig', prefixes: { ex: 'http://example.com/' } })
writer.addQuads(content)
writer.end((error, result) => {
if (error || !result) {
throw new Error('Could not serialize package string correctly')
}
resolve(indentTrigString(result as string))
})
})
}
export function parseTrigStringToTrigPackage (content: TrigString): TrigPackage {
return new Parser({ format: 'application/trig' }).parse(content)
}

function indentTrigString (trigString: TrigString): TrigString {
let result = ''
const indent = '\t'
let indented = false
for (const line of trigString.split('\n')) {
if (line.includes('{')) {
indented = true
result += line + '\n'
} else if (line.includes('}')) {
indented = false
result += line + '\n'
} else {
result += indented ? indent + line + '\n' : line + '\n'
}
}
return result.trimEnd()
}

0 comments on commit 407454e

Please sign in to comment.