Skip to content

Commit

Permalink
fix(safe-stringify): stringify falsy values and strings (#37)
Browse files Browse the repository at this point in the history
* fix(safe-stringify): stringify falsy values and strings

* revert: do not stringify string twice
  • Loading branch information
P0lip authored Feb 18, 2020
1 parent 2712bee commit 4bb4425
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/__tests__/safeStringify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ describe('safeStringify', () => {
it('should work', () => {
const val = { foo: true };

expect(safeStringify(val)).toEqual(`foo: true
`);
expect(safeStringify(val)).toEqual('foo: true\n');
});

it('should not stringify strings twice', () => {
expect(safeStringify('foo: true')).toEqual('foo: true');
expect(safeStringify('')).toEqual('');
});

it.each([0, null, false])('should stringify falsy value: %s', value => {
expect(safeStringify(value)).toEqual(`${value}\n`);
});

it('should respect lineWidth for multi-line strings', () => {
Expand Down
7 changes: 2 additions & 5 deletions src/safeStringify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { DumpOptions, safeDump } from '@stoplight/yaml-ast-parser';

export const safeStringify = (value: any, options?: DumpOptions): string => {
if (!value || typeof value === 'string') return value;

return safeDump(value, options);
};
export const safeStringify = (value: unknown, options?: DumpOptions): string =>
typeof value === 'string' ? value : safeDump(value, options);

0 comments on commit 4bb4425

Please sign in to comment.