Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.

Support lineWidth dump option #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions src/dumper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@


'use strict';

import { SchemaDefinition } from "./schema";

declare function require(n:string):any
/*eslint-disable no-use-before-define*/

Expand Down Expand Up @@ -110,9 +113,10 @@ function encodeHex(character) {
return '\\' + handle + common.repeat('0', length - string.length) + string;
}

function State(options) {
function State(options: DumpOptions) {
this.schema = options['schema'] || DEFAULT_FULL_SCHEMA;
this.indent = Math.max(1, (options['indent'] || 2));
this.lineWidth = options.lineWidth !== void 0 ? options.lineWidth : 80;
this.skipInvalid = options['skipInvalid'] || false;
this.flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel']);
this.styleMap = compileStyleMap(this.schema, options['styles'] || null);
Expand Down Expand Up @@ -264,11 +268,13 @@ function writeScalar(state, object, level) {
longestLine = 0;

indent = state.indent * level;
max = 80;
if (indent < 40) {
max -= indent;
} else {
max = 40;
max = state.lineWidth;
if (isFinite(max)) {
if (indent < 40) {
max -= indent;
} else {
max = 40;
}
}

for (position = 0; position < object.length; position++) {
Expand Down Expand Up @@ -431,6 +437,10 @@ function foldLine(line, max) {
return line;
}

if (!isFinite(max)) {
return line;
}

var foldRe = /[^\s] [^\s]/g,
result = '',
prevMatch = 0,
Expand Down Expand Up @@ -817,7 +827,24 @@ function inspectNode(object, objects, duplicatesIndexes) {
}
}

export function dump(input, options) {
export interface DumpOptions {
/** indentation width to use (in spaces). */
indent?: number;
/** when true, will not add an indentation level to array elements */
noArrayIndent?: boolean;
/** do not throw on invalid types (like function in the safe schema) and skip pairs and single values with such types. */
skipInvalid?: boolean;
/** specifies level of nesting, when to switch from block to flow style for collections. -1 means block style everwhere */
flowLevel?: number;
/** Each tag may have own set of styles. - "tag" => "style" map. */
styles?: { [x: string]: any; };
/** specifies a schema to use. */
schema?: SchemaDefinition;
/** set max line width. (default: 80) */
lineWidth?: number;
}

export function dump(input, options?: DumpOptions) {
options = options || {};

var state = new State(options);
Expand All @@ -830,6 +857,6 @@ export function dump(input, options) {
return '';
}

export function safeDump(input, options) {
export function safeDump(input, options?: DumpOptions) {
return dump(input, common.extend({ schema: DEFAULT_SAFE_SCHEMA }, options));
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

export {load, loadAll, safeLoad, safeLoadAll, LoadOptions} from './loader';
export {dump, safeDump} from './dumper';
export {dump, safeDump, DumpOptions} from './dumper';

import Mark=require("./mark")
export import YAMLException = require('./exception');
Expand Down
30 changes: 30 additions & 0 deletions test/dumper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as chai from 'chai';
import { safeDump } from '../src';
const expect = chai.expect;

suite('Dumper', () => {
suite('lineWidth dump option', () => {
test('should respect lineWidth for multi-line strings', () => {
const description = `Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae
Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et.`;

expect(safeDump({ description }, { lineWidth: 100 })).to.equal(`description: >-
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,
totam rem aperiam, eaque ipsa quae

Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed
quia non numquam eius modi tempora incidunt ut labore et.
`);
});

test('should use literal block-scalar style if lineWidth is Infinity (or very lengthy)', () => {
const description = `Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae
Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et.`;

expect(safeDump({ description }, { lineWidth: Infinity })).to.equal(`description: |-
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae
Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et.
`);
});
});
});