Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX : https://github.com/KenanY/json2toml/issues/92 #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ function format(obj) {
: JSON.stringify(obj);
}

function isArrayOfTables(simplePairs) {
return simplePairs.some(function(simplePairArray) {
return Array.isArray(simplePairArray[1]) && isPlainObject(simplePairArray[1][0]);
});
}

function isArrayOfObjects(array) {
return Array.isArray(array) && array.every(function(element) {
return isPlainObject(element);
});
}

function isObjectArrayOfTables(obj) {
return Array.isArray(obj) && obj.length === 2 && isArrayOfObjects(obj[1]);
}

function isLastObjectArrayOfTables(simplePairs) {
var array = simplePairs[simplePairs.length - 1];
return isObjectArrayOfTables(array);
}

module.exports = function(hash, options = {}) {
function visit(hash, prefix) {
var nestedPairs = [];
Expand All @@ -23,7 +44,8 @@ module.exports = function(hash, options = {}) {
(isPlainObject(value) ? nestedPairs : simplePairs).push([key, value]);
});

if (!(isEmpty(prefix) || isEmpty(simplePairs))) {
if (!isEmpty(prefix) && !isEmpty(simplePairs)
&& !isArrayOfTables(simplePairs)) {
toml += '[' + prefix + ']\n';
indentStr = ''.padStart(options.indent, ' ');
}
Expand All @@ -32,10 +54,30 @@ module.exports = function(hash, options = {}) {
var key = array[0];
var value = array[1];

toml += indentStr + key + ' = ' + format(value) + '\n';
if (isObjectArrayOfTables(array)) {
if (simplePairs.indexOf(array) > 0 && options.newlineAfterSection) {
var lastObj = simplePairs[simplePairs.indexOf(array) - 1];
if (!isObjectArrayOfTables(lastObj)) {
toml += '\n';
}
}
forEach(value, function(obj) {
if (!isEmpty(prefix)) {
toml += '[[' + prefix + '.' + key + ']]\n';
}
else {
toml += '[[' + key + ']]\n';
}
visit(obj, '');
});
}
else {
toml += indentStr + key + ' = ' + format(value) + '\n';
}
});

if (!isEmpty(simplePairs) && options.newlineAfterSection) {
if (!isEmpty(simplePairs) && !isLastObjectArrayOfTables(simplePairs)
&& options.newlineAfterSection) {
toml += '\n';
}

Expand Down
47 changes: 47 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,53 @@ test('nested', function(t) {
t.equal(json2toml(hash), toml);
});

test('mixed array', function(t) {
t.plan(1);
console.log(json2toml({"foo": [{"bar": false}, 9, true]}))
var hash = { nested: [{ a: 'one' }, 1, true, 'string'] };
var toml = 'nested = [{"a":"one"},1,true,"string"]\n';

t.equal(json2toml(hash), toml);
});

test('nested array of tables', function(t) {
t.plan(2);

var hash = { nested: [{ a: 'one' }, { a: 'two' }] };
var toml = '[[nested]]\n'
+ 'a = "one"\n'
+ '[[nested]]\n'
+ 'a = "two"\n';
t.equal(json2toml(hash), toml);

hash.other = {};
hash.other.nest = [{ a: 'one' }, { a: 'two' }];
toml = toml + '[[other.nest]]\n'
+ 'a = "one"\n'
+ '[[other.nest]]\n'
+ 'a = "two"\n';
t.equal(json2toml(hash), toml);
});

test('nested array of tables with new line', function(t) {
t.plan(2);

var hash = { nested: [{ a: 'one' }, { a: 'two' }] };
var toml = '[[nested]]\n'
+ 'a = "one"\n\n'
+ '[[nested]]\n'
+ 'a = "two"\n\n';
t.equal(json2toml(hash, { newlineAfterSection: true }), toml);

hash.other = {};
hash.other.nest = [{ a: 'one' }, { a: 'two' }];
toml = toml + '[[other.nest]]\n'
+ 'a = "one"\n\n'
+ '[[other.nest]]\n'
+ 'a = "two"\n\n';
t.equal(json2toml(hash, { newlineAfterSection: true }), toml);
});

test('pretty', function(t) {
t.plan(1);

Expand Down