Yaml.coffee 3.9 KB

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