Skip to content

prepor/edn-parser-js

Repository files navigation

edn-parser-js

npm package Build Status

EDN parser library for JS/TS.

Rationaly

There are a number of existing EDN parsers for JS/TS. But I'm not aware of one that supports all EDN features that modern Clojure could emit/parse. First of all, it's namespaced maps introduced in Clojure 1.9

This parser is trying to support everything that Clojure could emit and parse using clojure.edn, even if it's not in EDN "spec". It includes namespaced maps, | in symbols/keywords, metadata parsing.

Types mapping

EDN Type TS Type EDN Value JS Value
integer/float number 123 123
nil null nil null
boolean boolean true true
string string "hello" "hello!"
char { char: string } \a { char: "a" }
symbol { symbol: string; ns?: string } foo { symbol: "foo" }
bar/foo { symbol: "foo", ns: "bar" }
keyword { keyword: string; ns?: string } :foo { keyword: "foo" }
:bar/foo { keyword: "foo", ns: "bar" }
vector { list: EDN[] } [1 2 3] { list: [1, 2, 3] }
map { map: [EDN, EDN][] } {"hello" "world} { map: [["hello", "world"]]}
set { set: EDN[] } #{1 2 3} { set: [1, 2, 3] }
list { list: EDN[] } (1 2 3) { list: [1, 2 ,3] }
^meta ... { meta: [EDN, EDN][]; value: EDN } ^{"comment": "hi"} [] { meta: {"comment": "hi"}, value: List()}

Install

npm install edn-parser-js

Usage

import { ednParse } from 'edn-parser-js';

ednParse('{:hello "world"}');
//=> Map(1) { { keyword: 'hello' } => 'world' }

API

export type EDNSymbol = { symbol: string; ns?: string };

export type EDN =
  | number
  | null
  | boolean
  | string
  | EDNSymbol
  | { keyword: string; ns?: string }
  | { char: string }
  | EDN[]
  | { map: [EDN, EDN][] }
  | { set: EDN[] }
  | { list: EDN[] }
  | { tag: EDNSymbol; value: EDN }
  | { meta: [EDN, EDN][]; value: EDN };

ednParse(string): EDN

ednParseMulti(string): EDN[]