Skip to content

Commit

Permalink
Merge pull request #48 from rendrjs/server-2-client-json
Browse files Browse the repository at this point in the history
add serverToClientJson helper
  • Loading branch information
saponifi3d committed Feb 25, 2015
2 parents d7b4f62 + 7631e80 commit f1ee88e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"url": "http://github.com/rendrjs/rendr-handlebars.git"
},
"dependencies": {
"underscore": "~1.7.0",
"handlebars": "1.3.0"
"handlebars": "1.3.0",
"underscore": "~1.7.0"
},
"peerDependencies": {
"rendr": ">=0.5.1"
Expand Down
1 change: 1 addition & 0 deletions shared/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = function(Handlebars, getTemplate) {
partial: require('./helpers/partial')(Handlebars, getTemplate),
json: require('./helpers/json')(Handlebars),
each: require('./helpers/each')(Handlebars),
serverToClientJson: require('./helpers/serverToClientJson')(Handlebars),
forEach: require('./helpers/forEach')
};
};
6 changes: 6 additions & 0 deletions shared/helpers/serverToClientJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function (Handlebars) {
return function (obj) {
var data = escape(JSON.stringify(obj));
return new Handlebars.SafeString('JSON.parse(unescape("' + data + '"))');
};
};
1 change: 1 addition & 0 deletions test/shared/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ describe('helpers', function () {
expect(subject.forEach).to.be.a('function');
expect(subject.partial).to.be.a('function');
expect(subject.view).to.be.a('function');
expect(subject.serverToClientJson).to.be.a('function');
});
});
22 changes: 22 additions & 0 deletions test/shared/helpers/serverToClientJson.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var Handlebars = require('handlebars').create(),
sinon = require('sinon'),
chai = require('chai'),
expect = chai.expect,
subject = require('../../../shared/helpers/serverToClientJson')(Handlebars);

describe('serverToClientJson', function () {
var data = { key: 'права' }

it('should add JSON.parse and unescape to the string', function () {
var result = subject(data),
expectedResult = 'JSON.parse(unescape("%7B%22key%22%3A%22%u043F%u0440%u0430%u0432%u0430%22%7D"))';

expect(expectedResult).to.equal(result.string);
});

it('should result in the same data after eval', function () {
var result = subject(data);
expect(eval(result.string)).to.deep.equal(data);
});

});

0 comments on commit f1ee88e

Please sign in to comment.