didjs
Version:
Decentralized Identifiers parser for javascript
90 lines (67 loc) • 1.7 kB
Markdown
is a javascript package that parses and stringifies
[ ](https://w3c-ccg.github.io/did-spec).
```
yarn add didjs
```
or
```html
<script type="text/javascript" src="./dist/didjs.min.js"></script>
```
```js
import * as didjs from 'didjs'
const d = didjs.parse('did:example:1234567890/asdf/qwerty')
console.log(d)
```
The above example parses the input string according to the rules defined in the [DID Grammar](did.abnf) and prints the following object:
```js
{
id: "1234567890",
idStrings: ["1234567890"],
method: "example",
path: "asdf/qwerty",
pathSegments: ["asdf", "qwerty"]
}
```
The input string may also be a [DID Reference](https://w3c-ccg.github.io/did-spec/#dfn-did-reference) with a
[ ](https://w3c-ccg.github.io/did-spec/#dfn-did-fragment):
```js
const d = didjs.parse("did:example:1234567890#keys-1")
console.log(d.fragment)
// Output: keys-1
```
This package also stringifies DID objects into valid DID strings:
```js
const d = {
method: 'example',
id: '1234567890'
}
console.log(didjs.stringify(d))
// Output: did:example:1234567890
```
or with a refence with a fragment:
```js
const d = {
method: 'example',
id: '1234567890',
fragment: 'keys-1'
}
console.log(didjs.stringify(d))
// Output: did:example:1234567890#keys-1
```
To compile the code in this repository, run:
```
webpack
```
This repository includes a [suite of tests](test.js) that check for various edge cases within
the [DID Grammar](did.abnf).
To run the tests, run:
```
npm run test
```
This package is licensed under [Apache License 2.0](LICENSE).
`didjs`