Setup Manager — The interface to create JSON configurations within code
Simple API to extend and generate deep nested hash objects in JavaScript.
Installation
To install Setup Manager, simply:
$ npm install --save setup-manager
API
constructor(/* object */ source)
Constructor can receive an optional object to clone it and use as a base.
var ;var manager = constructor(source);
It returns an object with the list of functions
manager.set(/* string | object */ name, value)
It sets the dot-separated property with passed value
to the result object.
It has several uncommon features:
- Extends exists object in specified property with value's properties if
value
is an object. - Concatenates exists array in specified property if
value
is an array. - Uses
name
to extend core object ifname
is an object. - Returns setter-function if
value
is not defined. This function can be called with value to set the property defined inset
method.
.set("name", "value").setup();// Returns: { "name": "value" } .set("name", { prop: "value" });// Returns: { "name": { "prop": "value" }} .set("name.prop", "value").setup();// Returns: { "name": { "prop": "value" }} .set("name.prop1", "value1") .set("name.prop2", "value2") .setup();// Returns: { "name": { "prop1": "value1", "prop2": "value2" }} var config = ;var builder = config.set("list");builder([ 7, 13 ])([ 42 ]);config.setup();// Returns: { list: [ 7, 13, 42 ] }
manager.get(/* string */ name)
It returns the value of specified property
.set("user.name", "John").get("user.name");// Returns: "John"
manager.setup()
It returns the object with all sets applied to it
.set("user.name", "Alice") .set("user.email", "alice@setup-manager.npm") .setup(); // Returns: { "user": { "name": "Alice", "email": "alice@setup-manager.npm" }}
Usage
To create the plain list:
var session = ; session;session; moduleexports = session; /* Will return: * { * "username": "John Mora", * "email": "john.mora@setup-manager.npm" * } */
To create the nested object:
var server = ; server;server;server; moduleexports = server; /* Will return: * { * "name": "slave-01", * "host": { * "domain": "setup-manager.npm", * "port": 7364 * } * } */
To extend the object:
// default.config.jsvar config = ; config;config; moduleexports = config;
// local.config.jsvar base_configuration = ;var config = base_configuration; config;config; moduleexports = config; /* Will return: * { * "env": "development", * "host": { * "protocol": "https", * "host": "localhost", * "port": 3080 * } * } */
To create the complex configuration:
var SuperPlugin = ;var base_configuration = ;var config = base_configuration; var setAlias = config;var setPlugins = config; { var alias = {}; aliasname = source; ;} { ;} // set vendor aliases;; // setup an extra plugin;; moduleexports = config; /* Will return: * { * "plugins": [ SuperPlugin { mode: "unsafe" } ], * "resolve": { * "alias": { * "modue": "./modules", * "style": "./styles", * "super": "./node_modules/SuperPlugin/vendors" * } * } */