Repository: nodeca/js-yaml
Pull request #461 (2019-01-04 01:01:32)

Add "noArrayIndent" option
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.


Patch:
diff --git a/README.md b/README.md
index 7b9e0fc..03a9e5a 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/lib/js-yaml/dumper.js b/lib/js-yaml/dumper.js
index 67cf6ca..f1aefc2 100644
--- a/lib/js-yaml/dumper.js
+++ b/lib/js-yaml/dumper.js
@@ -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;
@@ -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;
         }


Test patch:
diff --git a/test/issues/0432.js b/test/issues/0432.js
new file mode 100644
index 0000000..b6cd162
--- /dev/null
+++ b/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);
+});