rc9
Read/Write config couldn't be easier!
Last updated 10 days ago by pi0 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install rc9 
SYNC missed versions from official npm registry.

RC9

npm version npm downloads bundle size codecov

Read/Write RC configs couldn't be easier!

Install

Install dependencies:

# ✨ Auto-detect
npx nypm install rc9

# npm
npm install rc9

# yarn
yarn add rc9

# pnpm
pnpm add rc9

# bun
bun install rc9

# deno
deno install npm:rc9

Import utils:

ESM (Node.js, Bun, Deno)

import {
  defaults,
  parse,
  parseFile,
  read,
  readUser,
  serialize,
  write,
  writeUser,
  readUserConfig,
  writeUserConfig,
  updateUserConfig,
  update,
  updateUser,
} from "rc9";

Usage

.conf:

db.username=username
db.password=multi word password
db.enabled=true

Update config:

update({ "db.enabled": false }); // or update(..., { name: '.conf' })

Push to an array:

update({ "modules[]": "test" });

Read/Write config:

const config = read(); // or read('.conf')

// config = {
//   db: {
//     username: 'username',
//     password: 'multi word password',
//     enabled: true
//   }
// }

config.enabled = false;
write(config); // or write(config, '.conf')

User Config:

You can use readUserConfig/writeUserConfig/updateUserConfig to store config in the user's config directory ($XDG_CONFIG_HOME or ~/.config):

writeUserConfig({ token: 123 }, ".zoorc"); // Will be saved in ~/.config/.zoorc

const conf = readUserConfig(".zoorc"); // { token: 123 }

[!NOTE] readUser/writeUser/updateUser are deprecated. Use readUserConfig/writeUserConfig/updateUserConfig instead, which follow XDG conventions (~/.config).

Unflatten

RC uses flat to automatically flat/unflat when writing and reading rcfile.

It means that you can use . for keys to define objects. Some examples:

  • hello.world = true <=> { hello: { world: true }
  • test.0 = A <=> tags: [ 'A' ]

Note: If you use keys that can override like x= and x.y=, you can disable this feature by passing flat: true option.

Tip: You can use keys ending with [] to push to an array like test[]=A

Native Values

RC uses destr to convert values into native javascript values.

So reading count=123 results { count: 123 } (instead of { count: "123" }) if you want to preserve strings as is, can use count="123".

Exports

const defaults: RCOptions;
function parse(contents: string, options?: RCOptions): RC;
function parseFile(path: string, options?: RCOptions): RC;
function read(options?: RCOptions | string): RC;
function readUserConfig(options?: RCOptions | string): RC;
function serialize(config: RC): string;
function write(config: RC, options?: RCOptions | string): void;
function writeUserConfig(config: RC, options?: RCOptions | string): void;
function update(config: RC, options?: RCOptions | string): RC;
function updateUserConfig(config: RC, options?: RCOptions | string): RC;

Types:

type RC = Record<string, any>;
interface RCOptions {
  name?: string;
  dir?: string;
  flat?: boolean;
}

Defaults:

{
  name: '.conf',
  dir: process.cwd(),
  flat: false
}

Why RC9?

Be the first one to guess ????

License

Published under the MIT license. Made by community ????

Current Tags

  • 1.2.4                                ...           1x (3 years ago)
  • 3.0.1                                ...           latest (10 days ago)

22 Versions

  • 3.0.1                                ...           10 days ago
  • 3.0.0                                ...           2 months ago
  • 2.1.2                                ...           2 years ago
  • 2.1.1                                ...           3 years ago
  • 2.1.0                                ...           3 years ago
  • 2.0.1                                ...           3 years ago
  • 1.2.4                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.2.3 [deprecated]           ...           3 years ago
  • 1.2.2                                ...           4 years ago
  • 1.2.1                                ...           4 years ago
  • 1.2.0                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.0                                ...           6 years ago
  • 0.0.7                                ...           6 years ago
  • 0.0.6                                ...           6 years ago
  • 0.0.5                                ...           6 years ago
  • 0.0.4                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
  • 0.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 1
Dependencies (2)
Dev Dependencies (11)

Copyright 2013 - present © cnpmjs.org | Home |