Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Dec 12, 2024
1 parent b337d3b commit 57d8796
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
16 changes: 14 additions & 2 deletions src/editorTable/editorTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ export async function ready() {
await updateCustomFields();
});
}

const jsonFormatOptions: y3.json.formatOptions = {
stringify: (value) => {
if (typeof value === 'bigint') {
return value.toString();
}
if (typeof value === 'number' && isFinite(value)) {
return value.toFixed(1);
}
},
};

export class EditorObject<N extends Table.NameCN = Table.NameCN> {
private _json?: y3.json.Json;
private _name?: string;
Expand All @@ -110,7 +122,7 @@ export class EditorObject<N extends Table.NameCN = Table.NameCN> {
if (!this.text) {
return undefined;
}
this._json = new y3.json.Json(this.text);
this._json = new y3.json.Json(this.text, jsonFormatOptions);
}
return this._json;
}
Expand Down Expand Up @@ -445,7 +457,7 @@ export class EditorTable<N extends Table.NameCN = Table.NameCN> extends vscode.D
templateJson = template.string;
}

let json = new y3.json.Json(templateJson);
let json = new y3.json.Json(templateJson, jsonFormatOptions);
json.set('name', y3.language.keyOf(name, true));
json.set('uid', key.toString());
json.set('key', BigInt(key));
Expand Down
42 changes: 26 additions & 16 deletions src/tools/json.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { string } from 'is';
import * as jsonc from 'jsonc-parser';

const parseOptions = {
Expand All @@ -18,9 +19,13 @@ export type Item = string | boolean | number | bigint | null | JObject | JArray;
export type JArray = Item[];
export type JObject = { [key: string]: Item };

export interface formatOptions {
stringify?: (value: any) => string | undefined;
}

export class Json {
private _text: string;
constructor(text: string) {
constructor(text: string, private options?: formatOptions) {
this._text = text;
}

Expand Down Expand Up @@ -77,20 +82,23 @@ export class Json {
}
this._tree = undefined;

let originStringify = JSON.stringify;

JSON.stringify = (value: any, replacer?: any, space?: any) => {
if (replacer) {
return originStringify(value, replacer, space);
}
if (typeof value === 'bigint') {
return value.toString();
}
if (typeof value === 'number' && isFinite(value)) {
return value.toFixed(1);
}
return originStringify(value, replacer, space);
};
let originStringify: typeof JSON.stringify | undefined;
if (this.options?.stringify) {
originStringify = JSON.stringify;
let hookedStringfy = (value: any, replacer?: any, space?: any) => {
if (replacer) {
return originStringify!(value, replacer, space);
}
JSON.stringify = originStringify!;
let result = this.options!.stringify!(value);
JSON.stringify = hookedStringfy;
if (result !== undefined) {
return result;
}
return originStringify!(value, replacer, space);
};
JSON.stringify = hookedStringfy;
}

try {
for (const key in this._patch) {
Expand All @@ -100,7 +108,9 @@ export class Json {
}
} finally {
this._patch = undefined;
JSON.stringify = originStringify;
if (originStringify) {
JSON.stringify = originStringify;
}
}
}
}
Expand Down

0 comments on commit 57d8796

Please sign in to comment.