# @rdfjs/serializer-turtle

> Turtle serializer that implements the RDF/JS Sink interface

Latest version **1.1.5** (published 2025-02-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @rdfjs/serializer-turtle
pnpm add @rdfjs/serializer-turtle
yarn add @rdfjs/serializer-turtle
bun add @rdfjs/serializer-turtle
```

## Health

**Score 53/100 (C)** — status: stable.

Positive: has types package; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 1.1.5 |
| Published | 2025-02-04 |
| First published | 2023-01-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/rdfjs__serializer-turtle) |
| Module format | ESM + CommonJS |
| Dependencies | 9 |
| Unpacked size | 35.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Thomas Bergwinkl |
| Maintainers | bergos |
| Keywords | rdf, rdfjs, tree |

## Links

- npm: https://www.npmjs.com/package/@rdfjs/serializer-turtle
- Repository: https://github.com/rdfjs-base/serializer-turtle
- Issues: https://github.com/rdfjs-base/serializer-turtle/issues
- npm.io page: https://npm.io/package/@rdfjs/serializer-turtle

## Dependencies (9)

- [@rdfjs/sink](https://npm.io/package/@rdfjs/sink.md) ^2.0.0
- [@rdfjs/tree](https://npm.io/package/@rdfjs/tree.md) ^0.2.1
- [stream-chunks](https://npm.io/package/stream-chunks.md) ^1.0.0
- [@rdfjs/term-map](https://npm.io/package/@rdfjs/term-map.md) ^2.0.0
- [readable-stream](https://npm.io/package/readable-stream.md) ^4.3.0
- [@rdfjs/namespace](https://npm.io/package/@rdfjs/namespace.md) ^2.0.0
- [@rdfjs/data-model](https://npm.io/package/@rdfjs/data-model.md) ^2.0.1
- [@rdfjs/prefix-map](https://npm.io/package/@rdfjs/prefix-map.md) ^0.1.1
- [@rdfjs/to-ntriples](https://npm.io/package/@rdfjs/to-ntriples.md) ^3.0.1

## Recent versions

- 1.1.5 (latest) — 2025-02-04
- 1.1.4 — 2024-08-09
- 1.1.3 — 2024-06-02
- 1.1.2 — 2024-01-05
- 1.1.1 — 2023-04-08
- 1.1.0 — 2023-02-17
- 1.0.1 — 2023-01-29
- 1.0.0 — 2023-01-18

## README

# @rdfjs/serializer-turtle

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

A [Turtle](https://www.w3.org/TR/turtle/) serializer that implements the [RDF/JS Sink interface](http://rdf.js.org/stream-spec/#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

```bash
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](http://rdf.js.org/stream-spec/#sink-interface), must be called to do the actual serialization.
It expects a [Stream](http://rdf.js.org/stream-spec/#stream-interface) of [Quads](http://rdf.js.org/data-model-spec/#quad-interface) as an argument.
The method will return a [Stream](http://rdf.js.org/stream-spec/#stream-interface) 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.

```javascript
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](http://rdf.js.org/stream-spec/#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](http://rdf.js.org/data-model-spec/#quad-interface) 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.

```javascript
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)
```

---
_Source: https://npm.io/package/@rdfjs/serializer-turtle · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
