mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-28 01:08:47 +00:00
Remove unnecessary json parse and stringify step when importing
This commit is contained in:
@@ -10,28 +10,18 @@ const INDENTS: Record<string, number | string | undefined> = {
|
||||
minified: undefined,
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
||||
let commentJson: typeof import('comment-json') | null = null
|
||||
|
||||
const FORMATS: Record<string, {
|
||||
parse: (v: string) => Promise<unknown>,
|
||||
stringify: (v: unknown, indentation: string | number | undefined) => string,
|
||||
parse: (source: string) => string,
|
||||
stringify: (source: string, indent: string | number | undefined) => string,
|
||||
}> = {
|
||||
json: {
|
||||
parse: async (v) => {
|
||||
try {
|
||||
return JSON.parse(v)
|
||||
} catch (e) {
|
||||
commentJson = await import('comment-json')
|
||||
return commentJson.parse(v)
|
||||
}
|
||||
},
|
||||
stringify: (v, i) => (commentJson ?? JSON).stringify(v, null, i) + '\n',
|
||||
parse: (s) => s,
|
||||
stringify: (s) => s,
|
||||
},
|
||||
snbt: {
|
||||
parse: async (v) => NbtTag.fromString(v).toSimplifiedJson(),
|
||||
stringify: (v, i) => {
|
||||
const tag = jsonToNbt(v)
|
||||
parse: (s) => JSON.stringify(NbtTag.fromString(s).toSimplifiedJson(), null, 2),
|
||||
stringify: (s, i) => {
|
||||
const tag = jsonToNbt(JSON.parse(s))
|
||||
if (i === undefined) {
|
||||
return tag.toString()
|
||||
}
|
||||
@@ -39,20 +29,20 @@ const FORMATS: Record<string, {
|
||||
},
|
||||
},
|
||||
yaml: {
|
||||
parse: async (v) => yaml.load(v),
|
||||
stringify: (v, i) => yaml.dump(v, {
|
||||
parse: (s) => JSON.stringify(yaml.load(s), null, 2),
|
||||
stringify: (s, i) => yaml.dump(JSON.parse(s), {
|
||||
flowLevel: i === undefined ? 0 : -1,
|
||||
indent: typeof i === 'string' ? 4 : i,
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
export function stringifySource(data: unknown, format?: string, indent?: string) {
|
||||
return FORMATS[format ?? Store.getFormat()].stringify(data, INDENTS[indent ?? Store.getIndent()])
|
||||
export function stringifySource(source: string, format?: string, indent?: string) {
|
||||
return FORMATS[format ?? Store.getFormat()].stringify(source, INDENTS[indent ?? Store.getIndent()])
|
||||
}
|
||||
|
||||
export async function parseSource(data: string, format: string) {
|
||||
return await FORMATS[format].parse(data)
|
||||
export async function parseSource(source: string, format: string) {
|
||||
return FORMATS[format].parse(source)
|
||||
}
|
||||
|
||||
export function getSourceIndent(indent: string) {
|
||||
|
||||
Reference in New Issue
Block a user