npm.io
1.0.0-alpha.33 • Published 2 weeks ago

@shexjs/writer

Licence
MIT
Version
1.0.0-alpha.33
Deps
2
Size
87 kB
Vulns
0
Weekly
0
Stars
62

@shexjs/writer

npm version CI

Write ShExJ as ShExC

Install

npm install @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": "ShapeDecl",
          "shapeExpr": {
            "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 the ShExC serialization 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": "ShapeDecl",
          "shapeExpr": {
            "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

The constructor's options object takes a prefixes map for prefixes 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": "ShapeDecl",
          "shapeExpr": {
            "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 precedence rules. For example, a ()'d EachOf expression:

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

will be serialized without the ()s:

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

@shexjs/writer is one of the shex.js packages; installing shex pulls in the whole suite, and its README maps them.

Keywords