@humanwhocodes/config-array
Advanced tools
Comparing version 0.5.0 to 0.6.0
72
api.js
@@ -127,3 +127,3 @@ 'use strict'; | ||
* found. | ||
* @returns {Array} A flattened array containing only config objects. | ||
* @returns {Promise<Array>} A flattened array containing only config objects. | ||
* @throws {TypeError} When a config function returns a function. | ||
@@ -133,4 +133,46 @@ */ | ||
// TODO: Allow async config functions | ||
async function *flatTraverse(array) { | ||
for (let item of array) { | ||
if (typeof item === 'function') { | ||
item = item(context); | ||
if (item.then) { | ||
item = await item; | ||
} | ||
} | ||
if (Array.isArray(item)) { | ||
yield * flatTraverse(item); | ||
} else if (typeof item === 'function') { | ||
throw new TypeError('A config function can only return an object or array.'); | ||
} else { | ||
yield item; | ||
} | ||
} | ||
} | ||
/* | ||
* Async iterables cannot be used with the spread operator, so we need to manually | ||
* create the array to return. | ||
*/ | ||
const asyncIterable = await flatTraverse(items); | ||
const configs = []; | ||
for await (const config of asyncIterable) { | ||
configs.push(config); | ||
} | ||
return configs; | ||
} | ||
/** | ||
* Normalizes a `ConfigArray` by flattening it and executing any functions | ||
* that are found inside. | ||
* @param {Array} items The items in a `ConfigArray`. | ||
* @param {Object} context The context object to pass into any function | ||
* found. | ||
* @returns {Array} A flattened array containing only config objects. | ||
* @throws {TypeError} When a config function returns a function. | ||
*/ | ||
function normalizeSync(items, context) { | ||
function *flatTraverse(array) { | ||
@@ -140,2 +182,5 @@ for (let item of array) { | ||
item = item(context); | ||
if (item.then) { | ||
throw new TypeError('Async config functions are not supported.'); | ||
} | ||
} | ||
@@ -377,3 +422,3 @@ | ||
* @param {ConfigContext} context The context object for config functions. | ||
* @returns {ConfigArray} A new ConfigArray instance that is normalized. | ||
* @returns {Promise<ConfigArray>} The current ConfigArray instance. | ||
*/ | ||
@@ -396,2 +441,23 @@ async normalize(context = {}) { | ||
/** | ||
* Normalizes a config array by flattening embedded arrays and executing | ||
* config functions. | ||
* @param {ConfigContext} context The context object for config functions. | ||
* @returns {ConfigArray} The current ConfigArray instance. | ||
*/ | ||
normalizeSync(context = {}) { | ||
if (!this.isNormalized()) { | ||
const normalizedConfigs = normalizeSync(this, context); | ||
this.length = 0; | ||
this.push(...normalizedConfigs.map(this[ConfigArraySymbol.preprocessConfig])); | ||
this[ConfigArraySymbol.isNormalized] = true; | ||
// prevent further changes | ||
Object.freeze(this); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Finalizes the state of a config before being cached and returned by | ||
@@ -398,0 +464,0 @@ * `getConfig()`. Does nothing by default but is provided to be |
{ | ||
"name": "@humanwhocodes/config-array", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Glob-based configuration matching.", | ||
@@ -61,2 +61,2 @@ "author": "Nicholas C. Zakas", | ||
} | ||
} | ||
} |
@@ -213,3 +213,3 @@ # Config Array | ||
**Note:** Config functions cannot be async. This will be added in a future version. | ||
**Note:** Config functions can also be async. | ||
@@ -230,2 +230,10 @@ ### Normalizing Config Arrays | ||
If you want to disallow async config functions, you can call `normalizeSync()` instead. This method is completely synchronous and does not require using the `await` operator as it does not return a promise: | ||
```js | ||
await configs.normalizeSync({ | ||
name: "MyApp" | ||
}); | ||
``` | ||
**Important:** Once a `ConfigArray` is normalized, it cannot be changed further. You can, however, create a new `ConfigArray` and pass in the normalized instance to create an unnormalized copy. | ||
@@ -232,0 +240,0 @@ |
37342
5
439
267