Skip to content

Commit

Permalink
Support noRefs dump option
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Sep 26, 2019
1 parent 56a3796 commit 6301660
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/dumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ 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.noRefs = options['noRefs'] || false;

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 @@ -842,14 +844,16 @@ export interface DumpOptions {
schema?: SchemaDefinition;
/** set max line width. (default: 80) */
lineWidth?: number;
/** if `true`, don't convert duplicate objects into references (default: false) */
noRefs?: boolean;
}

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

var state = new State(options);

getDuplicateReferences(input, state);
if (!options.noRefs) getDuplicateReferences(input, state);

if (writeNode(state, 0, input, true, true)) {
return state.dump + '\n';
Expand Down
21 changes: 21 additions & 0 deletions test/dumper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,25 @@ Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, ad
`);
});
});

suite('noRefs dump option', () => {
test('should not use anchors for same objects by default', () => {
const obj = { foo: 'bar' };

expect(safeDump({ a: obj, b: obj })).to.equal(`a: &ref_0
foo: bar
b: *ref_0
`)
});

test('should not use anchors for same objects if truthy', () => {
const obj = { foo: 'bar' };

expect(safeDump({ a: obj, b: obj }, { noRefs: true })).to.equal(`a:
foo: bar
b:
foo: bar
`)
});
});
});

0 comments on commit 6301660

Please sign in to comment.