loadee
TypeScript icon, indicating that this package has built-in type declarations

3.1.1 • Public • Published

loadee

A utility to simplify the loading of various types of files, including YAML, JSON, and CommonJS or ES modules. This module provides both synchronous and asynchronous functions for loading data and modules based on file extensions.

Install

You can install loadee using npm or yarn:

npm i loadee
# or
yarn add loadee

Usage

To use loadee, you can import it into your JavaScript or TypeScript project and utilize its various functions for loading files and modules.

Asynchronous Loading

loadFile<T = any>(pathlike: PathLike, cwd?: string, ...args: unknown[]): Promise<T>

Loads data from a file asynchronously based on its file extension.

  • pathlike: The path-like value representing the file.
  • cwd (optional): The current working directory. Defaults to the process's current working directory.
  • ...args (optional): Additional arguments to pass to the loaded module if it's a function.

To load data from a YAML file asynchronously:

import { loadFile } from 'loadee'

try {
  const data = await loadFile('data.yaml')
  console.log('Loaded YAML data:', data)
} catch (error) {
  console.error('Error loading YAML data:', error)
}

To load data from a JSON file asynchronously:

import { loadFile } from 'loadee'

try {
  const data = await loadFile('data.json')
  console.log('Loaded JSON data:', data)
} catch (error) {
  console.error('Error loading JSON data:', error)
}

To load a CommonJS or ES module from a JavaScript file asynchronously:

import { loadFile } from 'loadee'

try {
  const module = await loadFile('module.js')
  console.log('Loaded JavaScript module:', module)
} catch (error) {
  console.error('Error loading JavaScript module:', error)
}

Synchronous Loading

loadFileSync<T = any>(pathlike: PathLike, cwd?: string, ...args: unknown[]): T

Loads data from a file synchronously based on its file extension.

  • pathlike: The path-like value representing the file.
  • cwd (optional): The current working directory. Defaults to the process's current working directory.
  • ...args (optional): Additional arguments to pass to the loaded module if it's a function.

To load data from a YAML file synchronously:

import { loadFileSync } from 'loadee'

try {
  const data = loadFileSync('data.yaml')
  console.log('Loaded YAML data:', data)
} catch (error) {
  console.error('Error loading YAML data:', error)
}

To load data from a JSON file synchronously:

import { loadFileSync } from 'loadee'

try {
  const data = loadFileSync('data.json')
  console.log('Loaded JSON data:', data)
} catch (error) {
  console.error('Error loading JSON data:', error)
}

To load a JavaScript file synchronously:

import { loadFileSync } from 'loadee'

try {
  const module = loadFileSync('module.js')
  console.log('Loaded CommonJS:', module)
} catch (error) {
  console.error('Error loading CommonJS:', error)
}

NOTE: The loadFileSync function cannot be used to load ES module files. When you attempt to load a .js file using this function, it will be treated as a CommonJS module.

Supported File Types

loadee supports loading the following file types:

  • .yaml and .yml files: Loaded asynchronously and synchronously.
  • .json files: Loaded asynchronously and synchronously.
  • .js, .mjs, and .cjs files: Loaded asynchronously and synchronously.

Related

  • rcfy – Finds and loads runtime-configuration file for the current project with precedence.

Contributing

We 💛  issues.

When committing, please conform to the semantic-release commit standards. Please install commitizen and the adapter globally, if you have not already.

npm i -g commitizen cz-conventional-changelog

Now you can use git cz or just cz instead of git commit when committing. You can also use git-cz, which is an alias for cz.

git add . && git cz

License

GitHub

A project by Stilearning © 2021-2024.

Package Sidebar

Install

npm i loadee

Weekly Downloads

72

Version

3.1.1

License

MIT

Unpacked Size

15.2 kB

Total Files

10

Last publish

Collaborators

  • bent10