README
jsedn
A javascript implementation of edn. To see it in action checkout the edn playground. If you open your browser console you will have access to a global jsedn object with which you can try things beside JSON encoding. I recommend jsedn.unify("[?x was always better than ?y]", {x: "sonic", y: "mario"}).jsEncode()
.
Getting Started
Install
npm install jsedn
Use in a web page directly
Use jsedn.js
, which is a standalone version that will provide a global "jsedn".
Code
var edn = require("jsedn");
var map = edn.parse("{:a 5 [1 2] {:name :mike :age 40}}");
console.log(map.at(new edn.Vector([1, 2])).at(edn.kw(":name")));
Will output "mike"
.
Now the other way:
edn.encode({a: 1, "id": 333});
Will output:
{"a" 1 "id" 333}
Finally lets encode js into edn then back to js:
edn.parse(edn.encode({
a: 1,
b: {
age: 30,
feeling: ["electric", "pink"]
}
})).at("b").at("feeling").at(0)
Will output "electric"
. Definitely working in both directions.
Command Line
If you have installed via npm
you will have a jsedn script that accepts input via pipe/stdin. Currently takes:
-s
flag for "select" which you pass a path separated by space-j
encodes input as JSON-p
indicates pretty print for json output
> echo "{:a first-item :b [{:name :walter :age 50 :kids [:A :B :C]}]}" | jsedn -s ":b 0 :kids 2"
outputs: :b 0 :kids 2 => :C
Testing
I have developed this in a very test driven manner e.g. each test written before the next feature is implemented. Baring that in mind it means any bugs you find it would be awesome if you could edit the tests adding one which clearly indicates the bug/feature request.
npm install
git submodule update --init
npm test
API
parse (ednString)
Will turn a valid edn string into a js object structure based upon the classes details below.
edn.parse("{:a-keyword! [1 2 -3.4]}");
encode (jsObj)
Will encode both native JS and any object providing a ednEncode method.
edn.encode({"a-keyword!": [1,2,-3.4]});
setTagAction (tag, action)
Will add the tag action to be performed on any data prepended by said tag.
edn.setTagAction(new edn.Tag('myApp', 'tagName'), function(obj) {
//do stuff with obj here and then return it
var mutatedObj = thingsHandlerDoes(obj);
return mutatedObj;
});
setTokenPattern (tokenName, pattern)
If for some reason you would like to over-ride or add a new token pattern.
edn.setTokenPattern()))
setTokenAction (tokenName, actionCallback)
Allows for customization of token handling upon match. For instance if you decided you would prefer nil to be represented by undefined instead of null (default).
edn.setTokenAction('nil', function(token) { return undefined;});
setTypeClass (type, class)
This is useful if you want to over-ride the naive implementations of Map etc.
edn.setTypeClass('List', MyListClass));
atPath (obj, path)
Simple way to lookup a value in elements returned from parse.
var parsed = edn.parse("[[{:name :frank :kids [{:eye-color :red} {:eye-color :blue}]}]]");
edn.atPath(parsed, "0 0 :kids 1 :eye-color");
path is a space separated string which consists of index (remember Array/Set/Vector are all 0 indexed) and key locations.
encodeJson
Provides a json encoding including type information e.g. Vector, List, Set etc.
console.log(edn.encodeJson(edn.parse("[1 2 3 {:x 5 :y 6}]")));
yields:
{"Vector":[1,2,3,{"Map":[{"Keyword": ":x"},5,{"Keyword":":y"},6]}]}
toJS
Attempts to return a "plain" js object. Bear in mind this will yield poor results if you have any Map objects which utilize composite objects as keys. If an object has a hashId method it will use that when building the js dict.
var jsobj = edn.toJS(edn.parse("[1 2 {:name {:first :ray :last :cappo}}]"));
yields:
[1, 2, {":name": {":first": ":ray", ":last": ":cappo"}}]
Notice that you can not always go back the other direction. In the example above if you were to edn.parse it you would end up with:
[1 2 {":name" {":first" ":ray" ":last" ":cappo"}}]
In which you have strings for keys instead of keywords. At one point I would "infer" that if a string started with a ":" it would be treated as a keyword. This caused more problems than it resolved which brings us to our next methods.
kw (string)
Interns a valid keyword into an edn.Keyword
object e.g:
edn.kw(":myns/kw")
sym (string)
Interns a valid symbol into an edn.Symbol
object e.g:
edn.sym("?name")
unify (data, values, [tokenStart])
Unifies the first form with the second. Useful for populating a "data template". It accepts either edn objects or strings as arguments.
edn.unify("{?key1 ?key1-val ?key2 ?key2-val :all [?key1-val ?key2-val]}", "{key1 :x key1-val 200 key2 :y key2-val 300}");
yields:
{:x 200 :y 300 :all [200 300]}
A third argument is expected which can be used to indicate the "tokenStart" first character for unify tokens. This defaults to "?".
An example with Map as data and js obj as values and changing the tokenStart to $
edn.unify(new edn.Map([edn.kw(":place"), edn.sym("$place")]), {place: "salt lake city"}, "