obsidian-dev-utils
Version:
This is the collection of useful functions that you can use for your Obsidian plugin development
66 lines (65 loc) • 2.44 kB
text/typescript
/**
* @packageDocumentation
*
* Contains utility functions for JSON.
*/
import type { Promisable } from 'type-fest';
/**
* Options for {@link editJson}.
*/
export interface EditJsonOptions {
/**
* If true, skips editing if the file does not exist.
*/
shouldSkipIfMissing?: boolean;
}
/**
* Reads, edits, and writes back a JSON file using a provided edit function.
*
* @typeParam T - The type of the data to be edited.
* @param path - The path to the JSON file.
* @param editFn - The function to edit the parsed JSON data.
* @param options - Additional options for editing.
* @returns A {@link Promise} that resolves when the file has been edited and written.
*/
export declare function editJson<T>(path: string, editFn: (data: T) => Promisable<void>, options?: EditJsonOptions): Promise<void>;
/**
* Reads, edits, and writes back a JSON file using a provided edit function.
*
* @typeParam T - The type of the data to be edited.
* @param path - The path to the JSON file.
* @param editFn - The function to edit the parsed JSON data.
* @param options - Additional options for editing.
*/
export declare function editJsonSync<T>(path: string, editFn: (data: T) => void, options?: EditJsonOptions): void;
/**
* Reads a JSON file and parses its contents into a JavaScript object of type `T`.
*
* @typeParam T - The type to which the JSON content will be parsed.
* @param path - The path to the JSON file.
* @returns A {@link Promise} that resolves with the parsed JSON object of type `T`.
*/
export declare function readJson<T>(path: string): Promise<T>;
/**
* Reads a JSON file and parses its contents into a JavaScript object of type `T`.
*
* @typeParam T - The type to which the JSON content will be parsed.
* @param path - The path to the JSON file.
* @returns The parsed JSON object of type `T`.
*/
export declare function readJsonSync<T>(path: string): T;
/**
* Writes a JavaScript object to a JSON file.
*
* @param path - The path to the JSON file.
* @param data - The data to write to the JSON file.
* @returns A {@link Promise} that resolves when the file has been written.
*/
export declare function writeJson(path: string, data: unknown): Promise<void>;
/**
* Writes a JavaScript object to a JSON file.
*
* @param path - The path to the JSON file.
* @param data - The data to write to the JSON file.
*/
export declare function writeJsonSync(path: string, data: unknown): void;