123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965 |
- 'use strict';
- var common = require('./common');
- var YAMLException = require('./exception');
- var DEFAULT_SCHEMA = require('./schema/default');
- var _toString = Object.prototype.toString;
- var _hasOwnProperty = Object.prototype.hasOwnProperty;
- var CHAR_BOM = 0xFEFF;
- var CHAR_TAB = 0x09;
- var CHAR_LINE_FEED = 0x0A;
- var CHAR_CARRIAGE_RETURN = 0x0D;
- var CHAR_SPACE = 0x20;
- var CHAR_EXCLAMATION = 0x21;
- var CHAR_DOUBLE_QUOTE = 0x22;
- var CHAR_SHARP = 0x23;
- var CHAR_PERCENT = 0x25;
- var CHAR_AMPERSAND = 0x26;
- var CHAR_SINGLE_QUOTE = 0x27;
- var CHAR_ASTERISK = 0x2A;
- var CHAR_COMMA = 0x2C;
- var CHAR_MINUS = 0x2D;
- var CHAR_COLON = 0x3A;
- var CHAR_EQUALS = 0x3D;
- var CHAR_GREATER_THAN = 0x3E;
- var CHAR_QUESTION = 0x3F;
- var CHAR_COMMERCIAL_AT = 0x40;
- var CHAR_LEFT_SQUARE_BRACKET = 0x5B;
- var CHAR_RIGHT_SQUARE_BRACKET = 0x5D;
- var CHAR_GRAVE_ACCENT = 0x60;
- var CHAR_LEFT_CURLY_BRACKET = 0x7B;
- var CHAR_VERTICAL_LINE = 0x7C;
- var CHAR_RIGHT_CURLY_BRACKET = 0x7D;
- var ESCAPE_SEQUENCES = {};
- ESCAPE_SEQUENCES[0x00] = '\\0';
- ESCAPE_SEQUENCES[0x07] = '\\a';
- ESCAPE_SEQUENCES[0x08] = '\\b';
- ESCAPE_SEQUENCES[0x09] = '\\t';
- ESCAPE_SEQUENCES[0x0A] = '\\n';
- ESCAPE_SEQUENCES[0x0B] = '\\v';
- ESCAPE_SEQUENCES[0x0C] = '\\f';
- ESCAPE_SEQUENCES[0x0D] = '\\r';
- ESCAPE_SEQUENCES[0x1B] = '\\e';
- ESCAPE_SEQUENCES[0x22] = '\\"';
- ESCAPE_SEQUENCES[0x5C] = '\\\\';
- ESCAPE_SEQUENCES[0x85] = '\\N';
- ESCAPE_SEQUENCES[0xA0] = '\\_';
- ESCAPE_SEQUENCES[0x2028] = '\\L';
- ESCAPE_SEQUENCES[0x2029] = '\\P';
- var DEPRECATED_BOOLEANS_SYNTAX = [
- 'y', 'Y', 'yes', 'Yes', 'YES', 'on', 'On', 'ON',
- 'n', 'N', 'no', 'No', 'NO', 'off', 'Off', 'OFF'
- ];
- var DEPRECATED_BASE60_SYNTAX = /^[-+]?[0-9_]+(?::[0-9_]+)+(?:\.[0-9_]*)?$/;
- function compileStyleMap(schema, map) {
- var result, keys, index, length, tag, style, type;
- if (map === null) return {};
- result = {};
- keys = Object.keys(map);
- for (index = 0, length = keys.length; index < length; index += 1) {
- tag = keys[index];
- style = String(map[tag]);
- if (tag.slice(0, 2) === '!!') {
- tag = 'tag:yaml.org,2002:' + tag.slice(2);
- }
- type = schema.compiledTypeMap['fallback'][tag];
- if (type && _hasOwnProperty.call(type.styleAliases, style)) {
- style = type.styleAliases[style];
- }
- result[tag] = style;
- }
- return result;
- }
- function encodeHex(character) {
- var string, handle, length;
- string = character.toString(16).toUpperCase();
- if (character <= 0xFF) {
- handle = 'x';
- length = 2;
- } else if (character <= 0xFFFF) {
- handle = 'u';
- length = 4;
- } else if (character <= 0xFFFFFFFF) {
- handle = 'U';
- length = 8;
- } else {
- throw new YAMLException('code point within a string may not be greater than 0xFFFFFFFF');
- }
- return '\\' + handle + common.repeat('0', length - string.length) + string;
- }
- var QUOTING_TYPE_SINGLE = 1,
- QUOTING_TYPE_DOUBLE = 2;
- function State(options) {
- this.schema = options['schema'] || DEFAULT_SCHEMA;
- this.indent = Math.max(1, (options['indent'] || 2));
- this.noArrayIndent = options['noArrayIndent'] || false;
- this.skipInvalid = options['skipInvalid'] || false;
- this.flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel']);
- this.styleMap = compileStyleMap(this.schema, options['styles'] || null);
- this.sortKeys = options['sortKeys'] || false;
- this.lineWidth = options['lineWidth'] || 80;
- this.noRefs = options['noRefs'] || false;
- this.noCompatMode = options['noCompatMode'] || false;
- this.condenseFlow = options['condenseFlow'] || false;
- this.quotingType = options['quotingType'] === '"' ? QUOTING_TYPE_DOUBLE : QUOTING_TYPE_SINGLE;
- this.forceQuotes = options['forceQuotes'] || false;
- this.replacer = typeof options['replacer'] === 'function' ? options['replacer'] : null;
- this.implicitTypes = this.schema.compiledImplicit;
- this.explicitTypes = this.schema.compiledExplicit;
- this.tag = null;
- this.result = '';
- this.duplicates = [];
- this.usedDuplicates = null;
- }
- function indentString(string, spaces) {
- var ind = common.repeat(' ', spaces),
- position = 0,
- next = -1,
- result = '',
- line,
- length = string.length;
- while (position < length) {
- next = string.indexOf('\n', position);
- if (next === -1) {
- line = string.slice(position);
- position = length;
- } else {
- line = string.slice(position, next + 1);
- position = next + 1;
- }
- if (line.length && line !== '\n') result += ind;
- result += line;
- }
- return result;
- }
- function generateNextLine(state, level) {
- return '\n' + common.repeat(' ', state.indent * level);
- }
- function testImplicitResolving(state, str) {
- var index, length, type;
- for (index = 0, length = state.implicitTypes.length; index < length; index += 1) {
- type = state.implicitTypes[index];
- if (type.resolve(str)) {
- return true;
- }
- }
- return false;
- }
- function isWhitespace(c) {
- return c === CHAR_SPACE || c === CHAR_TAB;
- }
- function isPrintable(c) {
- return (0x00020 <= c && c <= 0x00007E)
- || ((0x000A1 <= c && c <= 0x00D7FF) && c !== 0x2028 && c !== 0x2029)
- || ((0x0E000 <= c && c <= 0x00FFFD) && c !== CHAR_BOM)
- || (0x10000 <= c && c <= 0x10FFFF);
- }
- function isNsCharOrWhitespace(c) {
- return isPrintable(c)
- && c !== CHAR_BOM
-
- && c !== CHAR_CARRIAGE_RETURN
- && c !== CHAR_LINE_FEED;
- }
- function isPlainSafe(c, prev, inblock) {
- var cIsNsCharOrWhitespace = isNsCharOrWhitespace(c);
- var cIsNsChar = cIsNsCharOrWhitespace && !isWhitespace(c);
- return (
-
- inblock ?
- cIsNsCharOrWhitespace
- : cIsNsCharOrWhitespace
-
- && c !== CHAR_COMMA
- && c !== CHAR_LEFT_SQUARE_BRACKET
- && c !== CHAR_RIGHT_SQUARE_BRACKET
- && c !== CHAR_LEFT_CURLY_BRACKET
- && c !== CHAR_RIGHT_CURLY_BRACKET
- )
-
- && c !== CHAR_SHARP
- && !(prev === CHAR_COLON && !cIsNsChar)
- || (isNsCharOrWhitespace(prev) && !isWhitespace(prev) && c === CHAR_SHARP)
- || (prev === CHAR_COLON && cIsNsChar);
- }
- function isPlainSafeFirst(c) {
-
-
-
- return isPrintable(c) && c !== CHAR_BOM
- && !isWhitespace(c)
-
-
- && c !== CHAR_MINUS
- && c !== CHAR_QUESTION
- && c !== CHAR_COLON
- && c !== CHAR_COMMA
- && c !== CHAR_LEFT_SQUARE_BRACKET
- && c !== CHAR_RIGHT_SQUARE_BRACKET
- && c !== CHAR_LEFT_CURLY_BRACKET
- && c !== CHAR_RIGHT_CURLY_BRACKET
-
- && c !== CHAR_SHARP
- && c !== CHAR_AMPERSAND
- && c !== CHAR_ASTERISK
- && c !== CHAR_EXCLAMATION
- && c !== CHAR_VERTICAL_LINE
- && c !== CHAR_EQUALS
- && c !== CHAR_GREATER_THAN
- && c !== CHAR_SINGLE_QUOTE
- && c !== CHAR_DOUBLE_QUOTE
-
- && c !== CHAR_PERCENT
- && c !== CHAR_COMMERCIAL_AT
- && c !== CHAR_GRAVE_ACCENT;
- }
- function isPlainSafeLast(c) {
-
- return !isWhitespace(c) && c !== CHAR_COLON;
- }
- function codePointAt(string, pos) {
- var first = string.charCodeAt(pos), second;
- if (first >= 0xD800 && first <= 0xDBFF && pos + 1 < string.length) {
- second = string.charCodeAt(pos + 1);
- if (second >= 0xDC00 && second <= 0xDFFF) {
-
- return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
- }
- }
- return first;
- }
- function needIndentIndicator(string) {
- var leadingSpaceRe = /^\n* /;
- return leadingSpaceRe.test(string);
- }
- var STYLE_PLAIN = 1,
- STYLE_SINGLE = 2,
- STYLE_LITERAL = 3,
- STYLE_FOLDED = 4,
- STYLE_DOUBLE = 5;
- function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth,
- testAmbiguousType, quotingType, forceQuotes, inblock) {
- var i;
- var char = 0;
- var prevChar = null;
- var hasLineBreak = false;
- var hasFoldableLine = false;
- var shouldTrackWidth = lineWidth !== -1;
- var previousLineBreak = -1;
- var plain = isPlainSafeFirst(codePointAt(string, 0))
- && isPlainSafeLast(codePointAt(string, string.length - 1));
- if (singleLineOnly || forceQuotes) {
-
-
- for (i = 0; i < string.length; char >= 0x10000 ? i += 2 : i++) {
- char = codePointAt(string, i);
- if (!isPrintable(char)) {
- return STYLE_DOUBLE;
- }
- plain = plain && isPlainSafe(char, prevChar, inblock);
- prevChar = char;
- }
- } else {
-
- for (i = 0; i < string.length; char >= 0x10000 ? i += 2 : i++) {
- char = codePointAt(string, i);
- if (char === CHAR_LINE_FEED) {
- hasLineBreak = true;
-
- if (shouldTrackWidth) {
- hasFoldableLine = hasFoldableLine ||
-
- (i - previousLineBreak - 1 > lineWidth &&
- string[previousLineBreak + 1] !== ' ');
- previousLineBreak = i;
- }
- } else if (!isPrintable(char)) {
- return STYLE_DOUBLE;
- }
- plain = plain && isPlainSafe(char, prevChar, inblock);
- prevChar = char;
- }
-
- hasFoldableLine = hasFoldableLine || (shouldTrackWidth &&
- (i - previousLineBreak - 1 > lineWidth &&
- string[previousLineBreak + 1] !== ' '));
- }
-
-
-
- if (!hasLineBreak && !hasFoldableLine) {
-
-
- if (plain && !forceQuotes && !testAmbiguousType(string)) {
- return STYLE_PLAIN;
- }
- return quotingType === QUOTING_TYPE_DOUBLE ? STYLE_DOUBLE : STYLE_SINGLE;
- }
-
- if (indentPerLevel > 9 && needIndentIndicator(string)) {
- return STYLE_DOUBLE;
- }
-
-
- if (!forceQuotes) {
- return hasFoldableLine ? STYLE_FOLDED : STYLE_LITERAL;
- }
- return quotingType === QUOTING_TYPE_DOUBLE ? STYLE_DOUBLE : STYLE_SINGLE;
- }
- function writeScalar(state, string, level, iskey, inblock) {
- state.dump = (function () {
- if (string.length === 0) {
- return state.quotingType === QUOTING_TYPE_DOUBLE ? '""' : "''";
- }
- if (!state.noCompatMode) {
- if (DEPRECATED_BOOLEANS_SYNTAX.indexOf(string) !== -1 || DEPRECATED_BASE60_SYNTAX.test(string)) {
- return state.quotingType === QUOTING_TYPE_DOUBLE ? ('"' + string + '"') : ("'" + string + "'");
- }
- }
- var indent = state.indent * Math.max(1, level);
-
-
-
-
-
-
-
- var lineWidth = state.lineWidth === -1
- ? -1 : Math.max(Math.min(state.lineWidth, 40), state.lineWidth - indent);
-
- var singleLineOnly = iskey
-
- || (state.flowLevel > -1 && level >= state.flowLevel);
- function testAmbiguity(string) {
- return testImplicitResolving(state, string);
- }
- switch (chooseScalarStyle(string, singleLineOnly, state.indent, lineWidth,
- testAmbiguity, state.quotingType, state.forceQuotes && !iskey, inblock)) {
- case STYLE_PLAIN:
- return string;
- case STYLE_SINGLE:
- return "'" + string.replace(/'/g, "''") + "'";
- case STYLE_LITERAL:
- return '|' + blockHeader(string, state.indent)
- + dropEndingNewline(indentString(string, indent));
- case STYLE_FOLDED:
- return '>' + blockHeader(string, state.indent)
- + dropEndingNewline(indentString(foldString(string, lineWidth), indent));
- case STYLE_DOUBLE:
- return '"' + escapeString(string, lineWidth) + '"';
- default:
- throw new YAMLException('impossible error: invalid scalar style');
- }
- }());
- }
- function blockHeader(string, indentPerLevel) {
- var indentIndicator = needIndentIndicator(string) ? String(indentPerLevel) : '';
-
- var clip = string[string.length - 1] === '\n';
- var keep = clip && (string[string.length - 2] === '\n' || string === '\n');
- var chomp = keep ? '+' : (clip ? '' : '-');
- return indentIndicator + chomp + '\n';
- }
- function dropEndingNewline(string) {
- return string[string.length - 1] === '\n' ? string.slice(0, -1) : string;
- }
- function foldString(string, width) {
-
-
-
-
- var lineRe = /(\n+)([^\n]*)/g;
-
- var result = (function () {
- var nextLF = string.indexOf('\n');
- nextLF = nextLF !== -1 ? nextLF : string.length;
- lineRe.lastIndex = nextLF;
- return foldLine(string.slice(0, nextLF), width);
- }());
-
- var prevMoreIndented = string[0] === '\n' || string[0] === ' ';
- var moreIndented;
-
- var match;
- while ((match = lineRe.exec(string))) {
- var prefix = match[1], line = match[2];
- moreIndented = (line[0] === ' ');
- result += prefix
- + (!prevMoreIndented && !moreIndented && line !== ''
- ? '\n' : '')
- + foldLine(line, width);
- prevMoreIndented = moreIndented;
- }
- return result;
- }
- function foldLine(line, width) {
- if (line === '' || line[0] === ' ') return line;
-
- var breakRe = / [^ ]/g;
- var match;
-
- var start = 0, end, curr = 0, next = 0;
- var result = '';
-
-
-
-
- while ((match = breakRe.exec(line))) {
- next = match.index;
-
- if (next - start > width) {
- end = (curr > start) ? curr : next;
- result += '\n' + line.slice(start, end);
-
- start = end + 1;
- }
- curr = next;
- }
-
-
- result += '\n';
-
- if (line.length - start > width && curr > start) {
- result += line.slice(start, curr) + '\n' + line.slice(curr + 1);
- } else {
- result += line.slice(start);
- }
- return result.slice(1);
- }
- function escapeString(string) {
- var result = '';
- var char = 0;
- var escapeSeq;
- for (var i = 0; i < string.length; char >= 0x10000 ? i += 2 : i++) {
- char = codePointAt(string, i);
- escapeSeq = ESCAPE_SEQUENCES[char];
- if (!escapeSeq && isPrintable(char)) {
- result += string[i];
- if (char >= 0x10000) result += string[i + 1];
- } else {
- result += escapeSeq || encodeHex(char);
- }
- }
- return result;
- }
- function writeFlowSequence(state, level, object) {
- var _result = '',
- _tag = state.tag,
- index,
- length,
- value;
- for (index = 0, length = object.length; index < length; index += 1) {
- value = object[index];
- if (state.replacer) {
- value = state.replacer.call(object, String(index), value);
- }
-
- if (writeNode(state, level, value, false, false) ||
- (typeof value === 'undefined' &&
- writeNode(state, level, null, false, false))) {
- if (_result !== '') _result += ',' + (!state.condenseFlow ? ' ' : '');
- _result += state.dump;
- }
- }
- state.tag = _tag;
- state.dump = '[' + _result + ']';
- }
- function writeBlockSequence(state, level, object, compact) {
- var _result = '',
- _tag = state.tag,
- index,
- length,
- value;
- for (index = 0, length = object.length; index < length; index += 1) {
- value = object[index];
- if (state.replacer) {
- value = state.replacer.call(object, String(index), value);
- }
-
- if (writeNode(state, level + 1, value, true, true, false, true) ||
- (typeof value === 'undefined' &&
- writeNode(state, level + 1, null, true, true, false, true))) {
- if (!compact || _result !== '') {
- _result += generateNextLine(state, level);
- }
- if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
- _result += '-';
- } else {
- _result += '- ';
- }
- _result += state.dump;
- }
- }
- state.tag = _tag;
- state.dump = _result || '[]';
- }
- function writeFlowMapping(state, level, object) {
- var _result = '',
- _tag = state.tag,
- objectKeyList = Object.keys(object),
- index,
- length,
- objectKey,
- objectValue,
- pairBuffer;
- for (index = 0, length = objectKeyList.length; index < length; index += 1) {
- pairBuffer = '';
- if (_result !== '') pairBuffer += ', ';
- if (state.condenseFlow) pairBuffer += '"';
- objectKey = objectKeyList[index];
- objectValue = object[objectKey];
- if (state.replacer) {
- objectValue = state.replacer.call(object, objectKey, objectValue);
- }
- if (!writeNode(state, level, objectKey, false, false)) {
- continue;
- }
- if (state.dump.length > 1024) pairBuffer += '? ';
- pairBuffer += state.dump + (state.condenseFlow ? '"' : '') + ':' + (state.condenseFlow ? '' : ' ');
- if (!writeNode(state, level, objectValue, false, false)) {
- continue;
- }
- pairBuffer += state.dump;
-
- _result += pairBuffer;
- }
- state.tag = _tag;
- state.dump = '{' + _result + '}';
- }
- function writeBlockMapping(state, level, object, compact) {
- var _result = '',
- _tag = state.tag,
- objectKeyList = Object.keys(object),
- index,
- length,
- objectKey,
- objectValue,
- explicitPair,
- pairBuffer;
-
- if (state.sortKeys === true) {
-
- objectKeyList.sort();
- } else if (typeof state.sortKeys === 'function') {
-
- objectKeyList.sort(state.sortKeys);
- } else if (state.sortKeys) {
-
- throw new YAMLException('sortKeys must be a boolean or a function');
- }
- for (index = 0, length = objectKeyList.length; index < length; index += 1) {
- pairBuffer = '';
- if (!compact || _result !== '') {
- pairBuffer += generateNextLine(state, level);
- }
- objectKey = objectKeyList[index];
- objectValue = object[objectKey];
- if (state.replacer) {
- objectValue = state.replacer.call(object, objectKey, objectValue);
- }
- if (!writeNode(state, level + 1, objectKey, true, true, true)) {
- continue;
- }
- explicitPair = (state.tag !== null && state.tag !== '?') ||
- (state.dump && state.dump.length > 1024);
- if (explicitPair) {
- if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
- pairBuffer += '?';
- } else {
- pairBuffer += '? ';
- }
- }
- pairBuffer += state.dump;
- if (explicitPair) {
- pairBuffer += generateNextLine(state, level);
- }
- if (!writeNode(state, level + 1, objectValue, true, explicitPair)) {
- continue;
- }
- if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
- pairBuffer += ':';
- } else {
- pairBuffer += ': ';
- }
- pairBuffer += state.dump;
-
- _result += pairBuffer;
- }
- state.tag = _tag;
- state.dump = _result || '{}';
- }
- function detectType(state, object, explicit) {
- var _result, typeList, index, length, type, style;
- typeList = explicit ? state.explicitTypes : state.implicitTypes;
- for (index = 0, length = typeList.length; index < length; index += 1) {
- type = typeList[index];
- if ((type.instanceOf || type.predicate) &&
- (!type.instanceOf || ((typeof object === 'object') && (object instanceof type.instanceOf))) &&
- (!type.predicate || type.predicate(object))) {
- if (explicit) {
- if (type.multi && type.representName) {
- state.tag = type.representName(object);
- } else {
- state.tag = type.tag;
- }
- } else {
- state.tag = '?';
- }
- if (type.represent) {
- style = state.styleMap[type.tag] || type.defaultStyle;
- if (_toString.call(type.represent) === '[object Function]') {
- _result = type.represent(object, style);
- } else if (_hasOwnProperty.call(type.represent, style)) {
- _result = type.represent[style](object, style);
- } else {
- throw new YAMLException('!<' + type.tag + '> tag resolver accepts not "' + style + '" style');
- }
- state.dump = _result;
- }
- return true;
- }
- }
- return false;
- }
- function writeNode(state, level, object, block, compact, iskey, isblockseq) {
- state.tag = null;
- state.dump = object;
- if (!detectType(state, object, false)) {
- detectType(state, object, true);
- }
- var type = _toString.call(state.dump);
- var inblock = block;
- var tagStr;
- if (block) {
- block = (state.flowLevel < 0 || state.flowLevel > level);
- }
- var objectOrArray = type === '[object Object]' || type === '[object Array]',
- duplicateIndex,
- duplicate;
- if (objectOrArray) {
- duplicateIndex = state.duplicates.indexOf(object);
- duplicate = duplicateIndex !== -1;
- }
- if ((state.tag !== null && state.tag !== '?') || duplicate || (state.indent !== 2 && level > 0)) {
- compact = false;
- }
- if (duplicate && state.usedDuplicates[duplicateIndex]) {
- state.dump = '*ref_' + duplicateIndex;
- } else {
- if (objectOrArray && duplicate && !state.usedDuplicates[duplicateIndex]) {
- state.usedDuplicates[duplicateIndex] = true;
- }
- if (type === '[object Object]') {
- if (block && (Object.keys(state.dump).length !== 0)) {
- writeBlockMapping(state, level, state.dump, compact);
- if (duplicate) {
- state.dump = '&ref_' + duplicateIndex + state.dump;
- }
- } else {
- writeFlowMapping(state, level, state.dump);
- if (duplicate) {
- state.dump = '&ref_' + duplicateIndex + ' ' + state.dump;
- }
- }
- } else if (type === '[object Array]') {
- if (block && (state.dump.length !== 0)) {
- if (state.noArrayIndent && !isblockseq && level > 0) {
- writeBlockSequence(state, level - 1, state.dump, compact);
- } else {
- writeBlockSequence(state, level, state.dump, compact);
- }
- if (duplicate) {
- state.dump = '&ref_' + duplicateIndex + state.dump;
- }
- } else {
- writeFlowSequence(state, level, state.dump);
- if (duplicate) {
- state.dump = '&ref_' + duplicateIndex + ' ' + state.dump;
- }
- }
- } else if (type === '[object String]') {
- if (state.tag !== '?') {
- writeScalar(state, state.dump, level, iskey, inblock);
- }
- } else if (type === '[object Undefined]') {
- return false;
- } else {
- if (state.skipInvalid) return false;
- throw new YAMLException('unacceptable kind of an object to dump ' + type);
- }
- if (state.tag !== null && state.tag !== '?') {
-
-
-
-
-
-
-
-
-
-
-
-
-
- tagStr = encodeURI(
- state.tag[0] === '!' ? state.tag.slice(1) : state.tag
- ).replace(/!/g, '%21');
- if (state.tag[0] === '!') {
- tagStr = '!' + tagStr;
- } else if (tagStr.slice(0, 18) === 'tag:yaml.org,2002:') {
- tagStr = '!!' + tagStr.slice(18);
- } else {
- tagStr = '!<' + tagStr + '>';
- }
- state.dump = tagStr + ' ' + state.dump;
- }
- }
- return true;
- }
- function getDuplicateReferences(object, state) {
- var objects = [],
- duplicatesIndexes = [],
- index,
- length;
- inspectNode(object, objects, duplicatesIndexes);
- for (index = 0, length = duplicatesIndexes.length; index < length; index += 1) {
- state.duplicates.push(objects[duplicatesIndexes[index]]);
- }
- state.usedDuplicates = new Array(length);
- }
- function inspectNode(object, objects, duplicatesIndexes) {
- var objectKeyList,
- index,
- length;
- if (object !== null && typeof object === 'object') {
- index = objects.indexOf(object);
- if (index !== -1) {
- if (duplicatesIndexes.indexOf(index) === -1) {
- duplicatesIndexes.push(index);
- }
- } else {
- objects.push(object);
- if (Array.isArray(object)) {
- for (index = 0, length = object.length; index < length; index += 1) {
- inspectNode(object[index], objects, duplicatesIndexes);
- }
- } else {
- objectKeyList = Object.keys(object);
- for (index = 0, length = objectKeyList.length; index < length; index += 1) {
- inspectNode(object[objectKeyList[index]], objects, duplicatesIndexes);
- }
- }
- }
- }
- }
- function dump(input, options) {
- options = options || {};
- var state = new State(options);
- if (!state.noRefs) getDuplicateReferences(input, state);
- var value = input;
- if (state.replacer) {
- value = state.replacer.call({ '': value }, '', value);
- }
- if (writeNode(state, 0, value, true, true)) return state.dump + '\n';
- return '';
- }
- module.exports.dump = dump;
|