Skip to content

Commit

Permalink
require(filename) now returns a single document
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed Jul 1, 2012
1 parent 062a245 commit b0e6ced
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 20 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Expand Up @@ -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
Expand Down
19 changes: 5 additions & 14 deletions README.md
Expand Up @@ -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
Expand Down Expand Up @@ -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);
```


Expand Down
3 changes: 1 addition & 2 deletions lib/js-yaml.js
Expand Up @@ -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);
};
Expand Down
2 changes: 1 addition & 1 deletion test/issues/issue-17.js
Expand Up @@ -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);
}
});
2 changes: 1 addition & 1 deletion test/issues/issue-19.js
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion test/issues/issue-26.js
Expand Up @@ -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');
}
});
2 changes: 1 addition & 1 deletion test/issues/issue-8.js
Expand Up @@ -11,7 +11,7 @@ module.exports = require('../helper').issue({
fixed: true,
test: function () {
Assert.doesNotThrow(function () {
require(source).shift();
require(source);
}, TypeError);
}
});

0 comments on commit b0e6ced

Please sign in to comment.