# preference

> Load various config files(`yaml`, `json`, `toml`, `ini`) and directory into one object. support Javascript(& Typescript).

Latest version **1.1.0** (published 2018-07-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install preference
pnpm add preference
yarn add preference
bun add preference
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2018-07-11 |
| First published | 2017-11-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 58.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Cris |
| Maintainers | corgidisco |
| Keywords | config, conf, yaml, ini, toml, json, dotenv, env, typescript |

## Links

- npm: https://www.npmjs.com/package/preference
- Repository: https://github.com/corgidisco/preference
- npm.io page: https://npm.io/package/preference

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.0 (latest) — 2018-07-11
- 1.0.1 — 2018-07-03
- 1.0.0 — 2018-05-27
- 0.2.0 — 2018-05-17
- 0.1.1 — 2018-05-03
- 0.1.0 — 2018-05-03
- 0.0.10 — 2017-11-10
- 0.0.9 — 2017-11-10
- 0.0.8 — 2017-11-10
- 0.0.2 — 2017-11-08

## README

# ⚙️ Preference

[![Build](https://travis-ci.org/corgidisco/preference.svg?branch=master)](https://travis-ci.org/corgidisco/preference)
[![Downloads](https://img.shields.io/npm/dt/preference.svg)](https://npmcharts.com/compare/preference?minimal=true)
[![Version](https://img.shields.io/npm/v/preference.svg)](https://www.npmjs.com/package/preference)
[![License](https://img.shields.io/npm/l/preference.svg)](https://www.npmjs.com/package/preference)

[![NPM](https://nodei.co/npm/preference.png)](https://www.npmjs.com/package/preference)

Load various config files(`yaml`, `json`, `toml`, `ini`) and directory into one object. support Javascript(& Typescript).

## Install

```
npm install preference --save
```

## Usage

import library,

```ts
const preference = require("preference") 
// or
import * as preference from "preference" // typescript
```

then, use like this:

```js
// promise
preference.load("./your_config_directory").then(/* ... */)
await preference.load("./your_config_directory") // you can use promise by await

// sync
preference.loadSync("./your_config_directory")
```

## Examples

Example with `dotenv`.

[Example Directory](https://github.com/corgidisco/preference/tree/master/test/stubs/service)

**Code**

```js
const path = require("path")
const dotenv = require("dotenv") // if you want to use dotenv
const preference = require("preference")

dotenv.config({
  path: path.resolve(process.cwd(), "config/.env")
})
preference.load(path.resolve(process.cwd(), "config")).then(config => {
  console.log(config) // output
})
```

**Output**

```json
{
  "cache": {
    "default": {
      "username": "cache",
      "password": "cache123"
    }
  },
  "client": {
    "api": {
      "host": "127.0.0.1",
      "port": "8080",
      "middleware": [
        "cors",
        "auth"
      ]
    }
  },
  "database": {
    "keyvalue": {
      "host": "localhost",
      "port": 6379
    },
    "master": {
      "host": "localhost",
      "username": "master",
      "password": "master123"
    },
    "slave": {
      "host": "slavehost",
      "username": "slave",
      "password": "slave123"
    }
  }
}
```

## Support Formats

- `js` (built-in)
- `json` (built-in)
- `ini`, `cfg`, `conf` (require `npm install ini --save`)
- `yaml` (require `npm install js-yaml --save`)
- `toml` (require `npm install toml --save`)

## Configs

```typescript
preference.create(/* preference.PreferenceConfig */)
```

option         | type                 | default
-------------- | -------------------- | ------------------------------------------------
noIgnoreErrors | boolean              | `false`
loaders        | preference.Loader[]  | `[YamlLoader, JsonLoader, TomlLoader, IniLoader, JsLoader]`

## Custom Loader

```ts
const customLoader: preference.Loader = {
  test(filename: string): boolean {
    return /\.json$/i.test(filename)
  },
  async load(dirname: string): Promise<any> {
    return {message: "load async", dirname}
  },
  loadSync(dirname: string): any {
    return {message: "load sync", dirname}
  },
}

const pref = preference.create({
  loaders: [
    customLoader,
    new preference.YamlLoader(),
  ],
})
```

---
_Source: https://npm.io/package/preference · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
