Skip to content

Commit

Permalink
refactor: move operations to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf authored and bergos committed Jun 15, 2021
1 parent fa90e3b commit 519e912
Show file tree
Hide file tree
Showing 24 changed files with 195 additions and 156 deletions.
31 changes: 31 additions & 0 deletions packages/rdf/.github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Test

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node: ['14', '15' ]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm test
- name: Codecov
uses: codecov/codecov-action@v1.0.5
with:
token: ${{ secrets.CODECOV_TOKEN }}

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '14'
- run: npm install
- run: npm run lint
3 changes: 3 additions & 0 deletions packages/rdf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# v1.0.0

- Moved to JavaScript modules
6 changes: 3 additions & 3 deletions packages/rdf/cube.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const buildCubeShape = require('./lib/cube/buildCubeShape')
const toObservation = require('./lib/cube/toObservation')
import buildCubeShape from './lib/cube/buildCubeShape.js'
import toObservation from './lib/cube/toObservation.js'

module.exports = {
export {
buildCubeShape,
toObservation
}
4 changes: 2 additions & 2 deletions packages/rdf/lib/PatternMatcher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const TermSet = require('@rdfjs/term-set')
import TermSet from '@rdfjs/term-set'

class PatternMatcher {
constructor ({ subject, predicate, object, graph } = {}) {
Expand Down Expand Up @@ -27,4 +27,4 @@ class PatternMatcher {
}
}

module.exports = PatternMatcher
export default PatternMatcher
12 changes: 6 additions & 6 deletions packages/rdf/lib/cube/buildCubeShape/Cube.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const clownface = require('clownface')
const rdf = require('rdf-ext')
const TermMap = require('@rdfjs/term-map')
const Dimension = require('./Dimension')
const ns = require('./namespaces')
import clownface from 'clownface'
import rdf from 'rdf-ext'
import TermMap from '@rdfjs/term-map'
import Dimension from './Dimension.js'
import * as ns from './namespaces.js'

class Cube {
constructor ({ term, observationSet, shape }) {
Expand Down Expand Up @@ -51,4 +51,4 @@ class Cube {
}
}

module.exports = Cube
export default Cube
12 changes: 6 additions & 6 deletions packages/rdf/lib/cube/buildCubeShape/Dimension.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const clownface = require('clownface')
const rdf = require('rdf-ext')
const TermMap = require('@rdfjs/term-map')
const TermSet = require('@rdfjs/term-set')
const ns = require('./namespaces')
import clownface from 'clownface'
import rdf from 'rdf-ext'
import TermMap from '@rdfjs/term-map'
import TermSet from '@rdfjs/term-set'
import * as ns from './namespaces.js'

const datatypeParsers = new TermMap([
[ns.xsd.date, term => new Date(term.value)],
Expand Down Expand Up @@ -99,4 +99,4 @@ class Dimension {
}
}

module.exports = Dimension
export default Dimension
18 changes: 9 additions & 9 deletions packages/rdf/lib/cube/buildCubeShape/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const clownface = require('clownface')
const TermMap = require('@rdfjs/term-map')
const TermSet = require('@rdfjs/term-set')
const rdf = require('rdf-ext')
const { Transform } = require('readable-stream')
const Cube = require('./Cube')
const ns = require('./namespaces')
const urlJoin = require('../../urlJoin')
import clownface from 'clownface'
import TermMap from '@rdfjs/term-map'
import TermSet from '@rdfjs/term-set'
import rdf from 'rdf-ext'
import { Transform } from 'readable-stream'
import Cube from './Cube.js'
import * as ns from './namespaces.js'
import urlJoin from '../../urlJoin.js'

function defaultCube ({ observationSet }) {
const observationSetIri = observationSet && observationSet.value
Expand Down Expand Up @@ -84,4 +84,4 @@ function buildCubeShape ({ excludeValuesOf } = {}) {
return new CubeShapeBuilder({ excludeValuesOf })
}

module.exports = buildCubeShape
export default buildCubeShape
16 changes: 7 additions & 9 deletions packages/rdf/lib/cube/buildCubeShape/namespaces.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const namespace = require('@rdfjs/namespace')
import namespace from '@rdfjs/namespace'

const ns = {
cube: namespace('https://cube.link/'),
rdf: namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#'),
rdfs: namespace('http://www.w3.org/2000/01/rdf-schema#'),
sh: namespace('http://www.w3.org/ns/shacl#'),
xsd: namespace('http://www.w3.org/2001/XMLSchema#')
}
const cube = namespace('https://cube.link/')
const rdf = namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
const rdfs = namespace('http://www.w3.org/2000/01/rdf-schema#')
const sh = namespace('http://www.w3.org/ns/shacl#')
const xsd = namespace('http://www.w3.org/2001/XMLSchema#')

module.exports = ns
export { cube, rdf, rdfs, sh, xsd }
20 changes: 10 additions & 10 deletions packages/rdf/lib/cube/toObservation.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { URL } = require('url')
const clownface = require('clownface')
const namespace = require('@rdfjs/namespace')
const TermMap = require('@rdfjs/term-map')
const TermSet = require('@rdfjs/term-set')
const rdf = require('rdf-ext')
const { Transform } = require('readable-stream')
const dateToId = require('../dateToId')
const urlJoin = require('../urlJoin')
import { URL } from 'url'
import clownface from 'clownface'
import namespace from '@rdfjs/namespace'
import TermMap from '@rdfjs/term-map'
import TermSet from '@rdfjs/term-set'
import rdf from 'rdf-ext'
import { Transform } from 'readable-stream'
import dateToId from '../dateToId.js'
import urlJoin from '../urlJoin.js'

const ns = {
cube: namespace('https://cube.link/'),
Expand Down Expand Up @@ -241,4 +241,4 @@ function toObservation ({
return new ToObservation({ blacklist, dimensions, observation, observations, observer, useDate, dateProperty, useIndex })
}

module.exports = toObservation
export default toObservation
2 changes: 1 addition & 1 deletion packages/rdf/lib/dateToId.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ function dateToId (date) {
// [^0-9Z]
}

module.exports = dateToId
export default dateToId
6 changes: 3 additions & 3 deletions packages/rdf/lib/urlJoin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { join } = require('path')
const { URL } = require('url')
import { join } from 'path'
import { URL } from 'url'

function urlJoin (base, part) {
const url = new URL(base)
Expand All @@ -9,4 +9,4 @@ function urlJoin (base, part) {
return url.toString()
}

module.exports = urlJoin
export default urlJoin
4 changes: 2 additions & 2 deletions packages/rdf/manifest.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
rdfs:label "Map (RDF/JS Quad)";
rdfs:comment "Calls a map function only for quads matching the given triple pattern.";
code:implementedBy [ a code:EcmaScript;
code:link <node:barnard59-rdf/mapMatch.js>
code:link <node:barnard59-rdf/mapMatch.js#default>
].

<cube.js#buildCubeShape> a p:Operation, p:WritableObjectMode, p:ReadableObjectMode;
Expand All @@ -28,5 +28,5 @@
rdfs:label "Set Graph";
rdfs:comment "Sets the graph of all quads to the given fixed value.";
code:implementedBy [ a code:EcmaScript;
code:link <node:barnard59-rdf/setGraph.js>
code:link <node:barnard59-rdf/setGraph.js#default>
].
10 changes: 4 additions & 6 deletions packages/rdf/mapMatch.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const rdf = require('rdf-ext')
const { Transform } = require('readable-stream')
const PatternMatcher = require('./lib/PatternMatcher')
import rdf from 'rdf-ext'
import { Transform } from 'readable-stream'
import PatternMatcher from './lib/PatternMatcher.js'

function mapMatch ({ map, subject, predicate, object, graph }) {
export default function mapMatch ({ map, subject, predicate, object, graph }) {
const matcher = new PatternMatcher({ subject, predicate, object, graph })

return new Transform({
Expand All @@ -21,5 +21,3 @@ function mapMatch ({ map, subject, predicate, object, graph }) {
}
})
}

module.exports = mapMatch
7 changes: 6 additions & 1 deletion packages/rdf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"version": "0.2.4",
"description": "RDF support for Linked Data pipelines",
"main": "index.js",
"type": "module",
"scripts": {
"test": "standard && nyc --reporter=lcov mocha"
"coverage": "codecov",
"lint": "standard",
"test": "nyc --reporter=lcov mocha"
},
"repository": {
"type": "git",
Expand All @@ -28,7 +31,9 @@
},
"devDependencies": {
"@rdfjs/to-ntriples": "^1.0.2",
"codecov": "^3.6.5",
"get-stream": "^6.0.1",
"into-stream": "^5.1.1",
"isstream": "^0.1.2",
"mocha": "^8.4.0",
"nyc": "^15.0.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/rdf/setGraph.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const rdf = require('rdf-ext')
const TripleToQuadTransform = require('rdf-transform-triple-to-quad')
import rdf from 'rdf-ext'
import TripleToQuadTransform from 'rdf-transform-triple-to-quad'

function setGraph (graph) {
const iri = (graph && graph.value) || (graph && graph.toString()) || ''
Expand All @@ -11,4 +11,4 @@ function setGraph (graph) {
return new TripleToQuadTransform(rdf.namedNode(iri), { factory: rdf })
}

module.exports = setGraph
export default setGraph
76 changes: 38 additions & 38 deletions packages/rdf/test/PatternMatcher.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const { strictEqual } = require('assert')
const { describe, it } = require('mocha')
const rdf = require('rdf-ext')
const ns = require('./support/namespaces')
const PatternMatcher = require('../lib/PatternMatcher')
import { strictEqual } from 'assert'
import { describe, it } from 'mocha'
import rdf from 'rdf-ext'
import { ex } from './support/namespaces.js'
import PatternMatcher from '../lib/PatternMatcher.js'

describe('PatternMatcher', () => {
it('should be a constructor', () => {
strictEqual(typeof PatternMatcher, 'function')
})

it('should assign the given terms to the pattern object', () => {
const subject = ns.ex.subject
const predicate = ns.ex.predicate
const object = ns.ex.object
const graph = ns.ex.graph
const subject = ex.subject
const predicate = ex.predicate
const object = ex.object
const graph = ex.graph

const matcher = new PatternMatcher({ subject, predicate, object, graph })

Expand All @@ -24,14 +24,14 @@ describe('PatternMatcher', () => {
})

it('should assign the given iterable to the pattern object', () => {
const subject1 = ns.ex.subject1
const subject2 = ns.ex.subject2
const predicate1 = ns.ex.predicate1
const predicate2 = ns.ex.predicate2
const object1 = ns.ex.object1
const object2 = ns.ex.object2
const graph1 = ns.ex.graph1
const graph2 = ns.ex.graph2
const subject1 = ex.subject1
const subject2 = ex.subject2
const predicate1 = ex.predicate1
const predicate2 = ex.predicate2
const object1 = ex.object1
const object2 = ex.object2
const graph1 = ex.graph1
const graph2 = ex.graph2

const matcher = new PatternMatcher({
subject: [subject1, subject2],
Expand All @@ -58,23 +58,23 @@ describe('PatternMatcher', () => {
})

it('should return false if the quad doesn\'t match the pattern', () => {
const subject = ns.ex.subject
const predicate = ns.ex.predicate
const object = ns.ex.object
const graph = ns.ex.graph
const subject = ex.subject
const predicate = ex.predicate
const object = ex.object
const graph = ex.graph

const matcher = new PatternMatcher({ subject, predicate, object, graph })

const result = matcher.test(rdf.quad(subject, predicate, object, ns.ex.graph1))
const result = matcher.test(rdf.quad(subject, predicate, object, ex.graph1))

strictEqual(result, false)
})

it('should return true if the subject matches', () => {
const subject = ns.ex.subject
const predicate = ns.ex.predicate
const object = ns.ex.object
const graph = ns.ex.graph
const subject = ex.subject
const predicate = ex.predicate
const object = ex.object
const graph = ex.graph

const matcher = new PatternMatcher({ subject })

Expand All @@ -84,10 +84,10 @@ describe('PatternMatcher', () => {
})

it('should return true if the predicate matches', () => {
const subject = ns.ex.subject
const predicate = ns.ex.predicate
const object = ns.ex.object
const graph = ns.ex.graph
const subject = ex.subject
const predicate = ex.predicate
const object = ex.object
const graph = ex.graph

const matcher = new PatternMatcher({ predicate })

Expand All @@ -97,10 +97,10 @@ describe('PatternMatcher', () => {
})

it('should return true if the object matches', () => {
const subject = ns.ex.subject
const predicate = ns.ex.predicate
const object = ns.ex.object
const graph = ns.ex.graph
const subject = ex.subject
const predicate = ex.predicate
const object = ex.object
const graph = ex.graph

const matcher = new PatternMatcher({ object })

Expand All @@ -110,10 +110,10 @@ describe('PatternMatcher', () => {
})

it('should return true if the graph matches', () => {
const subject = ns.ex.subject
const predicate = ns.ex.predicate
const object = ns.ex.object
const graph = ns.ex.graph
const subject = ex.subject
const predicate = ex.predicate
const object = ex.object
const graph = ex.graph

const matcher = new PatternMatcher({ graph })

Expand Down
4 changes: 2 additions & 2 deletions packages/rdf/test/cube.test.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require('./cube/buildCubeShape.test')
require('./cube/toObservation.test')
import './cube/buildCubeShape.test.js'
import './cube/toObservation.test.js'
Loading

0 comments on commit 519e912

Please sign in to comment.