Socket
Socket
Sign inDemoInstall

@rdfjs-elements/formats-pretty

Package Overview
Dependencies
96
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @rdfjs-elements/formats-pretty

RDF/JS Sink Maps with serializers configured for pretty printing


Version published
Weekly downloads
1.1K
decreased by-53.85%
Maintainers
1
Install size
25.8 MB
Created
Weekly downloads
 

Readme

Source

@rdfjs-elements/formats-pretty

Common RDF/JS parsers and serializers, the latter returning a nicely formatted output.

For basic usage see @rdfjs/formats-common

Serializers

FormatPretty or notImplementation
application/ld+json😀@rdfjs/serializer-jsonld-ext
application/trig🤩@graphy/content.trig.writer
text/n3🤩@graphy/content.ttl.writer
text/turtle🤩@graphy/content.ttl.writer
application/n-triples😶@rdfjs/serializer-ntriples
application/n-quads😶@rdfjs/serializer-ntriples
application/rdf+xml😶@graphy/content.xml.scribe

Individual serializer skins can also be created by importing from @rdfjs-elements/formats-pretty/serializers. This allows for initializing a sink preloaded with a given set of prefixes.

import prefixes from '@zazuko/prefixes'
import { TurtleSerializer } from '@rdfjs-elements/formats-pretty'

const { schema, dcterms, foaf } = prefixes

const sink = new TurtleSerializer({
  prefixes: { schema, dcterms, foaf, ex: 'http://example/org/' }
})

This sink can then be used to produce pretty-printed RDF

import rdf from '@rdfjs/data-model'
import { Readable } from 'readable-stream'
import getStream from 'get-stream'

// Example data
const data = [
  rdf.quad(rdf.namedNode('http://example/org/s1'), rdf.namedNode('http://schema.org/name'), rdf.literal('Alice')),
  rdf.quad(rdf.namedNode('http://example/org/s1'), rdf.namedNode('http://xmlns.com/foaf/0.1/knows'), rdf.namedNode('http://example/org/o1')),
  rdf.quad(rdf.namedNode('http://example/org/o1'), rdf.namedNode('http://schema.org/name'), rdf.literal('Bob'))
]

const stream = await sink.import(Readable.from(data))
console.log(await getStream(stream))

// Outputs:

// @prefix schema: <http://schema.org/> .
// @prefix dcterms: <http://purl.org/dc/terms/> .
// @prefix foaf: <http://xmlns.com/foaf/0.1/> .
// @prefix ex: <http://example/org/> .
//
//   ex:s1 schema:name "Alice" ;
//        foaf:knows ex:o1 .
//
//   ex:o1 schema:name "Bob" .

To get an output in n-triples, n-quads or n3 format the sink map interface can be used where media type must be specified:

import rdf from '@rdfjs/data-model'
import prefixes from '@zazuko/prefixes'
import formats from '@rdfjs-elements/formats-pretty'
import { Readable } from 'readable-stream'
import getStream from 'get-stream'

// Example data
const data = [
  rdf.quad(rdf.namedNode('http://example/org/s1'), rdf.namedNode('http://schema.org/name'), rdf.literal('Alice')),
  rdf.quad(rdf.namedNode('http://example/org/s1'), rdf.namedNode('http://xmlns.com/foaf/0.1/knows'), rdf.namedNode('http://example/org/o1')),
  rdf.quad(rdf.namedNode('http://example/org/o1'), rdf.namedNode('http://schema.org/name'), rdf.literal('Bob'))
]

const { schema, dcterms, foaf } = prefixes

const stream = formats.serialisers.import('application/n-triples', Readable.from(data), { schema, dcterms, foaf, ex:'http://example/org/' })

console.log(await getStream(stream))

Parsers

FormatImplementation
application/ld+json@rdfjs/parser-jsonld
application/trig@rdfjs/parser-n3
text/n3@rdfjs/parser-n3
text/turtle@rdfjs/parser-n3
application/n-triples@rdfjs/parser-n3
application/n-quads@rdfjs/parser-n3
application/rdf+xmlrdfxml-streaming-parser

FAQs

Last updated on 19 Jan 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc