Skip to content

Commit

Permalink
encoder: fixed block mode
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 committed Sep 14, 2014
1 parent 8171853 commit 96574de
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ var Decoder = require('./decoder');
var Map = require('./map');

function encoder() {
BLOCK = 1;

/**
* Returns the NEON representation of a value.
* @param xvar mixed
Expand Down Expand Up @@ -57,15 +55,15 @@ function encoder() {
}
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"))
s += (isList ? '-' : this.encode(k) + "" + ':')
+ "" + (v.indexOf("\n") === -1 ? (' ' + "" + v) : "\n\t" + "" + v.replace("\n", "\n\t"))
+ "" + "\n";
} else {
s += (isList ? '' : this.encode(k) + "" + ': ') + "" + this.encode(v) + "" + ', ';
}
}
if (options & encoder.BLOCK) {
return s;
return s.trim() + "\n";
} else {
return (isList ? '[' : '{') + "" + s.substr(0, s.length - 2) + "" + (isList ? ']' : '}');
}
Expand All @@ -82,5 +80,6 @@ function encoder() {


}
encoder.BLOCK = 1;

module.exports = encoder;
1 change: 1 addition & 0 deletions src/neon.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ module.exports.decode = function (input) {
module.exports.Entity = require('./entity');
module.exports.Map = require('./map');
module.exports.CHAIN = DecoderClass.CHAIN;
module.exports.BLOCK = EncoderClass.BLOCK;
22 changes: 22 additions & 0 deletions test/Encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,26 @@ suite('Encoder', function () {
neon.encode(entity)
);
});

test('block', function () {
assert.equal("- foo\n" +
"- bar\n", neon.encode(["foo", "bar"], neon.BLOCK));
});
test('block 2', function () {
assert.equal("x: foo\n" +
"y: bar\n", neon.encode({x: "foo", y: "bar"}, neon.BLOCK));
});
test('block 3', function () {
assert.equal("x: foo\n" +
"y:\n" +
" - 1\n" +
" - 2\n", neon.encode({x: "foo", y: [1,2]}, neon.BLOCK));
});
test('block 5', function () {
assert.equal("x: foo\n" +
"y:\n" +
" - 1\n" +
" - 2\n", neon.encode({x: "foo", y: [1, 2]}, neon.BLOCK));
});

});

0 comments on commit 96574de

Please sign in to comment.