JSPM

@rdfjs/serializer-turtle

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8483
  • Score
    100M100P100Q149243F
  • License MIT

Turtle serializer that implements the RDF/JS Sink interface

Package Exports

  • @rdfjs/serializer-turtle
  • @rdfjs/serializer-turtle/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@rdfjs/serializer-turtle) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@rdfjs/serializer-turtle

build status npm version

A Turtle serializer that implements the RDF/JS Sink interface. It serializes the given quads to a pretty-printed Turtle string.

All quads need to be kept in memory for pretty-printing. The quads of the stream are collected and serialized after the last quad is written.

Install

npm install --save @rdfjs/serializer-turtle

Usage

The package exports the serializer as a class, so an instance must be created before it can be used. The .import method, as defined in the RDF/JS specification, must be called to do the actual serialization. It expects a Stream of Quads as an argument. The method will return a Stream that emits the Turtle as a string.

Example

This example shows how to create a serializer instance and feed it with a stream of quads. The Turtle emitted by the serializer will be written to stdout.

import { Readable } from 'stream'
import rdf from '@rdfjs/data-model'
import Serializer from '@rdfjs/serializer-turtle'

const serializer = new Serializer()
const input = Readable.from([
  rdf.quad(
    rdf.namedNode('https://housemd.rdf-ext.org/person/gregory-house'),
    rdf.namedNode('http://schema.org/givenName'),
    rdf.literal('Gregory')),
  rdf.quad(
    rdf.namedNode('https://housemd.rdf-ext.org/person/gregory-house'),
    rdf.namedNode('http://schema.org/familyName'),
    rdf.literal('House')),
  rdf.quad(
    rdf.namedNode('https://housemd.rdf-ext.org/person/gregory-house'),
    rdf.namedNode('http://schema.org/knows'),
    rdf.namedNode('https://housemd.rdf-ext.org/person/james-wilson'))
])

const output = serializer.import(input)
output.pipe(process.stdout)

transform(quads)

The serializer code runs sync, and the RDF/JS Sink interface is just a wrapper. If your use case is very specific, with a low chance of using other formats, it can be used directly. The .transform method accepts Quads provided as an object that implements the Symbol.iterator method. It returns the generated Turtle code as a string.

Example

This example shows how to create a serializer instance and feed it with quads. The returned Turtle will be written to the console.

import rdf from '@rdfjs/data-model'
import Serializer from '@rdfjs/serializer-turtle'

const serializer = new Serializer()
const input = [
  rdf.quad(
    rdf.namedNode('https://housemd.rdf-ext.org/person/gregory-house'),
    rdf.namedNode('http://schema.org/givenName'),
    rdf.literal('Gregory')),
  rdf.quad(
    rdf.namedNode('https://housemd.rdf-ext.org/person/gregory-house'),
    rdf.namedNode('http://schema.org/familyName'),
    rdf.literal('House')),
  rdf.quad(
    rdf.namedNode('https://housemd.rdf-ext.org/person/gregory-house'),
    rdf.namedNode('http://schema.org/knows'),
    rdf.namedNode('https://housemd.rdf-ext.org/person/james-wilson'))
]

const output = serializer.transform(input)
process.stdout.write(output)