# @shexjs/writer

> Shape Expressions - convert ShExJ to ShExC.

Latest version **1.0.0-alpha.33** (published 2026-09-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @shexjs/writer
pnpm add @shexjs/writer
yarn add @shexjs/writer
bun add @shexjs/writer
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.0.0-alpha.33 |
| Published | 2026-09-14 |
| First published | 2021-06-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18 |
| Dependencies | 2 |
| Unpacked size | 86.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 62 |
| Author | Eric Prud'hommeaux |
| Maintainers | ericprud, justinwb |
| Keywords | shex, shape expressions, rdf, writer |

## Links

- npm: https://www.npmjs.com/package/@shexjs/writer
- Repository: https://github.com/shexjs/shex.js
- Homepage: https://github.com/shexjs/shex.js#readme
- Issues: https://github.com/shexSpec/grammar/issues
- npm.io page: https://npm.io/package/@shexjs/writer

## Dependencies (2)

- [@types/shexj](https://npm.io/package/@types/shexj.md) ^2.1.7
- [relativize-url](https://npm.io/package/relativize-url.md) ^0.1.0

## Alternatives

- [postcss-color-hex-alpha](https://npm.io/package/postcss-color-hex-alpha.md) — 6.4M weekly downloads
- [randomcolor](https://npm.io/package/randomcolor.md) — 348.0K weekly downloads
- [bows](https://npm.io/package/bows.md) — 1.3K weekly downloads
- [ep_prefer_color_scheme](https://npm.io/package/ep_prefer_color_scheme.md) — 260 weekly downloads
- [coc-yank](https://npm.io/package/coc-yank.md) — 61 weekly downloads

## Recent versions

- 1.0.0-alpha.33 (latest) — 2026-09-14
- 1.0.0-alpha.31 — 2026-09-06
- 1.0.0-alpha.30 — 2026-08-31
- 1.0.0-alpha.27 — 2023-10-23
- 1.0.0-alpha.25 — 2023-03-21
- 1.0.0-alpha.24 — 2022-09-16
- 1.0.0-alpha.22 — 2022-08-18
- 1.0.0-alpha.19 — 2021-12-22
- 1.0.0-alpha.15 — 2021-07-18
- 1.0.0-alpha.12 — 2021-06-03

## README

# @shexjs/writer

[![npm version](https://img.shields.io/npm/v/@shexjs/writer)](https://www.npmjs.com/package/@shexjs/writer)
[![CI](https://github.com/shexjs/shex.js/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/shexjs/shex.js/actions/workflows/ci.yml)

Write [ShExJ](https://shex.io/shex-semantics/#shexj) as [ShExC](https://shex.io/shex-semantics/#shexc)

## Install

``` shell
npm install @shexjs/writer
```

## Quick start
Invoke from the command line:
``` sh
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:
``` shex
<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](https://developer.mozilla.org/en-US/docs/Web/API/URL)) allows you to parse schemas with relative URLs for e.g. shape and property names:
``` sh
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);
    })'
```
``` shex
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:
``` sh
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);
    })'
```
``` shex
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:

``` shex
<S1> {
(    <p1> . ;
     <p2> @<S2>?)
}
```
will be serialized without the ()s:
``` shex
<S1> {
    <p1> . ;
    <p2> @<S2>?
}
```

---

`@shexjs/writer` is one of the [shex.js](https://github.com/shexjs/shex.js#readme) packages; installing [`shex`](https://www.npmjs.com/package/shex) pulls in the whole suite, and [its README](https://github.com/shexjs/shex.js/tree/main/packages/shex#the-shexjs-packages) maps them.

---
_Source: https://npm.io/package/@shexjs/writer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
