Skip to content

Commit e2ae05b

Browse files
Revert "Removed the external-facing YAML object. All methods will now allow a YAML string to be passed-in instead"
This reverts commit 54af127.
1 parent d26ba61 commit e2ae05b

File tree

10 files changed

+568
-228
lines changed

10 files changed

+568
-228
lines changed

dist/ref-parser.js

Lines changed: 155 additions & 111 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.js.map

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js

Lines changed: 96 additions & 93 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js.map

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ Classes & Methods
2727
- [`get()`](refs.md#getref-options)
2828
- [`set()`](refs.md#setref-value-options)
2929

30+
#### [`YAML`](yaml.md)
31+
- [`parse()`](yaml.md#parsetext)
32+
- [`stringify()`](yaml.md#stringifyvalue)
33+
3034
#### [`Options`](options.md)
3135

3236

docs/yaml.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
`YAML` object
2+
==========================
3+
4+
This object provides simple YAML parsing functions. JSON Schema $Ref Parser uses this object internally
5+
for its own YAML parsing, but it is also exposed so you can use it in your code if needed.
6+
7+
8+
### `parse(text)`
9+
10+
- **text** (_required_) - `string`<br>
11+
The YAML string to be parsed.
12+
13+
- **Return Value:**<br>
14+
Returns the parsed value, which can be any valid JSON type (object, array, string, number, etc.)
15+
16+
This method is similar to [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse), but it supports YAML _in addition_ to JSON (since any JSON document is also a valid YAML document).
17+
18+
```javascript
19+
var YAML = $RefParser.YAML;
20+
var text = "title: person \n" +
21+
"required: \n" +
22+
" - name \n" +
23+
" - age \n" +
24+
"properties: \n" +
25+
" name: \n" +
26+
" type: string \n" +
27+
" age: \n" +
28+
" type: number"
29+
30+
var obj = YAML.parse(text);
31+
32+
// {
33+
// title: "person",
34+
// required: ["name", "age"],
35+
// properties: {
36+
// name: {
37+
// type: "string"
38+
// },
39+
// age: {
40+
// type: "number"
41+
// }
42+
// }
43+
// }
44+
```
45+
46+
47+
### `stringify(value)`
48+
49+
- **value** (_required_)<br>
50+
The value to be converted to a YAML string. Can be any valid JSON type (object, array, string, number, etc.)
51+
52+
- **Return Value:** `string`<br>
53+
Returns the a YAML string containing the serialized value
54+
55+
This method is similar to [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify), except that it converts a value to a YAML string instead of a JSON string.
56+
57+
```javascript
58+
var YAML = $RefParser.YAML;
59+
var obj = {
60+
title: "person",
61+
required: ["name", "age"],
62+
properties: {
63+
name: {
64+
type: "string"
65+
},
66+
age: {
67+
type: "number"
68+
}
69+
}
70+
};
71+
72+
73+
var string = YAML.stringify(obj);
74+
75+
// title: person
76+
// required:
77+
// - name
78+
// - age
79+
// properties:
80+
// name:
81+
// type: string
82+
// age:
83+
// type: number
84+
```

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var Promise = require('./util/promise'),
1212
ono = require('ono');
1313

1414
module.exports = $RefParser;
15+
module.exports.YAML = require('./util/yaml');
1516

1617
/**
1718
* This class parses a JSON schema, builds a map of its JSON references and their resolved values,

lib/parsers/yaml.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
22

3-
var Promise = require('../util/promise');
4-
var yaml = require('js-yaml');
5-
var ono = require('ono');
3+
var Promise = require('../util/promise'),
4+
YAML = require('../util/yaml');
65

76
module.exports = {
87
/**
@@ -39,28 +38,14 @@ module.exports = {
3938
* @returns {Promise}
4039
*/
4140
parse: function parseYAML(file) {
42-
var parsed;
43-
4441
return new Promise(function(resolve, reject) {
4542
var data = file.data;
4643
if (Buffer.isBuffer(data)) {
4744
data = data.toString();
4845
}
4946

5047
if (typeof data === 'string') {
51-
try {
52-
parsed = yaml.safeLoad(data);
53-
}
54-
catch (e) {
55-
if (e instanceof Error) {
56-
throw e;
57-
}
58-
else {
59-
// https://github.com/nodeca/js-yaml/issues/153
60-
throw ono(e, e.message);
61-
}
62-
}
63-
resolve(parsed);
48+
resolve(YAML.parse(data));
6449
}
6550
else {
6651
// data is already a JavaScript value (object, array, number, null, NaN, etc.)

lib/util/yaml.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* eslint lines-around-comment: [2, {beforeBlockComment: false}] */
2+
'use strict';
3+
4+
var yaml = require('js-yaml'),
5+
ono = require('ono');
6+
7+
/**
8+
* Simple YAML parsing functions, similar to {@link JSON.parse} and {@link JSON.stringify}
9+
*/
10+
module.exports = {
11+
/**
12+
* Parses a YAML string and returns the value.
13+
*
14+
* @param {string} text - The YAML string to be parsed
15+
* @param {function} [reviver] - Not currently supported. Provided for consistency with {@link JSON.parse}
16+
* @returns {*}
17+
*/
18+
parse: function yamlParse(text, reviver) {
19+
try {
20+
return yaml.safeLoad(text);
21+
}
22+
catch (e) {
23+
if (e instanceof Error) {
24+
throw e;
25+
}
26+
else {
27+
// https://github.com/nodeca/js-yaml/issues/153
28+
throw ono(e, e.message);
29+
}
30+
}
31+
},
32+
33+
/**
34+
* Converts a JavaScript value to a YAML string.
35+
*
36+
* @param {*} value - The value to convert to YAML
37+
* @param {function|array} replacer - Not currently supported. Provided for consistency with {@link JSON.stringify}
38+
* @param {string|number} space - The number of spaces to use for indentation, or a string containing the number of spaces.
39+
* @returns {string}
40+
*/
41+
stringify: function yamlStringify(value, replacer, space) {
42+
try {
43+
var indent = (typeof space === 'string' ? space.length : space) || 2;
44+
return yaml.safeDump(value, {indent: indent});
45+
}
46+
catch (e) {
47+
if (e instanceof Error) {
48+
throw e;
49+
}
50+
else {
51+
// https://github.com/nodeca/js-yaml/issues/153
52+
throw ono(e, e.message);
53+
}
54+
}
55+
}
56+
};

test/specs/yaml.spec.js

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
'use strict';
2+
3+
describe('YAML object', function() {
4+
describe('parse', function() {
5+
it('should parse an object',
6+
function(done) {
7+
var obj = $RefParser.YAML.parse(
8+
'title: person\n' +
9+
'required:\n' +
10+
' - name\n' +
11+
' - age\n' +
12+
'properties:\n' +
13+
' name:\n' +
14+
' type: string\n' +
15+
' age:\n' +
16+
' type: number'
17+
);
18+
19+
expect(obj).to.deep.equal({
20+
title: 'person',
21+
required: ['name', 'age'],
22+
properties: {
23+
name: {
24+
type: 'string'
25+
},
26+
age: {
27+
type: 'number'
28+
}
29+
}
30+
});
31+
done();
32+
}
33+
);
34+
35+
it('should parse a string',
36+
function(done) {
37+
var str = $RefParser.YAML.parse('hello, world');
38+
expect(str).to.equal('hello, world');
39+
done();
40+
}
41+
);
42+
43+
it('should parse a number',
44+
function(done) {
45+
var str = $RefParser.YAML.parse('42');
46+
expect(str).to.be.a('number').equal(42);
47+
done();
48+
}
49+
);
50+
});
51+
52+
describe('stringify', function() {
53+
it('should stringify an object',
54+
function(done) {
55+
var yaml = $RefParser.YAML.stringify({
56+
title: 'person',
57+
required: ['name', 'age'],
58+
properties: {
59+
name: {
60+
type: 'string'
61+
},
62+
age: {
63+
type: 'number'
64+
}
65+
}
66+
});
67+
68+
expect(yaml).to.equal(
69+
'title: person\n' +
70+
'required:\n' +
71+
' - name\n' +
72+
' - age\n' +
73+
'properties:\n' +
74+
' name:\n' +
75+
' type: string\n' +
76+
' age:\n' +
77+
' type: number\n'
78+
);
79+
done();
80+
}
81+
);
82+
83+
it('should support a custom indent (as a string)',
84+
function(done) {
85+
var yaml = $RefParser.YAML.stringify({
86+
title: 'person',
87+
required: ['name', 'age'],
88+
properties: {
89+
name: {
90+
type: 'string'
91+
},
92+
age: {
93+
type: 'number'
94+
}
95+
}
96+
}, null, ' ');
97+
98+
expect(yaml).to.equal(
99+
'title: person\n' +
100+
'required:\n' +
101+
' - name\n' +
102+
' - age\n' +
103+
'properties:\n' +
104+
' name:\n' +
105+
' type: string\n' +
106+
' age:\n' +
107+
' type: number\n'
108+
);
109+
done();
110+
}
111+
);
112+
113+
it('should support a custom indent (as a number)',
114+
function(done) {
115+
var yaml = $RefParser.YAML.stringify({
116+
title: 'person',
117+
required: ['name', 'age'],
118+
properties: {
119+
name: {
120+
type: 'string'
121+
},
122+
age: {
123+
type: 'number'
124+
}
125+
}
126+
}, null, 10);
127+
128+
expect(yaml).to.equal(
129+
'title: person\n' +
130+
'required:\n' +
131+
' - name\n' +
132+
' - age\n' +
133+
'properties:\n' +
134+
' name:\n' +
135+
' type: string\n' +
136+
' age:\n' +
137+
' type: number\n'
138+
);
139+
done();
140+
}
141+
);
142+
143+
it('should stringify a string',
144+
function(done) {
145+
var yaml = $RefParser.YAML.stringify('hello, world');
146+
expect(yaml).to.equal('\'hello, world\'\n');
147+
done();
148+
}
149+
);
150+
151+
it('should stringify a number',
152+
function(done) {
153+
var yaml = $RefParser.YAML.stringify(42);
154+
expect(yaml).to.equal('42\n');
155+
done();
156+
}
157+
);
158+
});
159+
});

0 commit comments

Comments
 (0)