scratch – Blame information for rev 125

Subversion Repositories:
Rev:
Rev Author Line No. Line
75 office 1  
2 Parser = require './Parser'
3 Dumper = require './Dumper'
4 Utils = require './Utils'
5  
6 # Yaml offers convenience methods to load and dump YAML.
7 #
8 class Yaml
9  
10 # Parses YAML into a JavaScript object.
11 #
12 # The parse method, when supplied with a YAML string,
13 # will do its best to convert YAML in a file into a JavaScript object.
14 #
15 # Usage:
16 # myObject = Yaml.parse('some: yaml');
17 # console.log(myObject);
18 #
19 # @param [String] input A string containing YAML
20 # @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types, false otherwise
21 # @param [Function] objectDecoder A function to deserialize custom objects, null otherwise
22 #
23 # @return [Object] The YAML converted to a JavaScript object
24 #
25 # @throw [ParseException] If the YAML is not valid
26 #
27 @parse: (input, exceptionOnInvalidType = false, objectDecoder = null) ->
28 return new Parser().parse(input, exceptionOnInvalidType, objectDecoder)
29  
30  
31 # Parses YAML from file path into a JavaScript object.
32 #
33 # The parseFile method, when supplied with a YAML file,
34 # will do its best to convert YAML in a file into a JavaScript object.
35 #
36 # Usage:
37 # myObject = Yaml.parseFile('config.yml');
38 # console.log(myObject);
39 #
40 # @param [String] path A file path pointing to a valid YAML file
41 # @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types, false otherwise
42 # @param [Function] objectDecoder A function to deserialize custom objects, null otherwise
43 #
44 # @return [Object] The YAML converted to a JavaScript object or null if the file doesn't exist.
45 #
46 # @throw [ParseException] If the YAML is not valid
47 #
48 @parseFile: (path, callback = null, exceptionOnInvalidType = false, objectDecoder = null) ->
49 if callback?
50 # Async
51 Utils.getStringFromFile path, (input) =>
52 result = null
53 if input?
54 result = @parse input, exceptionOnInvalidType, objectDecoder
55 callback result
56 return
57 else
58 # Sync
59 input = Utils.getStringFromFile path
60 if input?
61 return @parse input, exceptionOnInvalidType, objectDecoder
62 return null
63  
64  
65 # Dumps a JavaScript object to a YAML string.
66 #
67 # The dump method, when supplied with an object, will do its best
68 # to convert the object into friendly YAML.
69 #
70 # @param [Object] input JavaScript object
71 # @param [Integer] inline The level where you switch to inline YAML
72 # @param [Integer] indent The amount of spaces to use for indentation of nested nodes.
73 # @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types (a JavaScript resource or object), false otherwise
74 # @param [Function] objectEncoder A function to serialize custom objects, null otherwise
75 #
76 # @return [String] A YAML string representing the original JavaScript object
77 #
78 @dump: (input, inline = 2, indent = 4, exceptionOnInvalidType = false, objectEncoder = null) ->
79 yaml = new Dumper()
80 yaml.indentation = indent
81  
82 return yaml.dump(input, inline, 0, exceptionOnInvalidType, objectEncoder)
83  
84  
85 # Alias of dump() method for compatibility reasons.
86 #
87 @stringify: (input, inline, indent, exceptionOnInvalidType, objectEncoder) ->
88 return @dump input, inline, indent, exceptionOnInvalidType, objectEncoder
89  
90  
91 # Alias of parseFile() method for compatibility reasons.
92 #
93 @load: (path, callback, exceptionOnInvalidType, objectDecoder) ->
94 return @parseFile path, callback, exceptionOnInvalidType, objectDecoder
95  
96  
97 # Expose YAML namespace to browser
98 window?.YAML = Yaml
99  
100 # Not in the browser?
101 unless window?
102 @YAML = Yaml
103  
104 module.exports = Yaml