Skip to content

suggestion #23

Closed
Closed
@jimmywarting

Description

@jimmywarting

Would be nice if this had 0 dependencies:

import {readFileSync} from 'node:fs';
import {readFile} from 'node:fs/promises';

const parse = (uint8, options = {}) => {
  // unlike buf.toString('utf8') or or readFile(path, 'utf8'), TextDecoder will remove BOM
  let data = new TextDecoder().decode(uint8);

  if (typeof options.beforeParse === 'function') {
    data = options.beforeParse(data);
  }

  return JSON.parse(data, options.reviver);
};

export const loadJsonFile = (filePath, options) =>
  readFile(filePath).then(data => parse(data, options));

export const loadJsonFileSync = (filePath, options) =>
  parse(readFileSync(filePath), options);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions