<commit_before>const {dirname} = require("path")

const fs = require("promise-fs")

const yaml = require("js-yaml")

const isNumber = require("./util/isNumber")
const isPlainObject = require("./util/isPlainObject")

const {normalizePath, normalizePathSync} = require("./util/normalizePath")

let base = process.cwd()
if (module.parent && typeof module.parent.filename === "string") {
  base = dirname(module.parent.filename)

  // eslint-disable-next-line no-underscore-dangle
  delete require.cache[__filename]
}

const DEFAULT_OPTIONS = {
  schema: yaml.DEFAULT_SAFE_SCHEMA,
  encoding: "utf8"
}

/**
 * @api private
 */
function normalizeOptions(options) {
  switch (true) {
    case isPlainObject(options):
      return {
        ...DEFAULT_OPTIONS, ...options
      }

    case options && typeof options === "string":
      return {
        ...DEFAULT_OPTIONS, encoding: options
      }

    default:
      return DEFAULT_OPTIONS
  }
}

/**
 * @api public
 */
async function read(path, options = {}) {
  const {encoding, ...opts} = normalizeOptions(options)

  if (!isNumber(path)) {
    path = await normalizePath(base, path)
  }

  const content = await fs.readFile(path, encoding)

  return yaml.load(content, opts)
}

/**
 * @api public
 */
function readSync(path, options = {}) {
  const {encoding, ...opts} = normalizeOptions(options)

  if (!isNumber(path)) {
    path = normalizePathSync(base, path)
  }

  const content = fs.readFileSync(path, encoding)

  return yaml.load(content, opts)
}

/**
 * @api public
 */
async function write(path, content, options = {}) {
  const {encoding, ...opts} = normalizeOptions(options)

  if (!isNumber(path)) {
    path = await normalizePath(base, path)
  }

  await fs.writeFile(path, yaml.dump(content, opts), encoding)
}

/**
 * @api public
 */
function writeSync(path, content, options = {}) {
  const {encoding, ...opts} = normalizeOptions(options)

  if (!isNumber(path)) {
    path = normalizePathSync(base, path)
  }

  fs.writeFileSync(path, yaml.dump(content, opts), encoding)
}

exports.read = read
exports.readSync = readSync
exports.write = write
exports.writeSync = writeSync
<commit_msg>Small improvement<commit_after>const {dirname} = require("path")

const fs = require("promise-fs")

const yaml = require("js-yaml")

const isNumber = require("./util/isNumber")
const isPlainObject = require("./util/isPlainObject")

const {normalizePath, normalizePathSync} = require("./util/normalizePath")

let base = process.cwd()
if (module.parent && typeof module.parent.filename === "string") {
  base = dirname(module.parent.filename)

  // eslint-disable-next-line no-underscore-dangle
  delete require.cache[__filename]
}

const defaults = {
  schema: yaml.DEFAULT_SAFE_SCHEMA,
  encoding: "utf8"
}

/**
 * @api private
 */
function normalizeOptions(options) {
  switch (true) {
    case isPlainObject(options):
      return {
        ...defaults, ...options
      }

    case options && typeof options === "string":
      return {
        ...defaults, encoding: options
      }

    default:
      return defaults
  }
}

/**
 * @api public
 */
async function read(path, options = {}) {
  const {encoding, ...opts} = normalizeOptions(options)

  if (!isNumber(path)) {
    path = await normalizePath(base, path)
  }

  const content = await fs.readFile(path, encoding)

  return yaml.load(content, opts)
}

/**
 * @api public
 */
function readSync(path, options = {}) {
  const {encoding, ...opts} = normalizeOptions(options)

  if (!isNumber(path)) {
    path = normalizePathSync(base, path)
  }

  const content = fs.readFileSync(path, encoding)

  return yaml.load(content, opts)
}

/**
 * @api public
 */
async function write(path, content, options = {}) {
  const {encoding, ...opts} = normalizeOptions(options)

  if (!isNumber(path)) {
    path = await normalizePath(base, path)
  }

  await fs.writeFile(path, yaml.dump(content, opts), encoding)
}

/**
 * @api public
 */
function writeSync(path, content, options = {}) {
  const {encoding, ...opts} = normalizeOptions(options)

  if (!isNumber(path)) {
    path = normalizePathSync(base, path)
  }

  fs.writeFileSync(path, yaml.dump(content, opts), encoding)
}

exports.read = read
exports.readSync = readSync
exports.write = write
exports.writeSync = writeSync

{'fraction_non_alphanumeric': 0.14939309056956115, 'fraction_numerical': 0.00046685340802987864, 'mean_word_length': 3.617456896551724, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3168860', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1345, 'n_words': 313}