diff --git a/README.md b/README.md index ccf76140..170c1648 100644 --- a/README.md +++ b/README.md @@ -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'); @@ -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. diff --git a/lib/js-yaml/loader.js b/lib/js-yaml/loader.js index def3e71b..456133da 100644 --- a/lib/js-yaml/loader.js +++ b/lib/js-yaml/loader.js @@ -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]); } @@ -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)); + } }