Skip to content

Commit

Permalink
feat: Add optional space param to jsonStringify helper (#313)
Browse files Browse the repository at this point in the history
* Add optional space param to jsonStringify helper

* Add unit tests

Co-authored-by: Alex Lewitt <[email protected]>
  • Loading branch information
simonkotwicz and alewitt2 authored Nov 1, 2021
1 parent a346e15 commit b129784
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/handlebar-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ const helpers = {
}
return Buffer.from(data).toString('base64');
},
jsonStringify: function (data) {
return JSON.stringify(data);
jsonStringify: function (data, space) {
return JSON.stringify(data, null, space);
},
jsonDoubleStringify: function (data) {
jsonDoubleStringify: function (data, space) {
// if you want to use this, you must use our strTemplate, and you must not put quotes around your template (ie. `my-field: {{ jsonStringify my-json }}` is valid but `my-field: "{{ jsonStringify my-json }}"` is not)
return JSON.stringify(JSON.stringify(data));
return JSON.stringify(JSON.stringify(data, null, space));
}
};

Expand Down
8 changes: 8 additions & 0 deletions test/handlebar-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ describe('handlebar-helpers', function () {
let ret = HandlebarHelper.jsonStringify({ 'a': { 'b': 80808, 'c': true }, 'd': 'efgh' });
assert.equal('{"a":{"b":80808,"c":true},"d":"efgh"}', ret, 'should stringify json string properly');
});
it('should create stringified json data with whitespace when given json data and using space param', function () {
let ret = HandlebarHelper.jsonStringify({ 'a': { 'b': 80808, 'c': true }, 'd': 'efgh' }, 2);
assert.equal('{\n "a": {\n "b": 80808,\n "c": true\n },\n "d": "efgh"\n}', ret, 'should stringify json string and add whitespace properly');
});
it('should create stringified data when given string', function () {
let ret = HandlebarHelper.jsonStringify('hello from the "test suite"');
assert.equal('"hello from the \\"test suite\\""', ret, 'should stringify string properly');
Expand All @@ -439,6 +443,10 @@ describe('handlebar-helpers', function () {
let ret = HandlebarHelper.jsonDoubleStringify({ 'a': { 'b': 80808, 'c': true }, 'd': 'efgh' });
assert.equal('"{\\"a\\":{\\"b\\":80808,\\"c\\":true},\\"d\\":\\"efgh\\"}"', ret, 'should double stringify json string properly');
});
it('should create double stringified json data with whitespace when given json data and using space param', function () {
let ret = HandlebarHelper.jsonDoubleStringify({ 'a': { 'b': 80808, 'c': true }, 'd': 'efgh' }, 2);
assert.equal('"{\\n \\"a\\": {\\n \\"b\\": 80808,\\n \\"c\\": true\\n },\\n \\"d\\": \\"efgh\\"\\n}"', ret, 'should double stringify json string and add whitespace properly');
});
it('should create double stringified data when given string', function () {
let ret = HandlebarHelper.jsonDoubleStringify('hello from the "test suite"');
assert.equal('"\\"hello from the \\\\\\"test suite\\\\\\"\\""', ret, 'should double stringify string properly');
Expand Down

0 comments on commit b129784

Please sign in to comment.