Skip to content

Commit

Permalink
Update packages/code-block/src/lib/formatter/jsonFormatter.ts
Browse files Browse the repository at this point in the history
Co-authored-by: Ziad Beyens <[email protected]>
  • Loading branch information
patrick-hertling and zbeyens authored Feb 1, 2025
1 parent 1acf0c7 commit 2d6c1f5
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions packages/code-block/src/lib/formatter/jsonFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import type { IFormatter } from './formatter';

export class JsonFormatter implements IFormatter {
format(code: string) {
try {
return JSON.stringify(JSON.parse(code), null, 2);
} catch (error) {
return code;
}
export const formatJson = (code: string): string => {
try {
return JSON.stringify(JSON.parse(code), null, 2);
} catch (error) {
return code;
}
};

validSyntax(code: string) {
try {
JSON.parse(code);

return true;
} catch (error) {
return false;
}
export const isValidJson = (code: string): boolean => {
try {
JSON.parse(code);
return true;
} catch (error) {
return false;
}
}
};

0 comments on commit 2d6c1f5

Please sign in to comment.