Skip to content

Commit

Permalink
generate()
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Nov 6, 2012
1 parent 6bfe907 commit b2b01a9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/cons.js
Expand Up @@ -12,7 +12,7 @@ Cons.list = function(array) {
};

Cons.prototype.toString = function() {
return "(" + this.car + " . " . this.cdr + ")";
return "(" + this.car + " . " + this.cdr + ")";
};

Cons.prototype.forEach = function(callback) {
Expand Down
51 changes: 34 additions & 17 deletions lib/generate.js
Expand Up @@ -2,31 +2,48 @@ var util = require('util')
, Symbol = require('./symbol')
, Cons = require('./cons');

var generate = function(obj) {
switch (typeof obj) {
case 'string':
return '"' + obj + '"';
var generate = function(obj, options) {
var result;

case 'boolean':
return obj ? 't' : 'nil';
options = options || { object: 'alist' };

case 'object':
switch (typeof obj) {
case 'string':
result = '"' + obj + '"';
break;
case 'boolean':
result = obj ? 't' : 'nil';
break;
case 'object':
if (obj === null) {
return 'nil';
} else if (obj instanceof Symbol || obj instanceof Cons) {
return obj.toString();
result = 'nil';
} else if (util.isArray(obj)) {
if (obj.length) {
result = '(' + obj.map(function(v) { return generate(v) }).join(' ') + ')';
} else {
result = 'nil';
}
} else if (obj instanceof Symbol) {
result = obj.toString();
} else if ('car' in obj && 'cdr' in obj) {
return "(" + obj.car + " . " + obj.cdr + ")";
result = '(' + generate(obj.car) + ' . ' + generate(obj.cdr) + ')';
} else {
return undefined;
result = [];
for (var key in obj) {
result.push(generate(new Cons(key, obj[key])));
}
result = '(' + result.join(' ') + ')';
}

case 'function':
break;
case 'function':
throw new Error('Function object cannt convert sexpression');

default:
return obj.toString();
break;
default:
result = obj.toString();
break;
}

return result;
};

module.exports = generate;
30 changes: 27 additions & 3 deletions test/generate.test.js
@@ -1,6 +1,7 @@
var expect = require('expect.js')
var expect = require('expect.js')
, sexpression = require('../')
, generate = sexpression.generate;
, generate = sexpression.generate
, intern = sexpression.Symbol.intern;

describe('generate()', function() {
describe('number', function() {
Expand Down Expand Up @@ -43,12 +44,35 @@ describe('generate()', function() {
});

it('should to be symbol name', function() {
var intern = sexpression.Symbol.intern;
expect(generate(intern('a'))).to.be(intern('a').toString());
});

it('should to be cons cell', function() {
expect(generate({ car: 1, cdr: 2 })).to.be('(1 . 2)');
});

it('should to be null from empty', function() {
expect(generate([])).to.be('nil');
});

it('should be number list from number array', function() {
expect(generate([1, 2, 3])).to.be('(1 2 3)');
});

it('should be nested list', function() {
expect(generate([1, [2, 3], 4, 5])).to.be('(1 (2 3) 4 5)');
});

it('should be string list from string array', function() {
expect(generate(["a", "b", "c"])).to.be('("a" "b" "c")');
});

it('should be mixed list from mixed array', function() {
expect(generate([1, "abcde", intern('hoge')])).to.be('(1 "abcde" hoge)');
});

it('should be alist from object', function() {
expect(generate({ a: 1, b: 2 })).to.be('(("a" . 1) ("b" . 2))');
});
});
});

0 comments on commit b2b01a9

Please sign in to comment.