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

Add "noArrayIndent" option #461

Merged
merged 1 commit into from
Jan 4, 2019
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ disable exceptions by setting the `skipInvalid` option to `true`.
options:

- `indent` _(default: 2)_ - indentation width to use (in spaces).
- `noArrayIndent` _(default: false)_ - when true, will not add an indentation level to array elements
- `skipInvalid` _(default: false)_ - do not throw on invalid types (like function
in the safe schema) and skip pairs and single values with such types.
- `flowLevel` (default: -1) - specifies level of nesting, when to switch from
Expand Down
26 changes: 14 additions & 12 deletions lib/js-yaml/dumper.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,17 @@ function encodeHex(character) {
}

function State(options) {
this.schema = options['schema'] || DEFAULT_FULL_SCHEMA;
this.indent = Math.max(1, (options['indent'] || 2));
this.skipInvalid = options['skipInvalid'] || false;
this.flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel']);
this.styleMap = compileStyleMap(this.schema, options['styles'] || null);
this.sortKeys = options['sortKeys'] || false;
this.lineWidth = options['lineWidth'] || 80;
this.noRefs = options['noRefs'] || false;
this.noCompatMode = options['noCompatMode'] || false;
this.condenseFlow = options['condenseFlow'] || false;
this.schema = options['schema'] || DEFAULT_FULL_SCHEMA;
this.indent = Math.max(1, (options['indent'] || 2));
this.noArrayIndent = options['noArrayIndent'] || false;
this.skipInvalid = options['skipInvalid'] || false;
this.flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel']);
this.styleMap = compileStyleMap(this.schema, options['styles'] || null);
this.sortKeys = options['sortKeys'] || false;
this.lineWidth = options['lineWidth'] || 80;
this.noRefs = options['noRefs'] || false;
this.noCompatMode = options['noCompatMode'] || false;
this.condenseFlow = options['condenseFlow'] || false;

this.implicitTypes = this.schema.compiledImplicit;
this.explicitTypes = this.schema.compiledExplicit;
Expand Down Expand Up @@ -734,13 +735,14 @@ function writeNode(state, level, object, block, compact, iskey) {
}
}
} else if (type === '[object Array]') {
var arrayLevel = (state.noArrayIndent) ? level - 1 : level;
if (block && (state.dump.length !== 0)) {
writeBlockSequence(state, level, state.dump, compact);
writeBlockSequence(state, arrayLevel, state.dump, compact);
if (duplicate) {
state.dump = '&ref_' + duplicateIndex + state.dump;
}
} else {
writeFlowSequence(state, level, state.dump);
writeFlowSequence(state, arrayLevel, state.dump);
if (duplicate) {
state.dump = '&ref_' + duplicateIndex + ' ' + state.dump;
}
Expand Down
18 changes: 18 additions & 0 deletions test/issues/0432.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';


var assert = require('assert');
var yaml = require('../../');


test('should indent arrays an extra level by default', function () {
var output = yaml.safeDump({ array: [ 'a', 'b' ] });
var expected = 'array:\n - a\n - b\n';
assert.strictEqual(output, expected);
});

test('should not indent arrays an extra level when disabled', function () {
var output = yaml.safeDump({ array: [ 'a', 'b' ] }, { noArrayIndent: true });
var expected = 'array:\n- a\n- b\n';
assert.strictEqual(output, expected);
});