Skip to content

Commit

Permalink
Support loadAll/safeLoadAdd without iterator (return array), #350
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin committed Jul 8, 2017
1 parent bd32506 commit 3c148e2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -142,10 +142,10 @@ require('js-yaml').load(untrusted_code) + ''
```


### safeLoadAll (string, iterator [ , options ])
### safeLoadAll (string [, iterator] [, options ])

Same as `safeLoad()`, but understands multi-document sources and applies
`iterator` to each document.
Same as `safeLoad()`, but understands multi-document sources. Applies
`iterator` to each document if specified, or returns aray of documents.

``` javascript
var yaml = require('js-yaml');
Expand All @@ -156,7 +156,7 @@ yaml.safeLoadAll(data, function (doc) {
```


### loadAll (string, iterator [ , options ])
### loadAll (string [, iterator] [ , options ])

Same as `safeLoadAll()` but uses `DEFAULT_FULL_SCHEMA` by default.

Expand Down
10 changes: 9 additions & 1 deletion lib/js-yaml/loader.js
Expand Up @@ -1556,6 +1556,10 @@ function loadDocuments(input, options) {
function loadAll(input, iterator, options) {
var documents = loadDocuments(input, options), index, length;

if (typeof iterator !== 'function') {
return documents;
}

for (index = 0, length = documents.length; index < length; index += 1) {
iterator(documents[index]);
}
Expand All @@ -1576,7 +1580,11 @@ function load(input, options) {


function safeLoadAll(input, output, options) {
loadAll(input, output, common.extend({ schema: DEFAULT_SAFE_SCHEMA }, 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));
}
}


Expand Down

0 comments on commit 3c148e2

Please sign in to comment.