Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bent10/loadee
A utility to simplify the loading of YAML, JSON, and JS files.
https://github.com/bent10/loadee
config js json load loader loading rc util yaml yml
Last synced: 4 months ago
JSON representation
A utility to simplify the loading of YAML, JSON, and JS files.
- Host: GitHub
- URL: https://github.com/bent10/loadee
- Owner: bent10
- License: mit
- Created: 2022-02-28T09:14:48.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-09-22T06:22:21.000Z (5 months ago)
- Last Synced: 2024-10-01T05:41:51.591Z (5 months ago)
- Topics: config, js, json, load, loader, loading, rc, util, yaml, yml
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/loadee
- Size: 876 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
- License: license
Awesome Lists containing this project
README
# 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:
```bash
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(pathlike: PathLike, cwd?: string, ...args: unknown[]): Promise`
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:
```js
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:
```js
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:
```js
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(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:
```js
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:
```js
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:
```js
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`](https://github.com/bent10/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](https://www.conventionalcommits.org/). Please install `commitizen` and the adapter globally, if you have not already.
```bash
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`.
```bash
git add . && git cz
```## License
![GitHub](https://img.shields.io/github/license/bent10/loadee)
A project by [Stilearning](https://stilearning.com) © 2021-2024.