Repository: nodeca/js-yaml
Pull request #548 (2020-05-16 08:22:32)

fix(loader): Add support for `safe/loadAll(input, options)`
This makes it possible to invoke `safeLoadAll` and `loadAll` by passing only `input` and `options` as two arguments without breaking support for the 3‑arguements invocation with a nullish `iterator` parameter.

---

Supersedes and closes <https://github.com/nodeca/js-yaml/pull/381>
Supersedes and closes <https://github.com/nodeca/js-yaml/pull/547>


Patch:
diff --git a/lib/js-yaml/loader.js b/lib/js-yaml/loader.js
index e33b6c9..ef01386 100644
--- a/lib/js-yaml/loader.js
+++ b/lib/js-yaml/loader.js
@@ -1593,13 +1593,18 @@ function loadDocuments(input, options) {
 
 
 function loadAll(input, iterator, options) {
-  var documents = loadDocuments(input, options), index, length;
+  if (iterator !== null && typeof iterator === 'object' && typeof options === 'undefined') {
+    options = iterator;
+    iterator = null;
+  }
+
+  var documents = loadDocuments(input, options);
 
   if (typeof iterator !== 'function') {
     return documents;
   }
 
-  for (index = 0, length = documents.length; index < length; index += 1) {
+  for (var index = 0, length = documents.length; index < length; index += 1) {
     iterator(documents[index]);
   }
 }
@@ -1618,12 +1623,13 @@ function load(input, options) {
 }
 
 
-function safeLoadAll(input, output, options) {
-  if (typeof output === 'function') {
-    loadAll(input, output, common.extend({ schema: DEFAULT_SAFE_SCHEMA }, options));
-  } else {
-    return loadAll(input, common.extend({ schema: DEFAULT_SAFE_SCHEMA }, options));
+function safeLoadAll(input, iterator, options) {
+  if (typeof iterator === 'object' && iterator !== null && typeof options === 'undefined') {
+    options = iterator;
+    iterator = null;
   }
+
+  return loadAll(input, iterator, common.extend({ schema: DEFAULT_SAFE_SCHEMA }, options));
 }
 
 


Test patch:
diff --git a/test/units/loader-parameters.js b/test/units/loader-parameters.js
new file mode 100644
index 0000000..56cf399
--- /dev/null
+++ b/test/units/loader-parameters.js
@@ -0,0 +1,54 @@
+'use strict';
+
+var assert = require('assert');
+var yaml = require('../..');
+
+suite('loader parameters', function () {
+  var testStr = 'test: 1 \ntest: 2';
+  var expected =  [ { test: 2 } ];
+  var result;
+
+  test('loadAll(input, options)', function () {
+    result = yaml.loadAll(testStr, { json: true });
+    assert.deepEqual(result, expected);
+
+    result = [];
+    yaml.loadAll(testStr, function (doc) {
+      result.push(doc);
+    }, { json: true });
+    assert.deepEqual(result, expected);
+  });
+
+  test('loadAll(input, null, options)', function () {
+    result = yaml.loadAll(testStr, null, { json: true });
+    assert.deepEqual(result, expected);
+
+    result = [];
+    yaml.loadAll(testStr, function (doc) {
+      result.push(doc);
+    }, { json: true });
+    assert.deepEqual(result, expected);
+  });
+
+  test('safeLoadAll(input, options)', function () {
+    result = yaml.safeLoadAll(testStr, { json: true });
+    assert.deepEqual(result, expected);
+
+    result = [];
+    yaml.safeLoadAll(testStr, function (doc) {
+      result.push(doc);
+    }, { json: true });
+    assert.deepEqual(result, expected);
+  });
+
+  test('safeLoadAll(input, null, options)', function () {
+    result = yaml.safeLoadAll(testStr, null, { json: true });
+    assert.deepEqual(result, expected);
+
+    result = [];
+    yaml.safeLoadAll(testStr, function (doc) {
+      result.push(doc);
+    }, { json: true });
+    assert.deepEqual(result, expected);
+  });
+});