superconf
Smart configuration loader
Last updated 4 years ago by andifeind .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install superconf 
SYNC missed versions from official npm registry.

Superconf

Build Status

Superconf is a smart configuration loader for Node.js. It supports json, cson, yaml, .rc or package.json config formats.

Usage

const superconf = require('superconf');
const conf = superconf('myconf');

This command loads a configuration from current working dir. Superconf tries to load configurations in this order:

  • ${name}.json
  • ${name}.cson
  • ${name}.yaml
  • ${name}.yml
  • .${name}rc
  • package.json (returns ${name} property)

Options

files overwrites the files array

const opts = {
  files: ['config/development.json', 'config/development.yml']
}

const conf = superconf('myconf', opts)

cwd set the current working dir

const opts = {
  cwd: `${process.cwd()}/config`
}

const conf = superconf('myconf', opts)

Merge configs

Superconf comes with a merge method. Morge is similar to Object.assign() or lodash.extend(). Its a configurable merge method which merges multiple configurations together.

This method was implemented because Object.assign or lodash.extend handle undefined or null properties differently.

static merge(left, right[, ...args])

const left = {
  fruit: 'Apple',
  vegetable: 'Carrot'
};

const right = {
  fruit: 'Banana',
  vegetable: undefined
};

const assgned = Object.assign({}, left, right);
// assgned === {
//   fruit: 'Banana'.
//   vegetable: undefined
// }

const conf = superconf.merge(left, right);
// conf === {
//   fruit: 'Banana',
//   vegetable: 'Carrot'
// }

Configure merge

Merge behavior can be changed.

A static config() method can be used to change merge begavior.

const left = {
  fruits: {
    red: 'Apple'
  },
  vegetable: 'Carrot'
};

const right = {
  fruits: {
    yellow: 'Banana'
  },
  vegetable: undefined
};

const conf = superconf.merge(left, right);
// conf === {
//   fruits: {
//     yellow: 'Banana'
//   },
//   vegetable: 'Carrot'
// }

const conf = superconf.config({
  dept: 1
}).merge(left, right);
// conf === {
//   fruits: {
//     red: 'Apple',
//     yellow: 'Banana'
//   },
//   vegetable: 'Carrot'
// }

This will merge objects together, but only on the first level.

Copy configs

The .copy() method returns a deep copy of the input object. It traverse through all objects and arrays and creates new copies of all objects and arrays.

static copy(any obj)

const obj = {
  foo: 'foo',
  bar: {
    bla: 'bla'
  }
}

const copy = superconf.copy(obj)

obj !== copy // true
obj.bar !== copy.bar // true

Current Tags

  • 1.2.3                                ...           latest (a year ago)

12 Versions

  • 1.2.3                                ...           a year ago
  • 1.2.2                                ...           a year ago
  • 1.2.1                                ...           2 years ago
  • 1.2.0                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
  • 0.2.2                                ...           3 years ago
  • 0.2.1                                ...           4 years ago
  • 0.2.0                                ...           4 years ago
  • 0.1.1                                ...           4 years ago
  • 0.1.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 1
Last Month 2
Dependencies (2)
Dev Dependencies (2)

Copyright 2014 - 2017 © taobao.org |