2
2
3
3
/*eslint-disable no-console*/
4
4
5
- var fs = require ( 'fs' ) ;
6
- var path = require ( 'path' ) ;
7
- var util = require ( 'util' ) ;
8
- var yaml = require ( '../' ) ;
5
+ const util = require ( 'util' ) ;
6
+ const yaml = require ( '../' ) ;
9
7
10
8
11
- var tags = [ 'scalar' , 'sequence' , 'mapping' ] . map ( function ( kind ) {
9
+ const tags = [ 'scalar' , 'sequence' , 'mapping' ] . map ( function ( kind ) {
12
10
// first argument here is a prefix, so this type will handle anything starting with !
13
11
return new yaml . Type ( '!' , {
14
12
kind : kind ,
@@ -19,24 +17,14 @@ var tags = [ 'scalar', 'sequence', 'mapping' ].map(function (kind) {
19
17
} ) ;
20
18
} ) ;
21
19
22
- var SCHEMA = yaml . DEFAULT_SCHEMA . extend ( tags ) ;
20
+ const SCHEMA = yaml . DEFAULT_SCHEMA . extend ( tags ) ;
23
21
24
- // do not execute the following if file is required (http://stackoverflow.com/a/6398335)
25
- if ( require . main === module ) {
22
+ const data = `
23
+ subject: Handling unknown types in JS-YAML
24
+ scalar: !unknown_scalar_tag 123
25
+ sequence: !unknown_sequence_tag [ 1, 2, 3 ]
26
+ mapping: !unknown_mapping_tag { foo: 1, bar: 2 }
27
+ ` ;
26
28
27
- // And read a document using that schema.
28
- fs . readFile ( path . join ( __dirname , 'handle_unknown_types.yml' ) , 'utf8' , function ( error , data ) {
29
- var loaded ;
30
-
31
- if ( ! error ) {
32
- loaded = yaml . load ( data , { schema : SCHEMA } ) ;
33
- console . log ( util . inspect ( loaded , false , 20 , true ) ) ;
34
- } else {
35
- console . error ( error . stack || error . message || String ( error ) ) ;
36
- }
37
- } ) ;
38
- }
39
-
40
- // There are some exports to play with this example interactively.
41
- module . exports . tags = tags ;
42
- module . exports . SCHEMA = SCHEMA ;
29
+ const loaded = yaml . load ( data , { schema : SCHEMA } ) ;
30
+ console . log ( util . inspect ( loaded , false , 20 , true ) ) ;
0 commit comments