Skip to content

Commit

Permalink
Add "noArrayIndent" option (#461)
Browse files Browse the repository at this point in the history
Addresses issue #432 by adding a `noArrayIndent` option to optionally not add an extra level of indentation to array elements.

When `noArrayIndent` option is set to `false` (or not provided), output is:
```
array:
  - a
  - b
  - c
```

When `noArrayIndent` option is set to `true`, output is:
```
array:
- a
- b
- c
```

This helps avoid diffs when parsing, modifying, and generating valid yaml that does *not* use extra indentation for arrays.
  • Loading branch information
jacob-hd authored and Vitaly Puzrin committed Jan 4, 2019
1 parent 00bba11 commit 784d1d0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
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
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
@@ -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);
});

0 comments on commit 784d1d0

Please sign in to comment.