@shexjs/writer
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-alpha.27 • Public • Published

NPM Version ShapeExpressions Gitter chat https://gitter.im/shapeExpressions/Lobby DOI

@shexjs/writer

Write ShExJ as ShExC

install

npm install --save @shexjs/writer

Quick Start

Invoke from the command line:

node -e 'new (require("@shexjs/writer"))()
  .writeSchema(
    {
      "type": "Schema",
      "shapes": [
        {
          "id": "http://a.example/S1",
          "type": "Shape",
          "expression": {
            "type": "TripleConstraint",
            "predicate": "http://a.example/p1",
            "valueExpr": {
              "type": "NodeConstraint",
              "values": [
                {
                  "value": "1",
                  "type": "http://www.w3.org/2001/XMLSchema#integer"
                },
                {
                  "value": "2",
                  "type": "http://www.w3.org/2001/XMLSchema#integer"
                }
              ]
            }
          }
        }
      ]
    },
    (error, text, prefixes) => {
      if (error)
        throw error;
      console.log(text);
    })'

The result is a ShExJ expression of the input schema:

<http://a.example/S1> {
  <http://a.example/p1> [1 2]
}

option: base - Base IRI for terse relative URLs

Providing a Base IRI (see MDN docs for URL) allows you to parse schemas with relative URLs for e.g. shape and property names:

node -e 'new (require("@shexjs/writer"))({
    base: "http://a.example/"
  })
  .writeSchema(
    {
      "type": "Schema",
      "shapes": [
        {
          "id": "http://a.example/S1",
          "type": "Shape",
          "expression": {
            "type": "TripleConstraint",
            "predicate": "http://a.example/p1",
            "valueExpr": {
              "type": "NodeConstraint",
              "values": [
                {
                  "value": "1",
                  "type": "http://www.w3.org/2001/XMLSchema#integer"
                },
                {
                  "value": "2",
                  "type": "http://www.w3.org/2001/XMLSchema#integer"
                }
              ]
            }
          }
        }
      ]
    },
    (error, text, prefixes) => {
      if (error)
        throw error;
      console.log(text);
    })'
BASE <http://a.example/>
<S1> {
  <p1> [1 2]
}

option: prefixes - Pre-loaded namespace prefixes

A second parameter to construct is a map for prefixes that are not defined in the schema:

node -e 'new (require("@shexjs/writer"))({
    base: "http://a.example/",
    prefixes: {
      "": "http://a.example/path/path3#",
      v: "http://a.example/vocab#"
    }
  })
  .writeSchema(
    {
      "type": "Schema",
      "shapes": [
        {
          "id": "http://a.example/path/S1",
          "type": "Shape",
          "expression": {
            "type": "TripleConstraint",
            "predicate": "http://a.example/path/path3#p1",
            "valueExpr": {
              "type": "NodeConstraint",
              "values": [
                "http://a.example/vocab#v1",
                "http://a.example/vocab#v2"
              ]
            }
          }
        }
      ]
    },
    (error, text, prefixes) => {
      if (error)
        throw error;
      console.log(text);
    })'
PREFIX : <http://a.example/path/path3#>
PREFIX v: <http://a.example/vocab#>

BASE <http://a.example/>
<path/S1> {
  :p1 [v:v1 v:v2]
}

option: simplifyParentheses

simplifyParentheses: true eliminates ()s to the degree possible using the ShExC parsing precedunce rules. For example, a ()'d EachOf expression:

<S1> {
(    <p1> . ;
     <p2> @<S2>?)
}

will be serialized without the ()s:

<S1> {
    <p1> . ;
    <p2> @<S2>?
}

Lerna Monorepo

This repo uses lerna to manage multiple NPM packages. These packages are located in packages/*:

Package Sidebar

Install

npm i @shexjs/writer

Weekly Downloads

226

Version

1.0.0-alpha.27

License

MIT

Unpacked Size

32.9 kB

Total Files

5

Last publish

Collaborators

  • ericprud
  • justinwb