# @comunica/utils-bindings-factory

> Bindings factory module for Comunica

Latest version **5.4.0** (published 2026-09-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @comunica/utils-bindings-factory
pnpm add @comunica/utils-bindings-factory
yarn add @comunica/utils-bindings-factory
bun add @comunica/utils-bindings-factory
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 5.4.0 |
| Published | 2026-09-14 |
| First published | 2024-10-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 50.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 592 |
| Maintainers | joachimvh, rubensworks, rubenverborgh, dexagod, jeswr |
| Keywords | comunica, types |

## Links

- npm: https://www.npmjs.com/package/@comunica/utils-bindings-factory
- Repository: https://github.com/comunica/comunica
- Homepage: https://comunica.dev/
- Issues: https://github.com/comunica/comunica/issues
- Funding: https://opencollective.com/comunica-association
- npm.io page: https://npm.io/package/@comunica/utils-bindings-factory

## Dependencies (6)

- [immutable](https://npm.io/package/immutable.md) ^5.1.3
- [rdf-string](https://npm.io/package/rdf-string.md) ^2.0.1
- [@rdfjs/types](https://npm.io/package/@rdfjs/types.md) *
- [@comunica/core](https://npm.io/package/@comunica/core.md) ^5.4.0
- [@comunica/types](https://npm.io/package/@comunica/types.md) ^5.4.0
- [@comunica/bus-merge-bindings-context](https://npm.io/package/@comunica/bus-merge-bindings-context.md) ^5.4.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 5.4.0 (latest) — 2026-09-14
- 4.4.2-alpha.49.0 (next) — 2025-10-22
- 5.3.0 — 2026-07-10
- 5.2.3 — 2026-05-26
- 5.2.2 — 2026-05-03
- 5.2.0 — 2026-04-14
- 5.1.3 — 2026-01-30
- 5.1.0 — 2026-01-16
- 5.0.3 — 2026-01-12
- 5.0.2 — 2026-01-12
- 5.0.0 — 2026-01-08
- 4.5.0 — 2025-11-18
- 4.4.0 — 2025-09-17
- 4.2.0 — 2025-04-29
- 4.1.0 — 2025-02-11
- … 4 more at https://npm.io/package/@comunica/utils-bindings-factory/versions

## README

# Comunica Bindings Factory

[![npm version](https://badge.fury.io/js/%40comunica%2Futils-bindings-factory.svg)](https://www.npmjs.com/package/@comunica/utils-bindings-factory)

This package provides a factory for `Bindings` objects, which allow variables to be mapped to RDF terms.
This package implements the RDF/JS [`BindingsFactory`](http://rdf.js.org/query-spec/#bindingsfactory-interface)
and [`Bindings`](http://rdf.js.org/query-spec/#bindings-interface) interfaces.

Internally, it makes use of [`immutable`](https://www.npmjs.com/package/immutable)
to make sure that operations such as `set` and `delete` reuse internal memory when possible.

This module is part of the [Comunica framework](https://github.com/comunica/comunica),
and should only be used by [developers that want to build their own query engine](https://comunica.dev/docs/modify/).

[Click here if you just want to query with Comunica](https://comunica.dev/docs/query/).

## Install

```bash
$ yarn add @comunica/utils-bindings-factory
```

## Usage

```typescript
import * as RDF from '@rdfjs/types';
import { DataFactory } from 'rdf-data-factory';
import { BindingsFactory } from '@comunica/utils-bindings-factory';

const DF = new DataFactory();
const BF = new BindingsFactory(DF);

const bindings1: RDF.Bindings = BF.bindings([
  [ DF.variable('var1'), DF.literal('abc') ],
  [ DF.variable('var2'), DF.literal('def') ],
]);

const bindings2: RDF.Bindings = BF.fromRecord({
  var1: DF.literal('abc'),
  var2: DF.literal('def'),
});
```

### Factory methods

Factory instances can be used as follows.

#### Creating new bindings

Bindings can either be created by passing key-value pairs, or in record-form:

```typescript
const bindings1: RDF.Bindings = BF.bindings([
  [ DF.variable('var1'), DF.literal('abc') ],
  [ DF.variable('var2'), DF.literal('def') ],
]);

const bindings2: RDF.Bindings = BF.fromRecord({
  var1: DF.literal('abc'),
  var2: DF.literal('def'),
});
```

#### Cloning bindings

Bindings can be cloned as follows:

```typescript
const clonedBindings = BF.fromBindings(otherBindings);
```

### Bindings methods

The following methods are exposed on bindings created by the factory.

Since all bindings are immutable, the original bindings instances will never be changed.

#### `Bindings.has()`

The `has()` method is used to check if a value exists for the given variable.
The variable can either be supplied as a string (without `?` prefix), or as an RDF/JS variable.

```typescript
if (bindings.has('var1')) {
  console.log('Has var1!');
}
if (bindings.has(DF.variable('var2'))) {
  console.log('Has var2!');
}
```

#### `Bindings.get()`

The `get()` method is used to read the bound value of variable.
The variable can either be supplied as a string (without `?` prefix), or as an RDF/JS variable.

```typescript
const term1: RDF.Term | undefined = bindings.get('var1');
const term2: RDF.Term | undefined = bindings.get(DF.variable('var2'));
```

#### `Bindings.set()`

The `set()` method is used to create a copy of the current bindings object, with the addition of a new variable binding.
The variable can either be supplied as a string (without `?` prefix), or as an RDF/JS variable.

```typescript
const newBindings = bindings
  .set('var1', DF.namedNode('ex:term1'))
  .set(DF.variable('var2'), DF.namedNode('ex:term2'));
```

#### `Bindings.delete()`

The `delete()` method is used to create a copy of the current bindings object, with the removal of a variable binding.
The variable can either be supplied as a string (without `?` prefix), or as an RDF/JS variable.

```typescript
const newBindings = bindings
  .delete('var1')
  .delete(DF.variable('var2'));
```

#### `Bindings.keys()`

The `keys()` method returns an iterable over all the RDF/JS variable keys that have a value in this bindings object.

```typescript
for (const variable of bindings.keys()) {
  console.log(variable);
}
```

#### `Bindings.values()`

The `values()` method returns an iterable over all the RDF/JS term values that have a key in this bindings object.

```typescript
for (const term of bindings.values()) {
  console.log(term);
}
```

#### `Bindings.forEach()`

The `forEach()` method iterates over all entries in this bindings object
and invokes the callback with the RDF/JS term value and RDF/JS variable key of each entry.

```typescript
bindings.forEach((value, key) => {
  console.log(key);
  console.log(value);
})
```

#### `Bindings.size`

The `size` field returns the number of key-value entries in the bindings object.

```typescript
console.log(bindings.size);
```

#### Entry iteration

Each bindings object is an Iterable over its key-value entries,
where each entry is a tuple of type `[RDF.Variable, RDF.Term]`.

```typescript
// Iterate over all entries
for (const [ key, value ] of bindings) {
  console.log(key);
  console.log(value);
}

// Save the entries in an array
const entries = [ ...bindings ];
```

#### `Bindings.equals`

The `equals()` method checks if the entries of this bindings object equal the entries in another bindings object.

```typescript
bindings1.equals(bindings2);
```

#### `Bindings.filter`

The `filter()` method creates a new bindings object by filtering entries using a callback.
The callback is applied on each entry.
Returning true indicates that this entry must be contained in the resulting bindings object.

```typescript
const filteredBindings = bindings.filter((value, key) => {
  return key.value !== 'abc';
});
```

#### `Bindings.map`

The `map()` method creates a new bindings object by mapping entries using a callback.
The callback is applied on each entry in which the original value is replaced by the returned value.

```typescript
const mappedBindings = bindings.map((value, key) => {
  return DF.namedNode(value.value + '_modified');
});
```

#### `Bindings.merge`

The `merge()` method merges the entries of this bindings object with all entries of another bindings object.
If a merge conflict occurs (this and other have an equal variable with unequal value), then undefined is returned.

```typescript
const mergedBindings = bindings1.merge(bindings2);
```

#### `Bindings.mergeWith`

The `mergeWith()` method merges this bindings object with another
where merge conflicts can be resolved using a callback function.
The callback function that is invoked when a merge conflict occurs,
for which the returned value is considered the merged value.

```typescript
const mergedBindings = bindings1.mergeWith((self, other, key) => {
  return DF.namedNode(self.value + other.value);
}, bindings2);
```

#### `Bindings.toString`

The `toString()` method returns a compact string representation of the bindings object,
which can be useful for debugging.

```typescript
console.log(bindings.toString());

/*
Can output in the form of:
{
  "a": "ex:a",
  "b": "ex:b",
  "c": "ex:c"
}
 */
```

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