Skip to content

Commit

Permalink
fix(loader): Add support for safe/loadAll(input, options)
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss authored and rlidwka committed May 22, 2020
1 parent d6983dd commit 10be97e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/js-yaml/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Expand All @@ -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));
}


Expand Down
54 changes: 54 additions & 0 deletions test/units/loader-parameters.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 10be97e

Please sign in to comment.