jsedn
Version:
js implementation of edn
364 lines (298 loc) • 8.73 kB
JavaScript
// Generated by CoffeeScript 1.6.1
(function() {
var Iterable, List, Map, Pair, Prim, Set, Vector, encode, equals, type,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
type = require("./type");
equals = require("equals");
Prim = require("./atoms").Prim;
encode = require("./encode").encode;
Iterable = (function(_super) {
__extends(Iterable, _super);
function Iterable() {
return Iterable.__super__.constructor.apply(this, arguments);
}
Iterable.prototype.hashId = function() {
return this.ednEncode();
};
Iterable.prototype.ednEncode = function() {
return (this.map(function(i) {
return encode(i);
})).val.join(" ");
};
Iterable.prototype.jsonEncode = function() {
return this.map(function(i) {
if (i.jsonEncode != null) {
return i.jsonEncode();
} else {
return i;
}
});
};
Iterable.prototype.jsEncode = function() {
return (this.map(function(i) {
if ((i != null ? i.jsEncode : void 0) != null) {
return i.jsEncode();
} else {
return i;
}
})).val;
};
Iterable.prototype.exists = function(index) {
return this.val[index] != null;
};
Iterable.prototype.each = function(iter) {
var i, _i, _len, _ref, _results;
_ref = this.val;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
i = _ref[_i];
_results.push(iter(i));
}
return _results;
};
Iterable.prototype.map = function(iter) {
return this.each(iter);
};
Iterable.prototype.walk = function(iter) {
return this.map(function(i) {
if ((i.walk != null) && type(i.walk) === "function") {
return i.walk(iter);
} else {
return iter(i);
}
});
};
Iterable.prototype.at = function(index) {
if (this.exists(index)) {
return this.val[index];
}
};
Iterable.prototype.set = function(index, val) {
this.val[index] = val;
return this;
};
return Iterable;
})(Prim);
List = (function(_super) {
__extends(List, _super);
function List() {
return List.__super__.constructor.apply(this, arguments);
}
List.prototype.ednEncode = function() {
return "(" + (List.__super__.ednEncode.call(this)) + ")";
};
List.prototype.jsonEncode = function() {
return {
List: List.__super__.jsonEncode.call(this)
};
};
List.prototype.map = function(iter) {
return new List(this.each(iter));
};
return List;
})(Iterable);
Vector = (function(_super) {
__extends(Vector, _super);
function Vector() {
return Vector.__super__.constructor.apply(this, arguments);
}
Vector.prototype.ednEncode = function() {
return "[" + (Vector.__super__.ednEncode.call(this)) + "]";
};
Vector.prototype.jsonEncode = function() {
return {
Vector: Vector.__super__.jsonEncode.call(this)
};
};
Vector.prototype.map = function(iter) {
return new Vector(this.each(iter));
};
return Vector;
})(Iterable);
Set = (function(_super) {
__extends(Set, _super);
Set.prototype.ednEncode = function() {
return "\#{" + (Set.__super__.ednEncode.call(this)) + "}";
};
Set.prototype.jsonEncode = function() {
return {
Set: Set.__super__.jsonEncode.call(this)
};
};
function Set(val) {
var item, _i, _len;
Set.__super__.constructor.call(this);
this.val = [];
for (_i = 0, _len = val.length; _i < _len; _i++) {
item = val[_i];
if (__indexOf.call(this.val, item) >= 0) {
throw "set not distinct";
} else {
this.val.push(item);
}
}
}
Set.prototype.map = function(iter) {
return new Set(this.each(iter));
};
return Set;
})(Iterable);
Pair = (function() {
function Pair(key, val) {
this.key = key;
this.val = val;
}
return Pair;
})();
Map = (function() {
Map.prototype.hashId = function() {
return this.ednEncode();
};
Map.prototype.ednEncode = function() {
var i;
return "{" + (((function() {
var _i, _len, _ref, _results;
_ref = this.value();
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
i = _ref[_i];
_results.push(encode(i));
}
return _results;
}).call(this)).join(" ")) + "}";
};
Map.prototype.jsonEncode = function() {
var i;
return {
Map: (function() {
var _i, _len, _ref, _results;
_ref = this.value();
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
i = _ref[_i];
_results.push(i.jsonEncode != null ? i.jsonEncode() : i);
}
return _results;
}).call(this)
};
};
Map.prototype.jsEncode = function() {
var hashId, i, k, result, _i, _len, _ref, _ref1;
result = {};
_ref = this.keys;
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
k = _ref[i];
hashId = (k != null ? k.hashId : void 0) != null ? k.hashId() : k;
result[hashId] = ((_ref1 = this.vals[i]) != null ? _ref1.jsEncode : void 0) != null ? this.vals[i].jsEncode() : this.vals[i];
}
return result;
};
function Map(val) {
var i, v, _i, _len, _ref;
this.val = val != null ? val : [];
if (this.val.length && this.val.length % 2 !== 0) {
throw "Map accepts an array with an even number of items. You provided " + this.val.length + " items";
}
this.keys = [];
this.vals = [];
_ref = this.val;
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
v = _ref[i];
if (i % 2 === 0) {
this.keys.push(v);
} else {
this.vals.push(v);
}
}
this.val = false;
}
Map.prototype.value = function() {
var i, result, v, _i, _len, _ref;
result = [];
_ref = this.keys;
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
v = _ref[i];
result.push(v);
if (this.vals[i] !== void 0) {
result.push(this.vals[i]);
}
}
return result;
};
Map.prototype.indexOf = function(key) {
var i, k, _i, _len, _ref;
_ref = this.keys;
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
k = _ref[i];
if (equals(k, key)) {
return i;
}
}
return void 0;
};
Map.prototype.exists = function(key) {
return this.indexOf(key) != null;
};
Map.prototype.at = function(key) {
var id;
if ((id = this.indexOf(key)) != null) {
return this.vals[id];
} else {
throw "key does not exist";
}
};
Map.prototype.set = function(key, val) {
var id;
if ((id = this.indexOf(key)) != null) {
this.vals[id] = val;
} else {
this.keys.push(key);
this.vals.push(val);
}
return this;
};
Map.prototype.each = function(iter) {
var k, _i, _len, _ref, _results;
_ref = this.keys;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
k = _ref[_i];
_results.push(iter(this.at(k), k));
}
return _results;
};
Map.prototype.map = function(iter) {
var result;
result = new Map;
this.each(function(v, k) {
var nv, _ref;
nv = iter(v, k);
if (nv instanceof Pair) {
_ref = [nv.key, nv.val], k = _ref[0], nv = _ref[1];
}
return result.set(k, nv);
});
return result;
};
Map.prototype.walk = function(iter) {
return this.map(function(v, k) {
if (type(v.walk) === "function") {
return iter(v.walk(iter), k);
} else {
return iter(v, k);
}
});
};
return Map;
})();
module.exports = {
Iterable: Iterable,
List: List,
Vector: Vector,
Set: Set,
Pair: Pair,
Map: Map
};
}).call(this);