diff --git a/HISTORY.md b/HISTORY.md index 63bdd234..7f8b276d 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,6 +3,8 @@ * `y`, `yes`, `n`, `no`, `on`, `off` are not converted to Booleans anymore. Fixes #42. +* `require(filename)` now returns a single document and throws an Error if + file contains more than one document. 0.3.7 / 2012-02-28 diff --git a/README.md b/README.md index 30c15af8..bc9bccce 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ Now you can use all modern YAML feature right in JavaScript. Originally snapshot core developers: from now on we try to keep as minimal subset of rules as possible to keep things obvious. Booleans are following YAML 1.2 core schema now: http://www.yaml.org/spec/1.2/spec.html#id2804923 +- `require('file.yml')` now returns a single document (was array of documents) + and throws an error when file contains multiple documents. ## Installation @@ -52,21 +54,10 @@ Just with one string! ``` javascript require('js-yaml'); -// Get array of documents, or throw exception on error -var docs = require('/home/ixti/examples.yml'); +// Get document, or throw exception on error +var doc = require('/home/ixti/example.yml'); -console.log(docs); -``` - -If you are sure, that file has only one document, chained `shift()` will help to exclude array wrapper: - -``` javascript -require('js-yaml'); - -// Get array of documents, or throw exception on error -var singleDoc = require('/home/ixti/examples.yml').shift(); - -console.log(singleDoc); +console.log(doc); ``` diff --git a/lib/js-yaml.js b/lib/js-yaml.js index 1c138cd3..dd58cbd1 100644 --- a/lib/js-yaml.js +++ b/lib/js-yaml.js @@ -72,8 +72,7 @@ jsyaml.addConstructor = function addConstructor(tag, constructor, Loader) { var fd = fs.openSync(filename, 'r'); // fill in documents - module.exports = []; - jsyaml.loadAll(fd, function (doc) { module.exports.push(doc); }); + module.exports = jsyaml.load(fd); fs.closeSync(fd); }; diff --git a/test/issues/issue-17.js b/test/issues/issue-17.js index 187150fa..87149cee 100644 --- a/test/issues/issue-17.js +++ b/test/issues/issue-17.js @@ -10,7 +10,7 @@ module.exports = require('../helper').issue({ title: "#17: Non-specific `!` tags should resolve to !!str", fixed: true, test: function () { - var str = require(source).shift(); + var str = require(source); Assert.equal('string', typeof str); } }); diff --git a/test/issues/issue-19.js b/test/issues/issue-19.js index 64410423..91f61f34 100644 --- a/test/issues/issue-19.js +++ b/test/issues/issue-19.js @@ -10,7 +10,7 @@ module.exports = require('../helper').issue({ title: "#19: Timestamp parsing is one month off", fixed: true, test: function () { - var doc = require(source).shift(), expected = new Date(2011, 11, 24); + var doc = require(source), expected = new Date(2011, 11, 24); // JS month starts with 0 (0 => Jan, 1 => Feb, ...) Assert.equal(doc.xmas.getTime(), expected.getTime()); diff --git a/test/issues/issue-26.js b/test/issues/issue-26.js index f5938c8a..eb85830a 100644 --- a/test/issues/issue-26.js +++ b/test/issues/issue-26.js @@ -10,7 +10,7 @@ module.exports = require('../helper').issue({ title: "#26: should convert new line into white space", fixed: true, test: function () { - var doc = require(source).shift(); + var doc = require(source); Assert.equal(doc.test, 'a b c\n'); } }); diff --git a/test/issues/issue-8.js b/test/issues/issue-8.js index 468cbbdc..1e1798cc 100644 --- a/test/issues/issue-8.js +++ b/test/issues/issue-8.js @@ -11,7 +11,7 @@ module.exports = require('../helper').issue({ fixed: true, test: function () { Assert.doesNotThrow(function () { - require(source).shift(); + require(source); }, TypeError); } });