npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@incremunica/incremental-bindings-factory

v1.3.0

Published

Incremental bindings factory module for Comunica

Downloads

12

Readme

Incremunica Incremental 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.

Install

$ yarn add @comunica/incremental-bindings-factory

Usage

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