dumper.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. 'use strict';
  2. var common = require('./common');
  3. var NIL = common.NIL;
  4. var YAMLException = require('./exception');
  5. var DEFAULT_SCHEMA = require('./schema/default');
  6. var SAFE_SCHEMA = require('./schema/safe');
  7. var _hasOwnProperty = Object.prototype.hasOwnProperty;
  8. var CHAR_TAB = 0x09; /* Tab */
  9. var CHAR_LINE_FEED = 0x0A; /* LF */
  10. var CHAR_CARRIAGE_RETURN = 0x0D; /* CR */
  11. var CHAR_SPACE = 0x20; /* Space */
  12. var CHAR_EXCLAMATION = 0x21; /* ! */
  13. var CHAR_DOUBLE_QUOTE = 0x22; /* " */
  14. var CHAR_SHARP = 0x23; /* # */
  15. var CHAR_PERCENT = 0x25; /* % */
  16. var CHAR_AMPERSAND = 0x26; /* & */
  17. var CHAR_SINGLE_QUOTE = 0x27; /* ' */
  18. var CHAR_ASTERISK = 0x2A; /* * */
  19. var CHAR_COMMA = 0x2C; /* , */
  20. var CHAR_MINUS = 0x2D; /* - */
  21. var CHAR_COLON = 0x3A; /* : */
  22. var CHAR_GREATER_THAN = 0x3E; /* > */
  23. var CHAR_QUESTION = 0x3F; /* ? */
  24. var CHAR_COMMERCIAL_AT = 0x40; /* @ */
  25. var CHAR_LEFT_SQUARE_BRACKET = 0x5B; /* [ */
  26. var CHAR_RIGHT_SQUARE_BRACKET = 0x5D; /* ] */
  27. var CHAR_GRAVE_ACCENT = 0x60; /* ` */
  28. var CHAR_LEFT_CURLY_BRACKET = 0x7B; /* { */
  29. var CHAR_VERTICAL_LINE = 0x7C; /* | */
  30. var CHAR_RIGHT_CURLY_BRACKET = 0x7D; /* } */
  31. var ESCAPE_SEQUENCES = {};
  32. ESCAPE_SEQUENCES[0x00] = '\\0';
  33. ESCAPE_SEQUENCES[0x07] = '\\a';
  34. ESCAPE_SEQUENCES[0x08] = '\\b';
  35. ESCAPE_SEQUENCES[0x09] = '\\t';
  36. ESCAPE_SEQUENCES[0x0A] = '\\n';
  37. ESCAPE_SEQUENCES[0x0B] = '\\v';
  38. ESCAPE_SEQUENCES[0x0C] = '\\f';
  39. ESCAPE_SEQUENCES[0x0D] = '\\r';
  40. ESCAPE_SEQUENCES[0x1B] = '\\e';
  41. ESCAPE_SEQUENCES[0x22] = '\\"';
  42. ESCAPE_SEQUENCES[0x5C] = '\\\\';
  43. ESCAPE_SEQUENCES[0x85] = '\\N';
  44. ESCAPE_SEQUENCES[0xA0] = '\\_';
  45. ESCAPE_SEQUENCES[0x2028] = '\\L';
  46. ESCAPE_SEQUENCES[0x2029] = '\\P';
  47. function kindOf(object) {
  48. var kind = typeof object;
  49. if (null === object) {
  50. return 'null';
  51. } else if ('number' === kind) {
  52. return 0 === object % 1 ? 'integer' : 'float';
  53. } else if ('object' === kind && Array.isArray(object)) {
  54. return 'array';
  55. } else {
  56. return kind;
  57. }
  58. }
  59. function compileStyleMap(schema, map) {
  60. var result, keys, index, length, tag, style, type;
  61. if (null === map) {
  62. return {};
  63. }
  64. result = {};
  65. keys = Object.keys(map);
  66. for (index = 0, length = keys.length; index < length; index += 1) {
  67. tag = keys[index];
  68. style = String(map[tag]);
  69. if ('!!' === tag.slice(0, 2)) {
  70. tag = 'tag:yaml.org,2002:' + tag.slice(2);
  71. }
  72. type = schema.compiledTypeMap[tag];
  73. if (type && type.dumper) {
  74. if (_hasOwnProperty.call(type.dumper.styleAliases, style)) {
  75. style = type.dumper.styleAliases[style];
  76. }
  77. }
  78. result[tag] = style;
  79. }
  80. return result;
  81. }
  82. function encodeHex(character) {
  83. var string, handle, length;
  84. string = character.toString(16).toUpperCase();
  85. if (character <= 0xFF) {
  86. handle = 'x';
  87. length = 2;
  88. } else if (character <= 0xFFFF) {
  89. handle = 'u';
  90. length = 4;
  91. } else if (character <= 0xFFFFFFFF) {
  92. handle = 'U';
  93. length = 8;
  94. } else {
  95. throw new YAMLException('code point within a string may not be greater than 0xFFFFFFFF');
  96. }
  97. return '\\' + handle + common.repeat('0', length - string.length) + string;
  98. }
  99. function dump(input, options) {
  100. options = options || {};
  101. var schema = options['schema'] || DEFAULT_SCHEMA,
  102. indent = Math.max(1, (options['indent'] || 2)),
  103. flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel']),
  104. styleMap = compileStyleMap(schema, options['styles'] || null),
  105. implicitTypes = schema.compiledImplicit,
  106. explicitTypes = schema.compiledExplicit,
  107. kind,
  108. tag,
  109. result;
  110. function generateNextLine(level) {
  111. return '\n' + common.repeat(' ', indent * level);
  112. }
  113. function testImplicitResolving(object) {
  114. var index, length, type;
  115. for (index = 0, length = implicitTypes.length; index < length; index += 1) {
  116. type = implicitTypes[index];
  117. if (null !== type.loader &&
  118. NIL !== type.loader.resolver(object, false)) {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. function writeScalar(object) {
  125. var isQuoted, checkpoint, position, length, character;
  126. result = '';
  127. isQuoted = false;
  128. checkpoint = 0;
  129. if (0 === object.length ||
  130. CHAR_SPACE === object.charCodeAt(0) ||
  131. CHAR_SPACE === object.charCodeAt(object.length - 1)) {
  132. isQuoted = true;
  133. }
  134. for (position = 0, length = object.length; position < length; position += 1) {
  135. character = object.charCodeAt(position);
  136. if (!isQuoted) {
  137. if (CHAR_TAB === character ||
  138. CHAR_LINE_FEED === character ||
  139. CHAR_CARRIAGE_RETURN === character ||
  140. CHAR_COMMA === character ||
  141. CHAR_LEFT_SQUARE_BRACKET === character ||
  142. CHAR_RIGHT_SQUARE_BRACKET === character ||
  143. CHAR_LEFT_CURLY_BRACKET === character ||
  144. CHAR_RIGHT_CURLY_BRACKET === character ||
  145. CHAR_SHARP === character ||
  146. CHAR_AMPERSAND === character ||
  147. CHAR_ASTERISK === character ||
  148. CHAR_EXCLAMATION === character ||
  149. CHAR_VERTICAL_LINE === character ||
  150. CHAR_GREATER_THAN === character ||
  151. CHAR_SINGLE_QUOTE === character ||
  152. CHAR_DOUBLE_QUOTE === character ||
  153. CHAR_PERCENT === character ||
  154. CHAR_COMMERCIAL_AT === character ||
  155. CHAR_GRAVE_ACCENT === character ||
  156. CHAR_QUESTION === character ||
  157. CHAR_COLON === character ||
  158. CHAR_MINUS === character) {
  159. isQuoted = true;
  160. }
  161. }
  162. if (ESCAPE_SEQUENCES[character] ||
  163. !((0x00020 <= character && character <= 0x00007E) ||
  164. (0x00085 === character) ||
  165. (0x000A0 <= character && character <= 0x00D7FF) ||
  166. (0x0E000 <= character && character <= 0x00FFFD) ||
  167. (0x10000 <= character && character <= 0x10FFFF))) {
  168. result += object.slice(checkpoint, position);
  169. result += ESCAPE_SEQUENCES[character] || encodeHex(character);
  170. checkpoint = position + 1;
  171. isQuoted = true;
  172. }
  173. }
  174. if (checkpoint < position) {
  175. result += object.slice(checkpoint, position);
  176. }
  177. if (!isQuoted && testImplicitResolving(result)) {
  178. isQuoted = true;
  179. }
  180. if (isQuoted) {
  181. result = '"' + result + '"';
  182. }
  183. }
  184. function writeFlowSequence(level, object) {
  185. var _result = '',
  186. _tag = tag,
  187. index,
  188. length;
  189. for (index = 0, length = object.length; index < length; index += 1) {
  190. if (0 !== index) {
  191. _result += ', ';
  192. }
  193. writeNode(level, object[index], false, false);
  194. _result += result;
  195. }
  196. tag = _tag;
  197. result = '[' + _result + ']';
  198. }
  199. function writeBlockSequence(level, object, compact) {
  200. var _result = '',
  201. _tag = tag,
  202. index,
  203. length;
  204. for (index = 0, length = object.length; index < length; index += 1) {
  205. if (!compact || 0 !== index) {
  206. _result += generateNextLine(level);
  207. }
  208. writeNode(level + 1, object[index], true, true);
  209. _result += '- ' + result;
  210. }
  211. tag = _tag;
  212. result = _result;
  213. }
  214. function writeFlowMapping(level, object) {
  215. var _result = '',
  216. _tag = tag,
  217. objectKeyList = Object.keys(object),
  218. index,
  219. length,
  220. objectKey,
  221. objectValue;
  222. for (index = 0, length = objectKeyList.length; index < length; index += 1) {
  223. if (0 !== index) {
  224. _result += ', ';
  225. }
  226. objectKey = objectKeyList[index];
  227. objectValue = object[objectKey];
  228. writeNode(level, objectKey, false, false);
  229. if (result.length > 1024) {
  230. _result += '? ';
  231. }
  232. _result += result + ': ';
  233. writeNode(level, objectValue, false, false);
  234. _result += result;
  235. }
  236. tag = _tag;
  237. result = '{' + _result + '}';
  238. }
  239. function writeBlockMapping(level, object, compact) {
  240. var _result = '',
  241. _tag = tag,
  242. objectKeyList = Object.keys(object),
  243. index,
  244. length,
  245. objectKey,
  246. objectValue,
  247. explicitPair;
  248. for (index = 0, length = objectKeyList.length; index < length; index += 1) {
  249. if (!compact || 0 !== index) {
  250. _result += generateNextLine(level);
  251. }
  252. objectKey = objectKeyList[index];
  253. objectValue = object[objectKey];
  254. writeNode(level + 1, objectKey, true, true);
  255. explicitPair = (null !== tag && '?' !== tag && result.length <= 1024);
  256. if (explicitPair) {
  257. _result += '? ';
  258. }
  259. _result += result;
  260. if (explicitPair) {
  261. _result += generateNextLine(level);
  262. }
  263. writeNode(level + 1, objectValue, true, explicitPair);
  264. _result += ': ' + result;
  265. }
  266. tag = _tag;
  267. result = _result;
  268. }
  269. function detectType(object, explicit) {
  270. var _result, typeList, index, length, type, style;
  271. typeList = explicit ? explicitTypes : implicitTypes;
  272. kind = kindOf(object);
  273. for (index = 0, length = typeList.length; index < length; index += 1) {
  274. type = typeList[index];
  275. if ((null !== type.dumper) &&
  276. (null === type.dumper.kind || kind === type.dumper.kind) &&
  277. (null === type.dumper.instanceOf || object instanceof type.dumper.instanceOf) &&
  278. (null === type.dumper.predicate || type.dumper.predicate(object))) {
  279. tag = explicit ? type.tag : '?';
  280. if (null !== type.dumper.representer) {
  281. style = styleMap[type.tag] || type.dumper.defaultStyle;
  282. if ('function' === typeof type.dumper.representer) {
  283. _result = type.dumper.representer(object, style);
  284. } else if (_hasOwnProperty.call(type.dumper.representer, style)) {
  285. _result = type.dumper.representer[style](object, style);
  286. } else {
  287. throw new YAMLException('!<' + type.tag + '> tag resolver accepts not "' + style + '" style');
  288. }
  289. if (NIL !== _result) {
  290. kind = kindOf(_result);
  291. result = _result;
  292. } else {
  293. if (explicit) {
  294. throw new YAMLException('cannot represent an object of !<' + type.tag + '> type');
  295. } else {
  296. continue;
  297. }
  298. }
  299. }
  300. return true;
  301. }
  302. }
  303. return false;
  304. }
  305. function writeNode(level, object, block, compact) {
  306. tag = null;
  307. result = object;
  308. if (!detectType(object, false)) {
  309. detectType(object, true);
  310. }
  311. if (block) {
  312. block = (0 > flowLevel || flowLevel > level);
  313. }
  314. if ((null !== tag && '?' !== tag) || (2 !== indent && level > 0)) {
  315. compact = false;
  316. }
  317. if ('object' === kind) {
  318. if (block && (0 !== Object.keys(result).length)) {
  319. writeBlockMapping(level, result, compact);
  320. } else {
  321. writeFlowMapping(level, result);
  322. }
  323. } else if ('array' === kind) {
  324. if (block && (0 !== result.length)) {
  325. writeBlockSequence(level, result, compact);
  326. } else {
  327. writeFlowSequence(level, result);
  328. }
  329. } else if ('string' === kind) {
  330. if ('?' !== tag) {
  331. writeScalar(result);
  332. }
  333. } else {
  334. throw new YAMLException('unacceptabe kind of an object to dump (' + kind + ')');
  335. }
  336. if (null !== tag && '?' !== tag) {
  337. result = '!<' + tag + '> ' + result;
  338. }
  339. }
  340. writeNode(0, input, true, true);
  341. return result + '\n';
  342. }
  343. function safeDump(input, options) {
  344. return dump(input, common.extend({ schema: SAFE_SCHEMA }, options));
  345. }
  346. module.exports.dump = dump;
  347. module.exports.safeDump = safeDump;