Comparing version 6.3.0 to 6.4.0
@@ -37,7 +37,8 @@ function _isObject(obj) { | ||
// EXAMPLE OUTPUT: | ||
// { foo: { ... }, bar: { ... } } | ||
// { foo: { orderHint: 0, ... }, bar: { orderHint: 1, ... } } | ||
function _arrayToObject(arr) { | ||
return arr.reduce((acc, cur) => { | ||
return arr.reduce((acc, cur, idx) => { | ||
Object.keys(cur).forEach(key => { | ||
acc[key] = cur[key]; | ||
acc[key].orderHint = idx; | ||
}); | ||
@@ -81,2 +82,22 @@ return acc; | ||
// Transform some TOP LEVEL properties from objects to simple arrays | ||
function objectsToArrays(ramlObj) { | ||
[ | ||
'types', | ||
'traits', | ||
'resourceTypes', | ||
'annotationTypes', | ||
'securitySchemes', | ||
].forEach(key => { | ||
if (ramlObj[key]) { | ||
ramlObj[key] = _objectToArray(ramlObj[key]); | ||
ramlObj[key].sort((first, second) => { | ||
first.orderHint - second.orderHint; | ||
}); | ||
} | ||
}); | ||
return ramlObj; | ||
} | ||
// Transform some TOP LEVEL properties from arrays to simple objects | ||
@@ -102,2 +123,3 @@ function arraysToObjects(ramlObj) { | ||
recursiveObjectToArray, | ||
objectsToArrays, | ||
}; |
@@ -0,1 +1,4 @@ | ||
6.4.0 - May 29, 2019 | ||
- Array output format and orderHints in object output (via options flag) | ||
6.3.0 - May 29, 2019 | ||
@@ -2,0 +5,0 @@ - Add support for RAML parser resolvers options |
@@ -101,2 +101,3 @@ function _isObject(obj) { | ||
// https://github.com/raml-org/raml-js-parser-2/issues/582 | ||
// TODO this issue is fixed since 26 Sep 2017, i.e. v1.1.32, could be removed | ||
if (Array.isArray(obj.headers)) { | ||
@@ -103,0 +104,0 @@ obj.headers.forEach(hdr => { |
27
index.js
@@ -98,3 +98,11 @@ #!/usr/bin/env node | ||
function _enhanceRamlObj(ramlObj) { | ||
function _enhanceRamlObj(ramlObj, options) { | ||
// Override default options | ||
options = Object.assign( | ||
{ | ||
collectionFormat: 'objects', | ||
}, | ||
options | ||
); | ||
// Some of the structures (like `types`) are an array that hold key/value pairs, which is very annoying to work with. | ||
@@ -109,4 +117,9 @@ // Let's make them into a simple object, this makes it easy to use them for direct lookups. | ||
// | ||
// EXAMPLE of what we want: | ||
// EXAMPLE of what we want (default option "objects") | ||
// { foo: { ... }, bar: { ... } } | ||
// | ||
// EXAMPLE of what we want (option "arrays") | ||
// [ { key: "foo", ... }, { key: "bar", ... } ] | ||
// the "arrays" option will be evalulated at the very end to so the conversion and cleanup code | ||
// does not have to handle different data structures. | ||
ramlObj = helpers.arraysToObjects(ramlObj); | ||
@@ -149,2 +162,10 @@ | ||
// convert to optional variations in the output structure: | ||
if (options.collectionFormat === 'arrays') { | ||
// repeat recursive to also clean up the types: | ||
ramlObj = helpers.recursiveObjectToArray(ramlObj); | ||
// modify the top-level collections to be arrays | ||
ramlObj = helpers.objectsToArrays(ramlObj); | ||
} | ||
return ramlObj; | ||
@@ -207,4 +228,4 @@ } | ||
return _sourceToRamlObj(source, options).then(ramlObj => | ||
_enhanceRamlObj(ramlObj) | ||
_enhanceRamlObj(ramlObj, options) | ||
); | ||
}; |
{ | ||
"name": "raml2obj", | ||
"description": "RAML to object", | ||
"version": "6.3.0", | ||
"version": "6.4.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Kevin Renskers", |
@@ -16,3 +16,2 @@ # RAML to object | ||
## Usage | ||
@@ -29,2 +28,25 @@ ```js | ||
## Options | ||
The `parse()` function can be called with options to customize the result. | ||
Defaults are compatible with `raml2html`. | ||
```js | ||
raml2obj.parse(source, { | ||
validate: true, | ||
extensionsAndOverlays : [], | ||
collectionFormat: 'arrays', | ||
}).then(function(ramlObj) { | ||
// Do something with the resulting ramlObj :) | ||
}); | ||
``` | ||
* `validate`: triggers the `rejectOnErrors` flag of the underlying parser. defaults to `false` | ||
* `extensionsAndOverlays`: Defaults to `[]`. See parser documentation. | ||
* `collectionFormat`: choose what data structure the double-nested `[{name1: {..}}, {name2: {..}}]` patterns of the `raml-1-parser` are transformed to in the output object: | ||
| `collectionFormat` value | output | | ||
| --- | --- | | ||
|`objects` (*default*)|`{name1: { orderHint: 0, ..}, name2: { orderHint: 1, ..}}` (eases e.g. property access). *Applies to top-level collections only, nested are arrays except type properties.*| | ||
|`arrays`|`[ {key: "name1", ..}, {key: "name2", ..}]` (eases e.g. representation in a database). *Applies recursively everywhere.* | | ||
## Questions & Support | ||
@@ -31,0 +53,0 @@ Do you have a question? Have you found a bug or would you like to request a feature? Please check out [`CONTRIBUTING.md`](CONTRIBUTING.md). |
22542
417
56