Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "index"

Contains functions for manipulating JSON files asynchronously using the File System module from NodeJS.

Only the main functions are exported. Functions not exported are abstractions of steps from the main functions.

Functions must be passed an absolute file path to behave as expected.

Functions that requires an optional intentation level parameter don't check its value. It's used by JSON.stringify and therefore, if the value is less than 1 no formatting is done, and if it's greater than 10, the indentation is just 10.

When a function fails it returns a rejected Promise with:

  • An intance of JSONFileHandlerError if it was caused by a misuse of the function, for example, when trying to write something that is not an object to a JSON file, or when trying to read a file that doesn't exists
  • An instance of Error (actually is of SystemError, but Node doesn't exposes the class so it can't be checked using the instanceof operator) if it was caused by violating an operating system constraint, like:
    • Trying to modify a read-only file
    • Trying to create or modify a file inside a read-only directory
    • Trying to modify a file that requires elevated privileges
    • Trying to work with many files at once
    • Trying to modify a file and running out of space in the process

Index

Functions

Functions

Const join

  • join(filePath: string, jsonContent: object, indentationLevel?: number): Promise<void>
  • Joins a given content to the content of a JSON file, or creates a new one if it doesn't exists.

    Parameters

    • filePath: string

      The absolute path where the file is or will be located

    • jsonContent: object

      The object to be merged or written to the JSON file

    • Default value indentationLevel: number = 2

      How much space to use for indentation when formatting

    Returns Promise<void>

    A promise that joins the content or creates the file when resolved

Const merge

  • merge(firstFilePath: string, secondFilePath: string, mergedFilePath: string, indentationLevel?: number): Promise<void>
  • Merges the content of two JSON files into a third file, or duplicates one of the files if the other one is empty.

    Parameters

    • firstFilePath: string

      The absolute path of the first file

    • secondFilePath: string

      The absolute path of the second file

    • mergedFilePath: string

      The absolute path where the file is or will be located

    • Default value indentationLevel: number = 2

      How much space to use for indentation when formatting

    Returns Promise<void>

    A promise that merges the files, or duplicates one of them, when resolved

Const overwrite

  • overwrite(filePath: string, jsonContent: object, indentationLevel?: number): Promise<void>
  • Overwrites the content of an existing JSON file, or creates a new one if it doesn't exist.

    Parameters

    • filePath: string

      The absolute path where the file is or will be located

    • jsonContent: object

      The object to be written to the JSON file

    • Default value indentationLevel: number = 2

      How much space to use for indentation when formatting

    Returns Promise<void>

    A promise that overwrites or creates the file when resolved

Const read

  • read(filePath: string): Promise<object>
  • Returns the content of a JSON file.

    Parameters

    • filePath: string

      The absolute path where the file is located

    Returns Promise<object>

    A promise resolved with the content

Generated using TypeDoc