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

RC9

Read/Write config couldn't be easier!

npm version npm downloads Github Actions Codecov

Install

Install using npm or yarn:

npm i rc9
# or
yarn add rc9

Import into your Node.js project:

// CommonJS
const { read, write, update } = require('rc9')

// ESM
import { read, write, update } from 'rc9'

Usage

.conf:

db.username=db username
db.password=db pass
db.enabled=true

Update config:

update({ 'db.enabled': true }) // or update(..., { name: '.conf' })

Push to an array:

update({ 'modules[]': 'test' })

Read/Write config:

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

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

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

User Config:

It is common to keep config in user home directory (MacOS: /Users/{name}, Linux: /home/{name}, Windows: C:\users\{name})

you can use readUser/writeuser/updateUser shortcuts to quickly do this:

writeUser({ token: 123 }, '.zoorc') // Will be saved in {home}/.zoorc

const conf = readUser('.zoorc') // { token: 123 }

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 readUser(options?: RCOptions | string): RC;
function serialize(config: RC): string;
function write(config: RC, options?: RCOptions | string): void;
function writeUser(config: RC, options?: RCOptions | string): void;
function update(config: RC, options?: RCOptions | string): RC;
function updateUser(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

MIT. Made with ????

Current Tags

  • 1.2.0                                ...           latest (2 months ago)

11 Versions

  • 1.2.0                                ...           2 months ago
  • 1.1.0                                ...           2 months ago
  • 1.0.0                                ...           7 months ago
  • 0.0.7                                ...           8 months ago
  • 0.0.6                                ...           8 months ago
  • 0.0.5                                ...           8 months ago
  • 0.0.4                                ...           8 months ago
  • 0.0.3                                ...           8 months ago
  • 0.0.2                                ...           8 months ago
  • 0.0.1                                ...           8 months ago
  • 0.0.0                                ...           8 months ago
Maintainers (1)
Downloads
Today 7
This Week 983
This Month 12,363
Last Day 976
Last Week 5,320
Last Month 26,645
Dependencies (3)
Dev Dependencies (11)

Copyright 2014 - 2017 © taobao.org |