GithubHelp home page GithubHelp logo

fleg / deep-conf Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 2do2go/deep-conf

0.0 2.0 0.0 22 KB

Configuration library for Node.js application server deployments. It lets you define a set of configs with inheritance.

License: MIT License

JavaScript 100.00%

deep-conf's Introduction

Deep-conf

Configuration library for Node.js application server deployments. It lets you define a set of configs with inheritance.

Quick Start

Install it with NPM or add it to your package.json:

$ npm install deep-conf

Then:

// Create instance by factory
var configBuilder = require('deep-conf')();

// Register new config with name "sampleConfig"
configBuilder.register({
	name: 'sampleConfig',
	config: {
		host: 'localhost',
		port: 80,
		fullName: function(config) {
			return 'http://' + config.host + ':' + config.port;
		}
	}
});

// Get registered config by name
var config = configBuilder.get('sampleConfig');

console.log(config.host, config.port, config.fullName);
// localhost 80 http://localhost:80

API

register: create new config with name and optional parent. NOTE: field info, that contains name and parentName, will be added to config, so do not add to your config field with this name, because it will be overrided.

// Register new config with name "development"
configBuilder.register({
	name: 'development',
	config: {
		host: 'localhost',
		port: 80
	}
});

// Register new config with name "production",
// inherite it from "development"
// and replace field "host" with new value
configBuilder.register({
	name: 'production',
	parent: 'development',
	config: {
		host: 'example.com'
	}
});

update: update existing config

// Update fields of existing config "development"
configBuilder.update({
	name: 'development',
	config: {
		protocol: 'https'
	}
});

get: get registered config

// Get config "production"
var config = configBuilder.get('production');

console.log(config.host, config.port);
// example.com 80

func: define in config functional field

// Register config with functional field
configBuilder.register({
	name: 'funcConfig',
	config: {
		f: configBuilder.func(function(a, b) {
			return a + b;
		})
	}
});

var config = configBuilder.get('funcConfig');

console.log(config.f(1, 2));
// 3

stringify: get registered config as string with optional space count (like JSON.stringify)

// Register config "stringConfig"
configBuilder.register({
	name: 'stringConfig',
	config: {
		host: 'localhost',
		port: 80,
		fullName: function(config) {
			return 'http://' + config.host + ':' + config.port;
		},
		sum: configBuilder.func(function(a, b) {
			var sum = a + b;
			return sum;
		})
	}
});

// Stringify config "stringConfig"
var str = configBuilder.stringify('stringConfig', 2);

console.log(str);
// {
//   "host": "localhost",
//   "port": 80,
//   "fullName": "http://localhost:80",
//   "sum": function (a, b) {var sum = a + b;return sum;}
// }

ConfigBuilder: create instance with constructor

var configBuilder = new (require('deep-conf').ConfigBuilder)();

deep-conf's People

Contributors

anton-makarov avatar artzhookov avatar fleg avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.