Skip to content

Commit 9e85c7d

Browse files
committed
fix: safeStringify should not re-stringify
1 parent d910abd commit 9e85c7d

File tree

6 files changed

+1305
-864
lines changed

6 files changed

+1305
-864
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"dependencies": {
4343
"@stoplight/fast-safe-stringify": "2.1.2",
44-
"@stoplight/types": "4.0.x",
44+
"@stoplight/types": "5.x.x",
4545
"jsonc-parser": "2.1.0",
4646
"lodash": "4.x.x"
4747
},
@@ -54,7 +54,7 @@
5454
"ts-jest": "24.0.x",
5555
"tslint": "5.14.x",
5656
"tslint-config-stoplight": "1.2.x",
57-
"typescript": "3.3.4000"
57+
"typescript": "3.4.5"
5858
},
5959
"lint-staged": {
6060
"*.{ts,tsx}$": [

src/__tests__/safeStringify.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ describe('safeStringify', () => {
5555
expect(safeStringify({ value: undefined })).toBe('{}');
5656
});
5757

58-
it('should not stringify twice', () => {
59-
expect(safeStringify('"0"')).toBe('"0"');
58+
it('should not stringify something that is already a string', () => {
59+
// invalid json (for example coming from user in code editor) SHOULD not stringify again
60+
expect(safeStringify('{"foo": "bar",}')).toBe('{"foo": "bar",}');
6061
expect(safeStringify(JSON.stringify({ foo: 'bar' }))).toBe(JSON.stringify({ foo: 'bar' }));
6162
});
6263
});

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ export * from './decodePointer';
33
export * from './decodePointerFragment';
44
export * from './encodePointer';
55
export * from './encodePointerFragment';
6-
export * from './isStringified';
76
export * from './pathToPointer';
87
export * from './pointerToPath';
98
export * from './safeParse';

src/isStringified.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/safeStringify.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import fastStringify from '@stoplight/fast-safe-stringify';
2-
import { isStringified } from './isStringified';
32

43
export const safeStringify = (
54
value: any,
65
replacer?: (key: string, value: any) => any | Array<string | number> | null,
76
space?: string | number
87
): string => {
9-
if (isStringified(value)) {
8+
if (typeof value === 'string') {
109
return value;
1110
}
1211

0 commit comments

Comments
 (0)