fix: uniquify results

This commit is contained in:
cha0s 2020-06-25 07:44:01 -05:00
parent 770db27eaa
commit 8c657994b4

View File

@ -59,11 +59,13 @@ export const candidates = (description, type) => {
const inverted = invertedDigraph(); const inverted = invertedDigraph();
const typeParts = type.split('|'); const typeParts = type.split('|');
if (typeParts.length > 1) { if (typeParts.length > 1) {
return typeParts.reduce((r, typePart) => r.concat(candidates(description, typePart)), []); return arrayUnique(
typeParts.reduce((r, typePart) => r.concat(candidates(description, typePart)), [])
);
} }
const types = (inverted[type] || []).concat(type); const types = (inverted[type] || []).concat(type);
const {children} = description; const {children} = description;
const c = 'any' === type return arrayUnique('any' === type
? Object.keys(children) ? Object.keys(children)
: Object.entries(children) : Object.entries(children)
.reduce((r, [key, {type}]) => ( .reduce((r, [key, {type}]) => (
@ -72,8 +74,8 @@ export const candidates = (description, type) => {
? [key] ? [key]
: [] : []
) )
), []); ), [])
return c; );
}; };
export function description(type, instance) { export function description(type, instance) {
@ -82,23 +84,37 @@ export function description(type, instance) {
children: {}, children: {},
type, type,
}; };
if (!allTypes[type]) { const splitTypes = type.split('|');
return { for (let i = 0; i < splitTypes.length; i++) {
...defaults, const type = splitTypes[i];
type: 'undefined', if (allTypes[type]) {
};
}
return { return {
...defaults, ...defaults,
...('function' === typeof allTypes[type] ? allTypes[type](instance) : allTypes[type]) ...('function' === typeof allTypes[type] ? allTypes[type](instance) : allTypes[type])
}; };
}
}
return {
...defaults,
type: 'undefined',
};
} }
export function fitsInto(candidate, reference) { export function fitsInto(candidate, reference) {
if ('any' === reference) { const candidates = candidate.split('|');
const references = candidate.split('|');
for (let i = 0; i < references.length; i++) {
if ('any' === references[i]) {
return true; return true;
} }
return -1 !== reference.split('|').map((type) => type.trim()).indexOf(candidate); for (let j = 0; j < candidates.length; j++) {
const refer = candidates[j];
if (references[i] === candidates[j]) {
return true;
}
}
}
return false;
} }
export function fromLiteral(value) { export function fromLiteral(value) {