Skip to content

Commit

Permalink
object stringify default to keylist
Browse files Browse the repository at this point in the history
  • Loading branch information
fkubotar committed Nov 7, 2012
1 parent 5b78c83 commit fada0c6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -30,11 +30,11 @@ sexpression.stringify([{ name: 'a' }, { name: 'b' }, [1, 2], 'a']) //=> '(a b (1
sexpression.stringify({ car: 1, cdr: 2}) //=> '(1 . 2)'
sexpression.stringify([{ car: { name: 'hoge'}, cdr: 1}, { car: { name: 'fuga' }, cdr: 2 }]) //=> '((hoge . 1) (fuga . 2))'

sexpression.stringify({ a: 1, b: 2}) //=> '(("a" . 1) ("b" . 2))'
sexpression.stringify([1, { hoge: 1, fuga: 2 }, "aaa"]) //=> '(1 (('hoge' . 1) ('fuga' . 2)) 'aaa')'
sexpression.stringify({ a: 1, b: 2}) //=> '(:a 1 :b 2)'
sexpression.stringify([1, { hoge: 1, fuga: 2 }, "aaa"]) //=> '(1 (:hoge 1 :fuga 2) "aaa")'
sexpression.stringify({ a: 1, b: 2}, 'alist) //=> '(("a" . 1) ("b" . 2))'
sexpression.stringify([1, { hoge: 1, fuga: 2 }, "aaa"], 'alist) //=> '(1 (('hoge' . 1) ('fuga' . 2)) 'aaa')'

sexpression.stringify({ a: 1, b: 2}, 'keylist') //=> '(:a 1 :b 2)'
sexpression.stringify([1, { hoge: 1, fuga: 2 }, "aaa"], 'keylist') //=> '(1 (:hoge 1 :fuga 2) "aaa")'
```

```javascript
Expand Down
8 changes: 4 additions & 4 deletions lib/stringify.js
Expand Up @@ -5,7 +5,7 @@ var util = require('util')
var stringify = function(obj, type) {
var result;

type = type || 'alist';
type = type || 'keylist';

switch (typeof obj) {
case 'string':
Expand All @@ -30,10 +30,10 @@ var stringify = function(obj, type) {
} else {
result = [];
for (var key in obj) {
if (type === 'keylist') {
result.push(':' + key + ' ' + stringify(obj[key], type));
} else {
if (type === 'alist') {
result.push(stringify(new Cons(key, obj[key]), type));
} else {
result.push(':' + key + ' ' + stringify(obj[key], type));
}
}
result = '(' + result.join(' ') + ')';
Expand Down
8 changes: 4 additions & 4 deletions test/stringify.test.js
Expand Up @@ -72,24 +72,24 @@ describe('stringify()', function() {
});

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

it('should be symbol from object', function() {
expect(stringify({ name: 'hoge' })).to.be('hoge');
});

it('should specify keyword list from object', function() {
expect(stringify({ a: 1, b: 2 }, 'keylist')).to.be('(:a 1 :b 2)');
expect(stringify({ a: 1, b: 2 })).to.be('(:a 1 :b 2)');
});

it('should alist from object nested', function() {
expect(stringify([1, { hoge: 1, fuga: 2 }, "aaa"])).to
expect(stringify([1, { hoge: 1, fuga: 2 }, "aaa"], 'alist')).to
.be('(1 (("hoge" . 1) ("fuga" . 2)) "aaa")');
});

it('should specify keyword list from object nested', function() {
expect(stringify([1, { hoge: 1, fuga: 2 }, "aaa"], 'keylist')).to
expect(stringify([1, { hoge: 1, fuga: 2 }, "aaa"])).to
.be('(1 (:hoge 1 :fuga 2) "aaa")');
});
});
Expand Down

0 comments on commit fada0c6

Please sign in to comment.