{"id":26190131,"url":"https://github.com/zazuko/rdf-loader-code","last_synced_at":"2025-04-15T02:09:42.786Z","repository":{"id":48832403,"uuid":"170331663","full_name":"zazuko/rdf-loader-code","owner":"zazuko","description":"JavaScript native types loader for the Code ontology","archived":false,"fork":false,"pushed_at":"2025-04-08T09:24:29.000Z","size":270,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-15T02:09:36.651Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zazuko.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-12T14:25:20.000Z","updated_at":"2025-04-08T09:24:11.000Z","dependencies_parsed_at":"2022-09-01T10:42:06.612Z","dependency_job_id":"498a309f-4119-44cd-8287-dd26286b31c7","html_url":"https://github.com/zazuko/rdf-loader-code","commit_stats":{"total_commits":39,"total_committers":5,"mean_commits":7.8,"dds":0.4358974358974359,"last_synced_commit":"552aaa61a0985308ce9b0e5e909d9db633c3fb88"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zazuko%2Frdf-loader-code","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zazuko%2Frdf-loader-code/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zazuko%2Frdf-loader-code/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zazuko%2Frdf-loader-code/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zazuko","download_url":"https://codeload.github.com/zazuko/rdf-loader-code/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248991544,"owners_count":21194894,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-12T00:52:41.192Z","updated_at":"2025-04-15T02:09:42.771Z","avatar_url":"https://github.com/zazuko.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Native RDF loaders for the [code](https://code.described.at) vocabulary\n\nThe loaders contained in this packages let developers link RDF to native code and load that code at runtime\nto be executed in their programs.\n\n## Installation\n\nThe code loader and the loader registry can be installed with the following lines:  \n\n```\nnpm i --save rdf-loader-code\nnpm i --save rdf-loaders-registry\n```\n\nSomewhere at the beginning of your code, register the loaders\n\n```js\nimport LoaderRegistry from 'rdf-loaders-registry'\nimport EcmaScriptLoader from 'rdf-loader-code/ecmaScript.js'\nimport EcmaScriptLiteralLoader from 'rdf-loader-code/ecmaScriptLiteral.js'\n\nconst registry = new LoaderRegistry()\n\nEcmaScriptLoader.register(registry)\nEcmaScriptLiteralLoader.register(registry)\n\nexport default registry\n```\n\n## Usage\n\nAll examples below assume that the `./dataset` module exports a [`RDF/JS` dataset](https://rdf.js.org/dataset-spec/).\nThat dataset would contain the triples presented in the respective turtle snippet.\nThe `./registry` module is assumed to be implemented as shown in the `Installation` section above.\n\n### JS code loaded from node modules (or built-ins)\n\nThis example is equivalent to calling `require('fs').createReadStream` \n\n```turtle\n@prefix code: \u003chttps://code.described.at/\u003e.\n\n\u003curn:example:node\u003e \n  code:implementedBy [\n    a code:EcmaScript ;\n    code:link \u003cnode:fs#createReadStream\u003e\n  ] .\n```\n\n```js\nimport rdf from '@zazuko/env'\nimport registry from './registry.js'\nimport dataset from './dataset.js'\n\nconst term = rdf.namedNode('urn:example:node')\nconst implementedBy = rdf.namedNode('https://code.described.at/implementedBy')\n\nconst createReadStream = registry.load(rdf.clownface({ term, dataset }).out(implementedBy))\n```\n\n### JS code loaded from source file\n\nSimilar to the above but loads the default export from a local `lib/myCode.js` file.\n\n```turtle\n@prefix code: \u003chttps://code.described.at/\u003e.\n\n\u003curn:example:node\u003e \n  code:implementedBy [\n    a code:EcmaScriptModule ;\n    code:link \u003cfile:lib/myCode.js#default\u003e\n  ] .\n```\n\n```js\nimport rdf from '@zazuko/env'\nimport registry from './registry.js'\nimport dataset from './dataset.js'\n\nconst term = rdf.namedNode('urn:example:node')\nconst implementedBy = rdf.namedNode('https://code.described.at/implementedBy')\n\nconst myCode = registry.load(\n  rdf.clownface({ term, dataset }).out(implementedBy),\n  {\n    basePath: process.cwd() // required to resolve relative paths\n  })\n```\n\nIt is also possible to load named exports similarly to the first example by using hash fragments\nlike `file:lib/myCode#namedExport`.\n\n### JS literal inlined in RDF dataset\n\nA piece of JS code can also be placed directly in the triples. \n\nThis snippet shows how an `Array#filter` callback can be loaded from the dataset.\n\n```turtle\n@prefix code: \u003chttps://code.described.at/\u003e.\n\n\u003curn:example:node\u003e \n  code:implementedBy \"name =\u003e name.startsWith('A')\"^^code:EcmaScript .\n```\n\n```js\nimport rdf from '@zazuko/env'\nimport registry from './registry.js'\nimport dataset from './dataset.js'\n\nconst term = rdf.namedNode('urn:example:node')\nconst implementedBy = rdf.namedNode('https://code.described.at/implementedBy')\n\nconst filterFunc = registry.load(rdf.clownface({ term, dataset }).out(implementedBy))\n```\n\n### JS template string literal\n\nA dedicated datatype can also be used to load a string based on a template literal. The loader will substitute\nthe variables based on an optional parameter.\n\n```turtle\n@prefix code: \u003chttps://code.described.at/\u003e.\n\n\u003curn:example:node\u003e \n  code:implementedBy \"Hello ${name}\"^^code:EcmaScriptTemplateLiteral .\n```\n\nTo fill in the template string, a Map of variable must be passed to the loader.\n\n```js\nimport rdf from '@zazuko/env'\nimport registry from './registry.js'\nimport dataset from './dataset.js'\n\nconst term = rdf.namedNode('urn:example:node')\nconst implementedBy = rdf.namedNode('https://code.described.at/implementedBy')\n\nconst variables = new Map()\nvariables.set('name', 'World')\n\nconst helloString = registry.load(\n  rdf.clownface({ term, dataset }).out(implementedBy),\n  {\n    variables\n  })\n```\n\n### Loading function arguments\n\nThis package also provides an utility loader which can be used to load an array of function arguments.\nUsing this arguments loader it is possible to declare parametrized code within the RDF graph.\n\nTwo flavor for such arguments are possible: positional parameters and parameter map.\n\nThe actual argument values will be recursively loaded using the loader registry itself.\n\nNote that the arguments loader cannot be registered with the registry because selection is based on an\n`rdf:type` which is not available when defining the triples (check the exampels below). For that reason\nthe loader must always be called directly and it is the caller's responsibility to select the correct\nRDF node which contains the `code:arguments` property.\n\n#### Positional parameters\n\nTo load positional parameters the graph, simply define them as an `rdf:List`.\n\n```turtle\n@prefix code: \u003chttps://code.described.at/\u003e.\n\n\u003curn:call:string#startsWith\u003e \n  code:arguments ( \"a\" 5 ).\n```\n\nExecuting the code below against the above triples will return an array containing the values\n`[ \"a\" \"5\" ]`.\n\nThe quotes around 5 is no mistake. The loader currently returns a raw literal value from RDF, disregarding\nits datatype.\n\n\n```js\nimport rdf from '@zazuko/env'\nimport loadArguments from 'rdf-loader-code/arguments.js'\nimport registry from './registry.js'\nimport dataset from './dataset.js'\n\nconst term = rdf.namedNode('urn:call:string#startsWith')\n\nconst argumentsArray = loadArguments(\n  rdf.clownface({ term, dataset }), \n  {\n    loaderRegistry: registry\n  })\n```\n\n#### Named parameters\n\nInstead of relying on the order of parameters, the loader also out-of-the-box supports\nloading of an argument map. Such arguments are declared as name/value pairs.\n\n```turtle\n@prefix code: \u003chttps://code.described.at/\u003e.\n@prefix arg: \u003chttps://code.described.at/argument#\u003e.\n\n\u003curn:call:string#startsWith\u003e \n    code:arguments ([\n      arg:first \"foo\" ;\n      arg:second \"bar\" ;\n    ]) .\n```\n\nExecuting the code below against the above triples will return an object containing the values\n\n```json\n[\n  {\n    \"first\": \"foo\",\n    \"second\": \"bar\"\n  }\n]\n```\n\n#### Mixed arguments\n\nBoth methods can be sed together to represent any kind of function signature.\n\nFor example, to call a function as below\n\n```js\nmyFunction('foo', 42, { bar: {  baz: 'baz' } })\n```\n\nYou would declare the argument list as follows:\n\n```turtle\n@prefix code: \u003chttps://code.described.at/\u003e .\n@prefix arg: \u003chttps://code.described.at/argument#\u003e .\n\n\u003curn:call:myFunction\u003e\n  code:arguments\n    (\n      \"foo\"\n      42\n      [\n        arg:bar\n          [\n            arg:baz \"baz\"\n          ]\n      ]\n    ) .\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzazuko%2Frdf-loader-code","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzazuko%2Frdf-loader-code","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzazuko%2Frdf-loader-code/lists"}