edn-data
is a JavaScript and TypeScript library that provides functionality to generate and parse data in the EDN format, as known from Clojure land.
Map
, Set
and bigint
The number one reason is, your code wants to interact with data coming from Clojure.
But even outside Clojure EDN can be a compelling data format in a world where developers are stuck fighting YAML and complaining about JSON.
EDN has:
Install with:
npm install edn-data
By default parsing returns JSON-compatible data structures that can represent all of the rich EDN types.
There are options to make it easier to parse simpler types.
import { parseEDNString } from 'edn-data'
parseEDNString('{:key "value" :list [1 2 3]}')
// Returns:
{
map: [
[{ key: 'key' }, 'value'],
[{ key: 'list' }, [1, 2, 3]],
],
}
parseEDNString(
'{:key "value" :list [1 2 3]}',
{ mapAs: 'object', keywordAs: 'string' },
)
// Returns:
{
key: 'value',
list: [1, 2, 3],
}
EDN lists can be streamed value by value as standard Node.js Readable streams. This is not available in the browser.
import { parseEDNListStream } from 'edn-data/stream'
const s = parseEDNListStream()
s.write('(1 2 3)')
s.read() // 1
s.read() // 2
s.read() // 3
EDN is generated from plain JSON structures.
With toEDNString
the same data structures parseEDNString
returns can be turned to valid strings, and they represent a rich set of types.
For simple JavaScript types often toEDNStringFromSimpleObject
might be the simpler use.
import { toEDNString, toEDNStringFromSimpleObject } from 'edn-data';
toEDNString({
map: [
[1, { key: 'keyword' }],
[{ set: [1, 2] }, { char: 'a' }],
],
})
// Returns:
'{1 :keyword #{1 2} \a}'
toEDNStringFromSimpleObject({ first: 1, second: 2 })
// Returns:
'{:first 1 :second 2}'
The library is developed driven by its tests.
Verify them using
npm test
For continuous development use
npm run test:watch
Ensure the code formatting with
npm run fix
CI verifies tests and creates npm releases for tags automatically.
version
in the package.json
Release <version>