# @rdfjs/traverser

> Generic traverser for RDF/JS datasets

Latest version **0.1.4** (published 2024-06-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install @rdfjs/traverser
pnpm add @rdfjs/traverser
yarn add @rdfjs/traverser
bun add @rdfjs/traverser
```

## Health

**Score 43/100 (D)** — status: abandoned.

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

Warnings: low downloads; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2024-06-07 |
| First published | 2021-12-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/rdfjs__traverser) |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 21.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Thomas Bergwinkl |
| Maintainers | bergos |
| Keywords | rdf, rdfjs, traverser |

## Links

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

## Dependencies (1)

- [@rdfjs/to-ntriples](https://npm.io/package/@rdfjs/to-ntriples.md) ^3.0.1

## Recent versions

- 0.1.4 (latest) — 2024-06-07
- 0.1.3 — 2024-06-02
- 0.1.2 — 2023-04-25
- 0.1.1 — 2023-01-07
- 0.1.0 — 2021-12-18

## README

# @rdfjs/traverser

[![build status](https://img.shields.io/github/actions/workflow/status/rdfjs-base/traverser/test.yaml?branch=master)](https://github.com/rdfjs-base/traverser/actions/workflows/test.yaml)

[![npm version](https://img.shields.io/npm/v/@rdfjs/traverser.svg)](https://www.npmjs.com/package/@rdfjs/traverser)

This package provides a generic traverser for [RDF/JS Datasets](https://rdf.js.org/dataset-spec/).

## Usage

The main export of the package is the `Traverser` class.
It can be imported like this:

```javascript
import Traverser from '@rdfjs/traverser'
```

The package also provides a factory that can be used with [@rdfjs/environment](https://github.com/rdfjs-base/environment):

```javascript
import Environment from '@rdfjs/environment'
import TraverserFactory from '@rdfjs/traverser/Factory.js'

const env = new Environment([DataFactory, DatasetFactory, TraverserFactory])
```

### Traverser(filter, { backward, factory, forward })

Creates a new `Traverser` instance.
A `Traverser` contains only the rules for traversing.
The dataset and the starting point must be given to the methods.  

- `filter`: A filter function that returns a truthy value if the `Traverser` should traverse the given quad.
- `backward`: If true, traverse from object to subject. (default: `false`)
- `factory`: A RDF/JS factory that supports RDF/JS Datasets.
  The function will be called like this: `filter({ dataset, level, quad })`.
  - `dataset`: The RDF/JS Dataset that is traversed.
  - `level`: The number of quads followed until the filter was called.
  - `quad`: The current quad to process.
- `forward`: (default: `true`)

#### forEach ({ term, dataset }, callback)

Calls the given callback function for each quad matching the rules of the `Traverser`, starting from the given `term` and `dataset`.
The callback function is called like this: `callback({ dataset, level, quad })`.
 
- `dataset`: The RDF/JS Dataset that is traversed.
- `level`: The number of quads followed until the callback was called.
- `quad`: The current quad to process.

#### match ({ term, dataset })

Returns a RDF/JS Dataset that contains all quads matching the rules of the `Traverser`, starting from the given `term` and `dataset`.

#### reduce ({ term, dataset }, callback, initialValue)

Calls the given callback function for each quad matching the rules of the `Traverser`, starting from the given `term` and `dataset`.
The callback function is called like this: `callback({ dataset, level, quad }, result)`.

- `dataset`: The RDF/JS Dataset that is traversed.
- `level`: The number of quads followed until the callback was called.
- `quad`: The current quad to process.
- `result`: The return value of the previous call of the callback function.
  If it's called the first time, the `initialValue` is used.

### Factory

The constructor is called by the [@rdfjs/environment](https://github.com/rdfjs-base/environment).
The environment must support the [RDF/JS DatasetFactory](https://rdf.js.org/dataset-spec/) interface. 

#### traverser(filter, { backward = false, forward = true } = {})

Creates a new `Traverser` instance and returns it.

For more details, see the [Traverser](#traverserfilter--backward-factory-forward-) constructor section.

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