-
Notifications
You must be signed in to change notification settings - Fork 785
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update packages/code-block/src/lib/formatter/formatter.ts
Co-authored-by: Ziad Beyens <[email protected]>
- Loading branch information
1 parent
2d6c1f5
commit 64c86b1
Showing
1 changed file
with
21 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,32 @@ | ||
import { JsonFormatter } from './jsonFormatter'; | ||
|
||
export interface IFormatter { | ||
format: (code: string) => string; | ||
validSyntax: (code: string) => boolean; | ||
} | ||
import { formatJson, isValidJson } from './jsonFormatter'; | ||
|
||
const supportedLanguages = new Set(['json']); | ||
|
||
export class Formatter { | ||
format(code: string, lang?: string) { | ||
if (!lang || !supportedLanguages.has(lang)) { | ||
return ''; | ||
} | ||
export const isLangSupported = (lang?: string): boolean => | ||
Boolean(lang && supportedLanguages.has(lang)); | ||
|
||
switch (lang) { | ||
case 'json': { | ||
return new JsonFormatter().format(code); | ||
} | ||
} | ||
export const formatCode = (code: string, lang?: string): string => { | ||
if (!isLangSupported(lang)) { | ||
return ''; | ||
} | ||
|
||
return code; | ||
switch (lang) { | ||
case 'json': | ||
return formatJson(code); | ||
default: | ||
return code; | ||
} | ||
}; | ||
|
||
isLangSupported(lang?: string) { | ||
return lang && supportedLanguages.has(lang); | ||
export const isValidSyntax = (code: string, lang?: string): boolean => { | ||
if (!isLangSupported(lang)) { | ||
return false; | ||
} | ||
|
||
validSyntax(code: string, lang?: string) { | ||
if (!lang || !supportedLanguages.has(lang)) { | ||
switch (lang) { | ||
case 'json': | ||
return isValidJson(code); | ||
default: | ||
return false; | ||
} | ||
|
||
switch (lang) { | ||
case 'json': { | ||
return new JsonFormatter().validSyntax(code); | ||
} | ||
} | ||
} | ||
} | ||
}; |