Skip to content

Commit

Permalink
Merge pull request #49 from rdf-ext/rm-dataset-ext
Browse files Browse the repository at this point in the history
feat!: converted to ESM, removed rdf-dataset-ext dep
  • Loading branch information
bergos authored Jun 7, 2024
2 parents b3afa52 + 18d7d55 commit 4e699c0
Show file tree
Hide file tree
Showing 28 changed files with 299 additions and 340 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: daily
- package-ecosystem: npm
directory: /
schedule:
interval: daily
versioning-strategy: increase-if-necessary
17 changes: 0 additions & 17 deletions .github/workflows/ci.yaml

This file was deleted.

21 changes: 21 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Test
on:
- pull_request
- push
jobs:
test:
runs-on: ubuntu-24.04
strategy:
matrix:
node:
- '18'
- '20'
- '22'
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v4
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.idea
coverage
node_modules
.DS_Store
package-lock.json
test/spec
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

24 changes: 18 additions & 6 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
# License
The MIT License (MIT)
Copyright © 2018–2019 Zazuko GmbH
MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Copyright (c) 2024 Thomas Bergwinkl

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# rdf-parser-csvw

A CSV on the Web parser with [RDFJS Stream interface](https://github.com/rdfjs/representation-task-force/).
[![build status](https://img.shields.io/github/actions/workflow/status/rdf-ext/rdf-parser-csvw/test.yaml?branch=master)](https://github.com/rdf-ext/rdf-parser-csvw/actions/workflows/test.yaml)
[![npm version](https://img.shields.io/npm/v/rdf-parser-csvw.svg)](https://www.npmjs.com/package/rdf-parser-csvw)

A CSV on the Web parser with [RDF/JS Stream interface](https://github.com/rdfjs/representation-task-force/).

## Usage

The package exports the parser as a class, so an instance must be created before it can be used.
The `.import` method, as defined in the [RDFJS specification](http://rdf.js.org/#sink-interface), must be called to do the actual parsing.
The `.import` method, as defined in the [RDF/JS specification](http://rdf.js.org/#sink-interface), must be called to do the actual parsing.
It expects a stream of strings.
The method will return a stream which emits the parsed quads.

Expand All @@ -17,7 +20,7 @@ The constructor accepts an `options` object with the following optional keys:
- `baseIRI`: Use the IRI to create Named Nodes.
The value must be a String.
This options is required.
- `factory`: Use an alternative RDFJS data factory.
- `factory`: Use an alternative RDF/JS data factory.
By default the [reference implementation](https://github.com/rdfjs/data-model/) us used.
- `timezone`: Use an alternative timezone to parse date and time values.
The value must be given as a String as defined in the [Luxon documentation](https://moment.github.io/luxon/docs/manual/zones.html#specifying-a-zone).
Expand Down
5 changes: 2 additions & 3 deletions bin/csvw-metadata.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node

const MetadataBuilder = require('../lib/MetadataBuilder')

const program = require('commander')
import program from 'commander'
import MetadataBuilder from '../lib/MetadataBuilder.js'

program
.arguments('<filename>')
Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const CsvParser = require('./lib/CsvParser')
const parseMetadata = require('./lib/metadata')
const rdf = require('@rdfjs/data-model')
const ObjectParserTransform = require('./lib/ObjectParserTransform')
import rdf from '@rdfjs/data-model'
import CsvParser from './lib/CsvParser.js'
import parseMetadata from './lib/metadata/index.js'
import ObjectParserTransform from './lib/ObjectParserTransform.js'

class Parser {
constructor ({ metadata, baseIRI = '', factory = rdf, timezone, relaxColumnCount, skipLinesWithError } = {}) {
Expand Down Expand Up @@ -43,7 +43,7 @@ class Parser {
output.destroy(err)
})

input.on('error', (err) => {
input.on('error', err => {
output.destroy(err)
})

Expand All @@ -57,4 +57,4 @@ class Parser {
}
}

module.exports = Parser
export default Parser
6 changes: 3 additions & 3 deletions lib/CsvParser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Parser } = require('csv-parse')
const { Transform } = require('readable-stream')
import { Parser } from 'csv-parse'
import { Transform } from 'readable-stream'

class CsvParser extends Transform {
constructor ({ delimiter, lineTerminators, quoteChar, relaxColumnCount, skipLinesWithError } = {}) {
Expand Down Expand Up @@ -43,4 +43,4 @@ class CsvParser extends Transform {
}
}

module.exports = CsvParser
export default CsvParser
4 changes: 2 additions & 2 deletions lib/MetadataBuilder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('fs')
import fs from 'fs'

class MetadataBuilder {
static readFirstLine (filename) {
Expand Down Expand Up @@ -83,4 +83,4 @@ class MetadataBuilder {
}
}

module.exports = MetadataBuilder
export default MetadataBuilder
16 changes: 8 additions & 8 deletions lib/ObjectParserTransform.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const parseMetadata = require('./metadata')
const namespace = require('./namespace')
const rdf = require('@rdfjs/data-model')
const { Transform } = require('readable-stream')
import rdf from '@rdfjs/data-model'
import { Transform } from 'readable-stream'
import parseMetadata from './metadata/index.js'
import namespace from './namespace.js'

class ObjectParserTransform extends Transform {
constructor ({ baseIRI = '', factory = rdf, metadata, tableSchema, timezone } = {}) {
Expand Down Expand Up @@ -60,7 +60,7 @@ class ObjectParserTransform extends Transform {
const urlQuad = [...this.parsedMetadata.dataset.match(null, this.ns.url)][0]

if (urlQuad) {
this.copySubgraph([...this.parsedMetadata.dataset.match(urlQuad.subject)].filter((quad) => {
this.copySubgraph([...this.parsedMetadata.dataset.match(urlQuad.subject)].filter(quad => {
return quad.predicate.value.slice(0, 26) !== 'http://www.w3.org/ns/csvw#'
}), this.tableNode)
}
Expand Down Expand Up @@ -100,7 +100,7 @@ class ObjectParserTransform extends Transform {
describesNode
))

this.tableSchema.columns({ contentLine: this.contentLine, row: rowData }).forEach((column) => {
this.tableSchema.columns({ contentLine: this.contentLine, row: rowData }).forEach(column => {
this.push(this.factory.quad(
column.subject || describesNode,
column.property,
Expand Down Expand Up @@ -130,7 +130,7 @@ class ObjectParserTransform extends Transform {
}

copySubgraph (quads, subject) {
quads.forEach((quad) => {
quads.forEach(quad => {
this.push(this.factory.quad(
subject || quad.subject,
quad.predicate,
Expand All @@ -144,4 +144,4 @@ class ObjectParserTransform extends Transform {
}
}

module.exports = ObjectParserTransform
export default ObjectParserTransform
6 changes: 3 additions & 3 deletions lib/metadata/Metadata.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const namespace = require('../namespace')
const TableSchema = require('./TableSchema')
import namespace from '../namespace.js'
import TableSchema from './TableSchema.js'

class Metadata {
constructor (dataset, { baseIRI, factory, timezone } = {}) {
Expand Down Expand Up @@ -61,4 +61,4 @@ class Metadata {
}
}

module.exports = Metadata
export default Metadata
4 changes: 2 additions & 2 deletions lib/metadata/RdfUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const rdf = require('@rdfjs/data-model')
import rdf from '@rdfjs/data-model'

const ns = {
first: rdf.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#first'),
Expand Down Expand Up @@ -52,4 +52,4 @@ class RdfUtils {
}
}

module.exports = RdfUtils
export default RdfUtils
38 changes: 22 additions & 16 deletions lib/metadata/TableSchema.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const difference = require('lodash/difference')
const namespace = require('../namespace')
const parseDateTime = require('../parseDateTime')
const uriTemplate = require('uri-templates')
const URL = require('url')
const RdfUtils = require('./RdfUtils')
import url from 'node:url'
import difference from 'lodash/difference.js'
import uriTemplate from 'uri-templates'
import namespace from '../namespace.js'
import parseDateTime from '../parseDateTime.js'
import RdfUtils from './RdfUtils.js'

const defaultColumnNames = new Set(['_column', '_sourceColumn', '_row', '_sourceRow', '_name'])

Expand Down Expand Up @@ -39,8 +39,9 @@ class TableSchema {

const aboutUrlTemplate = uriTemplate(aboutUrl)

return (row) => {
return this.factory.namedNode(URL.resolve(this.baseIRI, aboutUrlTemplate.fill(row))) // eslint-disable-line node/no-deprecated-api
return row => {
// eslint-disable-next-line
return this.factory.namedNode(url.resolve(this.baseIRI, aboutUrlTemplate.fill(row))) // eslint-disable-line node/no-deprecated-api
}
}

Expand All @@ -57,7 +58,7 @@ class TableSchema {
parseColumns () {
const columnNode = RdfUtils.findNode(this.dataset, this.root, this.ns.column)

this.parsedColumns = RdfUtils.parseArray(this.dataset, columnNode).map((node) => {
this.parsedColumns = RdfUtils.parseArray(this.dataset, columnNode).map(node => {
const titles = RdfUtils.findValues(this.dataset, node, this.ns.title)
const name = RdfUtils.findValue(this.dataset, node, this.ns.name) || titles[0]
const aboutUrl = RdfUtils.findValue(this.dataset, node, this.ns.aboutUrl)
Expand Down Expand Up @@ -101,7 +102,7 @@ class TableSchema {

return {
base: this.factory.namedNode('http://www.w3.org/2001/XMLSchema#' + (base || 'string')),
format: format
format
}
}

Expand All @@ -111,15 +112,15 @@ class TableSchema {
this.createAllColumns(row)
}

return this.allColumns.map((column) => {
return this.allColumns.map(column => {
const cellData = { ...row, _name: column.name }

return {
subject: this.subject(column, cellData),
property: this.property(column, cellData),
value: this.value(column, cellData)
}
}).filter((column) => {
}).filter(column => {
return column.value !== undefined
})
} catch (cause) {
Expand All @@ -136,7 +137,8 @@ class TableSchema {
return null
}

return this.factory.namedNode(URL.resolve(this.baseIRI, column.aboutUrl.fill(row))) // eslint-disable-line node/no-deprecated-api
// eslint-disable-next-line
return this.factory.namedNode(url.resolve(this.baseIRI, column.aboutUrl.fill(row))) // eslint-disable-line node/no-deprecated-api
}

value (column, row) {
Expand Down Expand Up @@ -169,7 +171,9 @@ class TableSchema {
}

if (column.datatype.base) {
return this.factory.literal(value, (column.language && column.language.fill(row).toLowerCase()) || column.datatype.base)
const language = column.language && column.language.fill(row).toLowerCase()

return this.factory.literal(value, language || this.factory.namedNode(column.datatype.base))
}
}

Expand All @@ -183,7 +187,9 @@ class TableSchema {
}, [])

const undefinedColumns = difference(Object.keys(row), titles).reduce((titles, title) => {
if (defaultColumnNames.has(title)) return titles
if (defaultColumnNames.has(title)) {
return titles
}

return [...titles, {
name: title,
Expand All @@ -209,4 +215,4 @@ class TableSchema {
}
}

module.exports = TableSchema
export default TableSchema
4 changes: 2 additions & 2 deletions lib/metadata/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Metadata = require('./Metadata')
import Metadata from './Metadata.js'

function metadata (input, { baseIRI, factory, timezone } = {}) {
if (!input || typeof input.match === 'function') {
Expand All @@ -8,4 +8,4 @@ function metadata (input, { baseIRI, factory, timezone } = {}) {
return input
}

module.exports = metadata
export default metadata
4 changes: 2 additions & 2 deletions lib/namespace.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const rdf = require('@rdfjs/data-model')
import rdf from '@rdfjs/data-model'

function namespace (factory) {
factory = factory || rdf
Expand Down Expand Up @@ -40,4 +40,4 @@ function namespace (factory) {
}
}

module.exports = namespace
export default namespace
4 changes: 2 additions & 2 deletions lib/parseDateTime.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { DateTime } = require('luxon')
import { DateTime } from 'luxon'

function parseDateTime (value, format, timezone) {
if (format) {
Expand All @@ -9,4 +9,4 @@ function parseDateTime (value, format, timezone) {
DateTime.fromRFC2822(value, { zone: timezone })
}

module.exports = parseDateTime
export default parseDateTime
Loading

0 comments on commit 4e699c0

Please sign in to comment.