dsl-config
Generate an asynchronous DSL to generate a configuration object
Install
npm install dsl-config
Usage
Define the DSL (note that methods are chainable but have been spaced out here to accomodate comments)
const DSLConfig = ; // create an instanceconst dslConfig = ; // define some valuesdslConfigvalue'value1'value'value2'; // give defaults to valuesdslConfigvalue'value3' 'default3'value'value4' 'default4'; // and lists of valuesdslConfig; // and mappings of key/value pairsdslConfig; // mappings and lists can use distinct property// and method names in case you worry about grammardslConfig; // you can define a DSL for a value, list or mappingconst subDSLConfig = ;subDSLConfigvalue'value1'value'value2';dslConfigvalue'value5' subDSLConfig; // you can default values with DSLsconst subDSLConfigWithDefaults = ;subDSLConfigWithDefaultsvalue'value1' 'default1'value'value2' 'default2';dslConfigvalue'value6' true subDSLConfigWithDefaults; // you can reuse DSLsdslConfig; // you can clone and extend or override DSLsconst cloneDSLConfig = subDSLConfig; // override value2 to convert it to a listcloneDSLConfig; // extend with value3cloneDSLConfigvalue'value3'; dslConfigvalue'value7' cloneDSLConfig; // You can also specify anonymous key/value mappings intended// to be used as sub mappings for other mappings//// NB. the supplied name will be used for the DSL method// but the mapping keys will be used in the resulting// config object. As such anonymous mappings should be// used alone in DSLConfig instances (ie. as the only// method under a value, list or mapping) to avoid overwriting// other named values, lists or mappings. This is done so that// mappings can contain mappings without having to have// extra keys at every leveldslConfigvalue 'mappings' ); // In the same vein you can also add anonymous// sub list methods so that a list can// contain a sub list without having to create a// named key for it.//// NB. this will convert the parent config to// an array instead of an object so it won't// be possible to use it as anything other than// a listdslConfig;
The above would generate a DSL to create a configuration object with the following possible structure
value1: 'value' value2: 'value' value3: 'default3' value4: 'default4' list1: 'value' 'value' list2: 'value' 'value' mapping1: 'key1': 'value' 'key2': 'value' mapping2: 'key1': 'value' 'key2': 'value' items: 'value' 'value' settings: 'key1': 'value' 'key2': 'value' value5: value1: 'value' value2: 'value' value6: value1: 'default1' value2: 'default2' list3: value1: 'value' value2: 'value' value1: 'value' value2: 'value' value7: value1: 'value' value2: 'value' 'value' value3: 'value' mappings: 'key1': 'value' 'key2': 'value' mapping3: 'key1': 'subkey1': 'value' 'subkey2': 'value' 'key2': 'subkey1': 'value' 'subkey2': 'value' list4: 'value' 'value' 'value' 'value'
Then to synchronously create the configuration above
const config = dslConfig;
You can also use generators to asynchronously create a configuration (when asynchronous, #configure
will actually return a Promise
)
dslConfig;
Or the DSL callbacks can return promises
dslConfig;
NB. Any fields not set using #configure
will be left undefined
As you may not know whether the supplied configuration callback is going to be asynchronous or not it is advisable to call #configure
in the following way
Promise;
Even if synchronous, the preceding method will return a promise (it may already be resolved though)