From 8c657994b4acf97fbc7997cb8153443cc0d0331c Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 25 Jun 2020 07:44:01 -0500 Subject: [PATCH] fix: uniquify results --- packages/behavior/type.js | 42 +++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/packages/behavior/type.js b/packages/behavior/type.js index 3d8fad0..3933cdc 100644 --- a/packages/behavior/type.js +++ b/packages/behavior/type.js @@ -59,11 +59,13 @@ export const candidates = (description, type) => { const inverted = invertedDigraph(); const typeParts = type.split('|'); 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 {children} = description; - const c = 'any' === type + return arrayUnique('any' === type ? Object.keys(children) : Object.entries(children) .reduce((r, [key, {type}]) => ( @@ -72,8 +74,8 @@ export const candidates = (description, type) => { ? [key] : [] ) - ), []); - return c; + ), []) + ); }; export function description(type, instance) { @@ -82,23 +84,37 @@ export function description(type, instance) { children: {}, type, }; - if (!allTypes[type]) { - return { - ...defaults, - type: 'undefined', - }; + const splitTypes = type.split('|'); + for (let i = 0; i < splitTypes.length; i++) { + const type = splitTypes[i]; + if (allTypes[type]) { + return { + ...defaults, + ...('function' === typeof allTypes[type] ? allTypes[type](instance) : allTypes[type]) + }; + } } return { ...defaults, - ...('function' === typeof allTypes[type] ? allTypes[type](instance) : allTypes[type]) + type: 'undefined', }; } export function fitsInto(candidate, reference) { - if ('any' === reference) { - return true; + const candidates = candidate.split('|'); + const references = candidate.split('|'); + for (let i = 0; i < references.length; i++) { + if ('any' === references[i]) { + return true; + } + for (let j = 0; j < candidates.length; j++) { + const refer = candidates[j]; + if (references[i] === candidates[j]) { + return true; + } + } } - return -1 !== reference.split('|').map((type) => type.trim()).indexOf(candidate); + return false; } export function fromLiteral(value) {