Skip to content

Commit

Permalink
fix: count with the option that params can have no value
Browse files Browse the repository at this point in the history
  • Loading branch information
honzajavorek committed Jan 9, 2019
1 parent 233dc0e commit f332490
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 19 deletions.
65 changes: 47 additions & 18 deletions lib/compile-uri/compile-params.js
@@ -1,22 +1,51 @@
module.exports = function compileParams(hrefVariablesElement) {
const params = {};
function getRequired(memberElement) {
const typeAttributes = memberElement.attributes.getValue('typeAttributes') || [];
return typeAttributes.includes('required');
}

if (!hrefVariablesElement) return params;

hrefVariablesElement.forEach((valueElement, keyElement, memberElement) => {
const name = keyElement.toValue();
const typeAttributes = memberElement.attributes.getValue('typeAttributes') || [];
const values = valueElement.attributes.getValue('enumerations') || [];
function getDefault(valueElement) {
return valueElement ? valueElement.attributes.getValue('default') : undefined;
}


function getExample(valueElement) {
if (valueElement) {
const example = valueElement.toValue();
params[name] = {
required: Array.from(typeAttributes).includes('required'),
default: valueElement.attributes.getValue('default'),
example: typeof example === 'undefined' || example === null ? values[0] : example,
values,
};

return params;
});
return params;
};

if (typeof example === 'undefined' || example === null) {
const values = valueElement.attributes.getValue('enumerations') || [];
return values[0];
}
return example;
}
return undefined;
}


function getValues(valueElement) {
return valueElement
? valueElement.attributes.getValue('enumerations') || []
: [];
}


function compileParams(hrefVariablesElement) {
if (!hrefVariablesElement) return {};
return hrefVariablesElement
.map((valueElement, keyElement, memberElement) => {
const name = keyElement.toValue();
return {
[name]: {
required: getRequired(memberElement),
default: getDefault(valueElement),
example: getExample(valueElement),
values: getValues(valueElement),
},
};
})
.reduce((params, param) => Object.assign(params, param), {});
}


module.exports = compileParams;
18 changes: 17 additions & 1 deletion test/unit/compile-uri/compile-params-test.js
@@ -1,9 +1,9 @@
const { assert } = require('chai');

const fury = require('fury');

const compileParams = require('../../../lib/compile-uri/compile-params');


describe('compileParams()', () => {
it('should compile a primitive href variable', () => {
const hrefVariables = new fury.minim.elements.HrefVariables();
Expand Down Expand Up @@ -54,6 +54,22 @@ describe('compileParams()', () => {
});
});

it('should compile a primitive href variable with no value', () => {
const hrefVariables = new fury.minim.elements.HrefVariables();
hrefVariables.set('name', undefined);

const parameters = compileParams(hrefVariables);

assert.deepEqual(parameters, {
name: {
default: undefined,
example: undefined,
required: false,
values: [],
},
});
});

it('should compile a primitive href variable with default', () => {
const hrefVariables = new fury.minim.elements.HrefVariables();
hrefVariables.set('name', new fury.minim.elements.String());
Expand Down

0 comments on commit f332490

Please sign in to comment.