Skip to content

Commit

Permalink
Merge pull request #17 from rdfjs-base/pretty
Browse files Browse the repository at this point in the history
feat!: renamed package, added pretty-print serializers
  • Loading branch information
bergos authored Nov 18, 2023
2 parents 9ec386c + eb9833a commit 15c6b41
Show file tree
Hide file tree
Showing 18 changed files with 424 additions and 106 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ jobs:
strategy:
matrix:
node:
- '14'
- '16'
- '18'
- '20'
- '21'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
13 changes: 13 additions & 0 deletions Factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Formats from './lib/Formats.js'

class Factory {
init () {
this.formats = new Formats({ factory: this })
}

clone (original) {
this.formats.import(original.formats)
}
}

export default Factory
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Thomas Bergwinkl

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 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.
38 changes: 33 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
# @rdfjs/formats-common
[![build status](https://img.shields.io/github/workflow/status/rdfjs-base/formats-common/Test)](https://github.com/rdfjs-base/formats-common/actions/workflows/test.yaml)
[![npm version](https://img.shields.io/npm/v/@rdfjs/formats-common.svg)](https://www.npmjs.com/package/@rdfjs/formats-common)
# @rdfjs/formats

[![build status](https://img.shields.io/github/actions/workflow/status/rdfjs-base/formats/test.yaml?branch=master)](https://github.com/rdfjs-base/formats/actions/workflows/test.yaml)
[![npm version](https://img.shields.io/npm/v/@rdfjs/formats.svg)](https://www.npmjs.com/package/@rdfjs/formats)

This module bundles parsers and serializers for the most common RDF formats.
Instances of [SinkMap](https://github.com/rdfjs-base/sink-map) are used to handle different media types.

## Usage

The formats object has a `parsers` and `serializers` property.
Each of it is an instance of `SinkMap` with the most common RDF media types as key.
Each is an instance of `SinkMap` with the most common RDF media types as key.
The formats object is exported as default and can be imported like this:

```javascript
import formats from '@rdfjs/formats'
```

### Pretty-Print Serializers

The default bundle of serializers is optimized for streaming.
The pretty-print formats bundle can be used if a more human-readable output is required.
It can be imported like this:

### Factory

A factory that takes care of parsers and serializers.
An additional `.formats` object will be attached to the environment.
That object is compatible with the data structure of [@rdfjs/formats-common](https://github.com/rdfjs-base/formats-common).
The `.formats` object has an additional `.import()` function to import other format bundles.
The following code shows how to import the [@rdfjs/formats-common](https://github.com/rdfjs-base/formats-common) bundle:


```javascript
import formats from '@rdfjs/formats/pretty.js'
```

## Example

The following example uses the parser for the media type `text/turtle` to parse the given string.
The quads emitted by the `data` event are written to the console:

```javascript
import formats from '@rdfjs/formats-common'
import formats from '@rdfjs/formats'
import { Readable } from 'readable-stream'

const input = Readable.from([`
Expand Down
46 changes: 25 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
import JsonLdParser from '@rdfjs/parser-jsonld'
import N3Parser from '@rdfjs/parser-n3'
import NTriplesSerializer from '@rdfjs/serializer-ntriples'
import TurtleSerializer from '@rdfjs/serializer-turtle'
import SinkMap from '@rdfjs/sink-map'
import JsonLdSerializer from './lib/CustomJsonLdSerializer.js'
import RdfXmlParser from './lib/CustomRdfXmlParser.js'
import JsonLdSerializer from './lib/JsonLdSerializer.js'
import PrettyJsonLdSerializer from './lib/PrettyJsonLdSerializer.js'
import RdfXmlParser from './lib/RdfXmlParser.js'

const parsers = new SinkMap()
const serializers = new SinkMap()
const parsers = new SinkMap([
['application/ld+json', new JsonLdParser()],
['application/trig', new N3Parser()],
['application/n-quads', new N3Parser()],
['application/n-triples', new N3Parser()],
['text/n3', new N3Parser()],
['text/turtle', new N3Parser()],
['application/rdf+xml', new RdfXmlParser()]
])

const serializers = new SinkMap([
['application/ld+json', new JsonLdSerializer()],
['application/n-quads', new NTriplesSerializer()],
['application/n-triples', new NTriplesSerializer()],
['text/n3', new NTriplesSerializer()],
['text/turtle', new NTriplesSerializer()]
])

const formats = {
parsers,
serializers
}

formats.parsers.set('application/ld+json', new JsonLdParser())
formats.parsers.set('application/trig', new N3Parser())
formats.parsers.set('application/n-quads', new N3Parser())
formats.parsers.set('application/n-triples', new N3Parser())
formats.parsers.set('text/n3', new N3Parser())
formats.parsers.set('text/turtle', new N3Parser())
formats.parsers.set('application/rdf+xml', new RdfXmlParser())

formats.serializers.set('application/ld+json', new JsonLdSerializer())
formats.serializers.set('application/n-quads', new NTriplesSerializer())
formats.serializers.set('application/n-triples', new NTriplesSerializer())
formats.serializers.set('text/n3', new NTriplesSerializer())
formats.serializers.set('text/turtle', new NTriplesSerializer())

export { parsers, serializers }
export default formats
export {
formats as default,
JsonLdParser,
JsonLdSerializer,
N3Parser,
NTriplesSerializer,
RdfXmlParser
PrettyJsonLdSerializer,
RdfXmlParser,
TurtleSerializer
}
9 changes: 0 additions & 9 deletions lib/CustomJsonLdSerializer.js

This file was deleted.

9 changes: 0 additions & 9 deletions lib/CustomRdfXmlParser.js

This file was deleted.

27 changes: 27 additions & 0 deletions lib/Formats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import SinkMap from '@rdfjs/sink-map'

class Formats {
constructor ({ factory }) {
this.factory = factory
this.parsers = new SinkMap()
this.serializers = new SinkMap()
}

import (other) {
if (other.parsers) {
for (const [mediaType, parser] of other.parsers) {
this.parsers.set(mediaType, new parser.constructor({ factory: this.factory }))
}
}

if (other.serializers) {
for (const [mediaType, serializer] of other.serializers) {
this.serializers.set(mediaType, new serializer.constructor({ factory: this.factory }))
}
}

return this
}
}

export default Formats
9 changes: 9 additions & 0 deletions lib/JsonLdSerializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import JsonLdSerializerBase from '@rdfjs/serializer-jsonld'

class JsonLdSerializer extends JsonLdSerializerBase {
constructor ({ ...args } = {}) {
super({ ...args, encoding: 'string' })
}
}

export default JsonLdSerializer
14 changes: 14 additions & 0 deletions lib/PrettyJsonLdSerializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import JsonLdSerializer from '@rdfjs/serializer-jsonld-ext'

class PrettyJsonLdSerializer extends JsonLdSerializer {
constructor ({ ...args } = {}) {
super({
...args,
compact: true,
encoding: 'string',
prettyPrint: true
})
}
}

export default PrettyJsonLdSerializer
9 changes: 9 additions & 0 deletions lib/RdfXmlParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { RdfXmlParser as RdfXmlParserBase } from 'rdfxml-streaming-parser'

class RdfXmlParser extends RdfXmlParserBase {
constructor ({ factory, ...args } = {}) {
super({ ...args, dataFactory: factory })
}
}

export default RdfXmlParser
25 changes: 14 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "@rdfjs/formats-common",
"name": "@rdfjs/formats",
"version": "3.1.0",
"description": "Parsers and serializers for common RDF formats",
"description": "Bundle of RDF/JS parsers and serializers",
"type": "module",
"main": "index.js",
"scripts": {
"test": "stricter-standard && c8 --reporter=lcov --reporter=text mocha"
},
"repository": {
"type": "git",
"url": "https://github.com/rdfjs-base/formats-common.git"
"url": "https://github.com/rdfjs-base/formats.git"
},
"keywords": [
"rdf",
Expand All @@ -21,20 +21,23 @@
"author": "Thomas Bergwinkl <[email protected]> (https://www.bergnet.org/people/bergi/card#me)",
"license": "MIT",
"bugs": {
"url": "https://github.com/rdfjs-base/formats-common/issues"
"url": "https://github.com/rdfjs-base/formats/issues"
},
"homepage": "https://github.com/rdfjs-base/formats-common",
"homepage": "https://github.com/rdfjs-base/formats",
"dependencies": {
"@rdfjs/parser-jsonld": "^2.0.0",
"@rdfjs/parser-n3": "^2.0.0",
"@rdfjs/parser-jsonld": "^2.1.0",
"@rdfjs/parser-n3": "^2.0.1",
"@rdfjs/serializer-jsonld": "^2.0.0",
"@rdfjs/serializer-jsonld-ext": "^4.0.0",
"@rdfjs/serializer-ntriples": "^2.0.0",
"@rdfjs/serializer-turtle": "^1.1.1",
"@rdfjs/sink-map": "^2.0.0",
"rdfxml-streaming-parser": "^2.2.0"
"rdfxml-streaming-parser": "^2.3.0"
},
"devDependencies": {
"c8": "^7.12.0",
"mocha": "^10.0.0",
"stricter-standard": "^0.2.0"
"@rdfjs/environment": "^1.0.0",
"c8": "^8.0.1",
"mocha": "^10.2.0",
"stricter-standard": "^0.3.0"
}
}
17 changes: 17 additions & 0 deletions pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import SinkMap from '@rdfjs/sink-map'
import defaultFormats, { PrettyJsonLdSerializer, TurtleSerializer } from './index.js'

const parsers = new SinkMap(defaultFormats.parsers)

const serializers = new SinkMap(defaultFormats.serializers)

serializers.set('application/ld+json', new PrettyJsonLdSerializer())
serializers.set('text/n3', new TurtleSerializer())
serializers.set('text/turtle', new TurtleSerializer())

const formats = {
parsers,
serializers
}

export default formats
45 changes: 45 additions & 0 deletions test/Factory.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { notStrictEqual, strictEqual } from 'assert'
import Environment from '@rdfjs/environment'
import { describe, it } from 'mocha'
import Factory from '../Factory.js'
import Formats from '../lib/Formats.js'

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

it('should attach a Formats instance to .formats', () => {
const env = new Environment([Factory])

strictEqual(env.formats instanceof Formats, true)
})

it('should assign itself as factory to the formats instance', () => {
const env = new Environment([Factory])

strictEqual(env.formats.factory, env)
})

describe('.clone', () => {
it('should import the parsers and serializers from the original environment', () => {
class Parser {}
class Serializer {}
const original = {
parsers: new Map([['a', new Parser()]]),
serializers: new Map([['a', new Serializer()]])
}
const env = new Environment([Factory])
env.formats.import(original)

const clone = env.clone()

strictEqual(clone.formats.parsers.size, 1)
strictEqual(clone.formats.parsers.get('a') instanceof Parser, true)
notStrictEqual(clone.formats.parsers.get('a'), env.formats.parsers.get('a'))
strictEqual(clone.formats.serializers.size, 1)
strictEqual(clone.formats.serializers.get('a') instanceof Serializer, true)
notStrictEqual(clone.formats.serializers.get('a'), env.formats.serializers.get('a'))
})
})
})
Loading

0 comments on commit 15c6b41

Please sign in to comment.