Skip to content

Commit

Permalink
jskos-convert: Support reading registry as ndjson (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Oct 1, 2024
1 parent aa377ab commit 904aa53
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions examples/registry.ndjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"uri": "http://example.org/voc","notation": ["example"],"notationPattern": "[A-Z][0-9]*","namespace": "http://example.org/voc/"}
{"uri": "http://example.com/voc","notationPattern": "[a-z]","namespace": "http://example.com/voc/"}
7 changes: 6 additions & 1 deletion lib/convert-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export default (options, type, files) => {

let registry = {}
if (options.registry) {
let json = JSON.parse(fs.readFileSync(options.registry))
let json, data = fs.readFileSync(options.registry, "utf8")
// Support .ndjson
if (options.registry.endsWith(".ndjson")) {
data = "[" + data.split("\n").filter(Boolean).join(",") + "]"
}
json = JSON.parse(data)
registry = Array.isArray(json) ? { schemes: json } : json
}
const output = new Writable({
Expand Down
11 changes: 11 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,16 @@ describe("jskos-convert", () => {
})
})

it("should use registry file to look up scheme, if given", (done) => {
const input = file("test/test-concepts.csv")
const registry = file("test/test-registry.ndjson")
convert(["concepts", "-r", registry, "-s", "http://example.com/", "-t", "ndjson", "-m", input], (error, stdout, stderr) => {
const expected = fs.readFileSync(input.replace(".csv", ".ndjson")).toString().trim()
const output = (""+stdout).trim()
assert.strictEqual(expected, output)
assert.equal(stderr, "")
done()
})
})

})
2 changes: 2 additions & 0 deletions test/test-registry.ndjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"uri": "http://example.com/","namespace": "http://example.com/"}
{"uri": "http://anotherexample.com/","namespace": "http://anotherexample.com/"}

0 comments on commit 904aa53

Please sign in to comment.