@comunica/bindings-factory

中文文档
3.3.0发布于 2024/10/08MITTypeScriptCommonJS近 30 天下载 1.9万

Bindings factory module for Comunica

原始使用文档
文档目录4 节
  1. Install
  2. Usage
  3. Factory methods
  4. Bindings methods

Comunica Bindings Factory

npm version

This package provides a factory for Bindings objects, which allow variables to be mapped to RDF terms. This package implements the RDF/JS BindingsFactory and Bindings interfaces.

Internally, it makes use of immutable to make sure that operations such as set and delete reuse internal memory when possible.

This module is part of the Comunica framework, and should only be used by developers that want to build their own query engine.

Click here if you just want to query with Comunica.

Install

$ yarn add @comunica/bindings-factory

Usage

import * as RDF from '@rdfjs/types';
import { DataFactory } from '@comunica/data-factory';
import { BindingsFactory } from '@comunica/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:

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:

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.

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.

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.

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.

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.

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.

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.

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.

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].

// 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.

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.

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.

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.

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.

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.

console.log(bindings.toString());

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

版本与发布记录

@comunica/bindings-factory 共发布 55 个版本,首次发布于 2022/02/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.3.0latest2024/10/082 年前
3.2.12024/08/072 年前
3.2.02024/07/052 年前
3.1.02024/05/112 年前
3.0.32024/04/122 年前
3.0.12024/03/192 年前

其他发布通道:next → 3.0.1-alpha.46.0。

关于 @comunica/bindings-factory 的常见问题

@comunica/bindings-factory 是什么?

Bindings factory module for Comunica

如何安装 @comunica/bindings-factory?

在项目目录运行 npm install @comunica/bindings-factory,或使用 pnpm add / yarn add / bun add。

@comunica/bindings-factory 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@comunica/bindings-factory 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@comunica/bindings-factory 可以商用吗?

@comunica/bindings-factory 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@comunica/bindings-factory 的下载量有多少?

根据 npm 官方下载统计,@comunica/bindings-factory 在 2026/10/11 – 2026/11/09 这 30 天内被下载 19,089 次,日均约 636 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@comunica/bindings-factory 还在维护吗?

最新版本 3.3.0 发布于 2024/10/08(2 年前);至今共发布 55 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/02/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。