New:Introducing Socket Scanning for VS Code Marketplace Extensions.Learn more →
Get Started

@rdfjs/serializer-rdfjs

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rdfjs/serializer-rdfjs

RDF/JS JavaScript code serializer that implements the RDF/JS Sink interface

latest
Source
npmnpm
Version
0.1.3
Version published
Maintainers
1
Created
Source

@rdfjs/serializer-rdfjs

build status

npm version

RDF/JS JavaScript code serializer that implements the RDF/JS Sink interface. It serializes the given quads to a JavaScript module that exports a single function, like shown below. The created function will return an array of the re-created quads.

export default ({ factory }) => {
  return [
    factory.quad(
      factory.blankNode('foo'),
      factory.namedNode('http://example.com/bar'),
      factory.literal('baz')
    )
  ]
}

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 quad stream as argument. The method will return a stream that emits the JavaScript code as a string.

Example

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

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


const serializer = new Serializer()
const input = new Readable({
  objectMode: true,
  read: () => {
    input.push(rdf.quad(
      rdf.namedNode('http://example.org/sheldon-cooper'),
      rdf.namedNode('http://schema.org/givenName'),
      rdf.literal('Sheldon')))
    input.push(rdf.quad(
      rdf.namedNode('http://example.org/sheldon-cooper'),
      rdf.namedNode('http://schema.org/familyName'),
      rdf.literal('Cooper')))
    input.push(rdf.quad(
      rdf.namedNode('http://example.org/sheldon-cooper'),
      rdf.namedNode('http://schema.org/knows'),
      rdf.namedNode('http://example.org/amy-farrah-fowler')))
    input.push(null)
  }
})

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

Target module type

By default, the serializer will produce an ECMAScript module. The optional argument module can be used to serialize it to CommonJS or TypeScript.

Example

import Serializer from '@rdfjs/serializer-rdfjs'
import quadStream from './quads'

// serialize to CommonJS by default
const serializer = new Serializer({
  module: 'commonjs'
})

// call to .import also accepts same options parameter
const typescriptStream = serializer.import(quadStream, {
  module: 'ts'
})

transform(quads)

The actual 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 to use 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 JavaScript code as a string.

Example

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

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

const serializer = new Serializer()
const code = serializer.transform([
  rdf.quad(
    rdf.namedNode('http://example.org/sheldon-cooper'),
    rdf.namedNode('http://schema.org/givenName'),
    rdf.literal('Sheldon')),
  rdf.quad(
    rdf.namedNode('http://example.org/sheldon-cooper'),
    rdf.namedNode('http://schema.org/familyName'),
    rdf.literal('Cooper')),
  rdf.quad(
    rdf.namedNode('http://example.org/sheldon-cooper'),
    rdf.namedNode('http://schema.org/knows'),
    rdf.namedNode('http://example.org/amy-farrah-fowler'))
])

console.log(code)

Keywords

rdf

FAQs

Package last updated on 06 Jun 2024

Related posts