Skip to content

Commit

Permalink
working encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 committed Sep 14, 2014
1 parent 6e9f8e6 commit 8171853
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 52 deletions.
84 changes: 39 additions & 45 deletions src/encoder.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
var Entity = require('./entity');
var Decoder = require('./decoder');
var Map = require('./map');

function encoder() {
BLOCK = 1;

Expand All @@ -12,75 +16,65 @@ function encoder() {
options = null;
}

if (xvar instanceof DateTime) {
if (xvar instanceof Date) {
return xvar.format('Y-m-d H:i:s O');

} else if (xvar instanceof Neon.Entity) {
} else if (xvar instanceof Entity) {
var attrs = '';
if (xval.attributes instanceof Array) {
attrs = this.encode(xval.attributes);
if (xvar.attributes !== null && typeof xvar.attributes === "object") {
attrs = this.encode(xvar.attributes);
attrs = attrs.substr(1, attrs.length - 2);
}
return this.encode(xvar.value) + "" + '(' + "" + attrs + "" + ')';
}

if (typeof xvar == 'object') {
var obj = xvar;
xvar = {};
for (var k in obj) {
var v = obj[k];
xvar[k] = v;
}
}

if (xvar instanceof Array) {
var isList = !xvar || (function (arr) {
var i = 0;
if (xvar !== null && typeof xvar == 'object') {
var isMap = xvar instanceof Map;
var isList = (xvar instanceof Array && (function (arr) {
var length = 0;
for (var i in arr) {
length++;
}
return length === arr.length;
})(xvar)) || (isMap && xvar.isList());
var s = '';
xvar = isMap ? xvar.items() : xvar;
if ((function (arr) {
for (key in arr) {
if (key != i) {
return false;
}
i++;
return false;
}
return true;
})(xvar);
var s = '';
if (options & encoder.BLOCK) {
if ((function (arr) {
for (key in arr) {
return false;
}
return true;
})(xvar)) {
return '[]';
})(xvar)) {
return '[]';
}
for (var i in xvar) {
if (isMap) {
var k = xvar[i].key;
var v = xvar[i].value;
} else {
var k = xvar instanceof Array ? parseInt(i) : i;
var v = xvar[i];
}
for (var k in xvar) {
var v = xvar[k];
if (options & encoder.BLOCK) {
v = this.encode(v, encoder.BLOCK);
s += (isList ? '-' : encoder.encode(k) + "" + ':')
+ "" + (v.indexOf("\n") === false ? ' ' + "" + v : "\n\t" + "" + v.replace("\n", "\n\t"))
+ "" + "\n";
} else {
s += (isList ? '' : this.encode(k) + "" + ': ') + "" + this.encode(v) + "" + ', ';
}
}
if (options & encoder.BLOCK) {
return s;

} else {
for (var k in xvar) {
var v = xvar[k];
s += (isList ? '' : this.encode(k) + "" + ': ') + "" + this.encode(v) + "" + ', ';
}
return (isList ? '[' : '{') + "" + s.substr(0, s.length - 2) + "" + (isList ? ']' : '}');
}

} else if (typeof xvar == "string" && isNaN(xvar)
&& !preg_match('~[\x00-\x1F]|^\d{4}|^(true|false|yes|no|on|off|null)\z~i', xvar)
&& preg_match('~^' + "" + this.patterns[1] + "" + '\z~x', xvar) // 1 = literals
&& !xvar.match(/[\x00-\x1F]|^\d{4}|^(true|false|yes|no|on|off|null)$/i)
&& (new RegExp("^" + Decoder.patterns[1] + "$")).exec(xvar) // 1 = literals
) {
return xvar;

} else if (is_float(xvar)) {
xvar = JSON.stringify(xvar);
return xvar.indexOf('.') === -1 ? xvar + "" + '.0' : xvar;

} else {
return JSON.stringify(xvar);
}
Expand All @@ -89,4 +83,4 @@ function encoder() {

}

module.exports.encoder = encoder;
module.exports = encoder;
34 changes: 27 additions & 7 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ function Map() {
} else {
var number = parseInt(key * 1);
if (!isNaN(number) && number > this._key) {
this._key = number;
this._key = number + 1;
}
}
if (typeof this._indexes[key] === "undefined") {
this._items[this.length] = {_key: key, value: value};
this._items[this.length] = {key: key, value: value};
this._indexes[key] = this.length;
this.length++;
return 1;
Expand All @@ -42,8 +42,8 @@ function Map() {
return this._items[this._indexes[key]].value;
};

this.last = function() {
if(this.length === 0) {
this.last = function () {
if (this.length === 0) {
throw new Error("Map has no items");
}
return this._items[this.length - 1];
Expand All @@ -69,7 +69,7 @@ function Map() {
this.keys = function () {
var keys = [];
for (var i in this._items) {
keys.push(this._items[i]._key);
keys.push(this._items[i].key);
}
return keys;
};
Expand All @@ -89,18 +89,30 @@ function Map() {
var result = {};
for (var i in this._items) {
var value = this._items[i].value;
result[this._items[i]._key] = value instanceof Map && deep ? value.toObject(true) : value;
result[this._items[i].key] = value instanceof Map && deep ? value.toObject(true) : value;
}

return result;
};

this.forEach = function (callable) {
for (var i in this._items) {
callable(this._items[i]._key, this._items[i].value);
callable(this._items[i].key, this._items[i].value);
}
};

this.isList = function () {
return (function(items) {
var cmp = 0;
for(var i in items) {
if(cmp !== items[i].key) {
return false;
}
cmp = items[i].key + 1;
}
return true;
})(this.items());
};

}
Map.fromObject = function (obj) {
Expand All @@ -111,4 +123,12 @@ Map.fromObject = function (obj) {
return mapInst;
};

Map.fromArray = function (obj) {
var mapInst = new Map;
for (var i in obj) {
mapInst.set(null, obj[i]);
}
return mapInst;
};

module.exports = Map;
103 changes: 103 additions & 0 deletions test/Encoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
var assert = require('assert');
var neon = require('../src/neon');

suite('Encoder', function () {

test('true, false, null constants and strings', function () {
assert.equal('[true, "TRUE", "tRuE", "true", false, "FALSE", "fAlSe", "false", null, "NULL", "nUlL", "null", "yes", "no", "on", "off"]',
neon.encode([
true, 'TRUE', 'tRuE', 'true',
false, 'FALSE', 'fAlSe', 'false',
null, 'NULL', 'nUlL', 'null',
'yes', 'no', 'on', 'off'
]));
});
test('numbers', function () {
assert.equal('[1, 1, 0, 0, -1, -1.2, "1", "1.0", "-1"]',
neon.encode([1, 1.0, 0, 0.0, -1, -1.2, '1', '1.0', '-1'])
);
});
test('symbols', function () {
assert.equal('["[", "]", "{", "}", ":", ": ", "=", "#"]',
neon.encode(['[', ']', '{', '}', ':', ': ', '=', '#'])
);
});
test('list', function () {
assert.equal('[1, 2, 3]',
neon.encode([1, 2, 3])
);
});
test('object', function () {
assert.equal('{"1": 1, "2": 2, "3": 3}',
neon.encode({1: 1, 2: 2, 3: 3})
);
});

test('list with missing key', function () {
var arr = [];
arr[1] = 1;
arr[2] = 2;
arr[3] = 3;
assert.equal('{1: 1, 2: 2, 3: 3}',
neon.encode(arr)
);
});

test('object and array', function () {
assert.equal('{foo: 1, bar: [2, 3]}',
neon.encode({foo: 1, bar: [2, 3]})
);
});

test('map with list', function () {
assert.equal('[1, 2, 3]',
neon.encode(neon.Map.fromArray([1, 2, 3]))
);
});
test('map with assoc array', function () {
assert.equal('{"1": 1, foo: 2}',
neon.encode(neon.Map.fromObject({1: 1, "foo": 2}))
);
});
test('map', function () {
var map = new neon.Map;
map.set(1, 1);
map.set("foo", 2);
assert.equal('{1: 1, foo: 2}', neon.encode(map));
});

test('entity 1', function () {
assert.equal('item(a, b)',
neon.encode(neon.decode('item(a, b)'))
);
});
test('entity 2', function () {
assert.equal('item<item>(a, b)',
neon.encode(neon.decode('item<item>(a, b)'))
);
});
test('entity 3', function () {
assert.equal('item(foo: a, bar: b)',
neon.encode(neon.decode('item(foo: a, bar: b)'))
);
});

test('entity 4', function () {
assert.equal('[]()',
neon.encode(neon.decode('[]()'))
);
});
test('entity 5', function () {
assert.equal('item(0: a, foo: b)',
neon.encode(neon.decode('item(a, foo: b)'))
);
});

test('entity 6', function () {
var entity = new neon.Entity("ent");
entity.attributes = null;
assert.equal('ent()',
neon.encode(entity)
);
});
});

0 comments on commit 8171853

Please sign in to comment.