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

rdf-loader-code

v2.2.0

Published

JavaScript native types loader for the Code ontology

Downloads

17,836

Readme

Native RDF loaders for the code vocabulary

The loaders contained in this packages let developers link RDF to native code and load that code at runtime to be executed in their programs.

Installation

The code loader and the loader registry can be installed with the following lines:

npm i --save rdf-loader-code
npm i --save rdf-loaders-registry

Somewhere at the beginning of your code, register the loaders

import LoaderRegistry from 'rdf-loaders-registry'
import EcmaScriptLoader from 'rdf-loader-code/ecmaScript.js'
import EcmaScriptLiteralLoader from 'rdf-loader-code/ecmaScriptLiteral.js'

const registry = new LoaderRegistry()

EcmaScriptLoader.register(registry)
EcmaScriptLiteralLoader.register(registry)

export default registry

Usage

All examples below assume that the ./dataset module exports a RDF/JS dataset. That dataset would contain the triples presented in the respective turtle snippet. The ./registry module is assumed to be implemented as shown in the Installation section above.

JS code loaded from node modules (or built-ins)

This example is equivalent to calling require('fs').createReadStream

@prefix code: <https://code.described.at/>.

<urn:example:node> 
  code:implementedBy [
    a code:EcmaScript ;
    code:link <node:fs#createReadStream>
  ] .
import rdf from '@zazuko/env'
import registry from './registry.js'
import dataset from './dataset.js'

const term = rdf.namedNode('urn:example:node')
const implementedBy = rdf.namedNode('https://code.described.at/implementedBy')

const createReadStream = registry.load(rdf.clownface({ term, dataset }).out(implementedBy))

JS code loaded from source file

Similar to the above but loads the default export from a local lib/myCode.js file.

@prefix code: <https://code.described.at/>.

<urn:example:node> 
  code:implementedBy [
    a code:EcmaScriptModule ;
    code:link <file:lib/myCode.js#default>
  ] .
import rdf from '@zazuko/env'
import registry from './registry.js'
import dataset from './dataset.js'

const term = rdf.namedNode('urn:example:node')
const implementedBy = rdf.namedNode('https://code.described.at/implementedBy')

const myCode = registry.load(
  rdf.clownface({ term, dataset }).out(implementedBy),
  {
    basePath: process.cwd() // required to resolve relative paths
  })

It is also possible to load named exports similarly to the first example by using hash fragments like file:lib/myCode#namedExport.

JS literal inlined in RDF dataset

A piece of JS code can also be placed directly in the triples.

This snippet shows how an Array#filter callback can be loaded from the dataset.

@prefix code: <https://code.described.at/>.

<urn:example:node> 
  code:implementedBy "name => name.startsWith('A')"^^code:EcmaScript .
import rdf from '@zazuko/env'
import registry from './registry.js'
import dataset from './dataset.js'

const term = rdf.namedNode('urn:example:node')
const implementedBy = rdf.namedNode('https://code.described.at/implementedBy')

const filterFunc = registry.load(rdf.clownface({ term, dataset }).out(implementedBy))

JS template string literal

A dedicated datatype can also be used to load a string based on a template literal. The loader will substitute the variables based on an optional parameter.

@prefix code: <https://code.described.at/>.

<urn:example:node> 
  code:implementedBy "Hello ${name}"^^code:EcmaScriptTemplateLiteral .

To fill in the template string, a Map of variable must be passed to the loader.

import rdf from '@zazuko/env'
import registry from './registry.js'
import dataset from './dataset.js'

const term = rdf.namedNode('urn:example:node')
const implementedBy = rdf.namedNode('https://code.described.at/implementedBy')

const variables = new Map()
variables.set('name', 'World')

const helloString = registry.load(
  rdf.clownface({ term, dataset }).out(implementedBy),
  {
    variables
  })

Loading function arguments

This package also provides an utility loader which can be used to load an array of function arguments. Using this arguments loader it is possible to declare parametrized code within the RDF graph.

Two flavor for such arguments are possible: positional parameters and parameter map.

The actual argument values will be recursively loaded using the loader registry itself.

Note that the arguments loader cannot be registered with the registry because selection is based on an rdf:type which is not available when defining the triples (check the exampels below). For that reason the loader must always be called directly and it is the caller's responsibility to select the correct RDF node which contains the code:arguments property.

Positional parameters

To load positional parameters the graph, simply define them as an rdf:List.

@prefix code: <https://code.described.at/>.

<urn:call:string#startsWith> 
  code:arguments ( "a" 5 ).

Executing the code below against the above triples will return an array containing the values [ "a" "5" ].

The quotes around 5 is no mistake. The loader currently returns a raw literal value from RDF, disregarding its datatype.

import rdf from '@zazuko/env'
import loadArguments from 'rdf-loader-code/arguments.js'
import registry from './registry.js'
import dataset from './dataset.js'

const term = rdf.namedNode('urn:call:string#startsWith')

const argumentsArray = loadArguments(
  rdf.clownface({ term, dataset }), 
  {
    loaderRegistry: registry
  })

Named parameters

Instead of relying on the order of parameters, the loader also out-of-the-box supports loading of an argument map. Such arguments are declared as name/value pairs.

@prefix code: <https://code.described.at/>.
@prefix arg: <https://code.described.at/argument#>.

<urn:call:string#startsWith> 
    code:arguments ([
      arg:first "foo" ;
      arg:second "bar" ;
    ]) .

Executing the code below against the above triples will return an object containing the values

[
  {
    "first": "foo",
    "second": "bar"
  }
]

Mixed arguments

Both methods can be sed together to represent any kind of function signature.

For example, to call a function as below

myFunction('foo', 42, { bar: {  baz: 'baz' } })

You would declare the argument list as follows:

@prefix code: <https://code.described.at/> .
@prefix arg: <https://code.described.at/argument#> .

<urn:call:myFunction>
  code:arguments
    (
      "foo"
      42
      [
        arg:bar
          [
            arg:baz "baz"
          ]
      ]
    ) .