diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2024-11-14 18:42:23 -0400 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2024-11-15 20:40:12 -0400 |
commit | 33e87a188b4067526f8ee4db181ad0a95470a632 (patch) | |
tree | 7c410a1f6767675878fa131b3fe066c2bbb2f12d /src/find.js | |
parent | 36f1a86102d622095ef2a3141cdae81770d2b70c (diff) |
find: factor out some stuff into matchHelper
Diffstat (limited to 'src/find.js')
-rw-r--r-- | src/find.js | 166 |
1 files changed, 86 insertions, 80 deletions
diff --git a/src/find.js b/src/find.js index 238abda8..74f0f765 100644 --- a/src/find.js +++ b/src/find.js @@ -94,6 +94,33 @@ export function processAllAvailableMatches(data, spec) { return {byName, byDirectory, multipleNameMatches}; } +function matchHelper(fullRef, { + mode, + + matchByDirectory = (_referenceType, _directory) => null, + matchByName = (_name) => null, +}) { + const regexMatch = fullRef.match(keyRefRegex); + if (!regexMatch) { + return warnOrThrow(mode, + `Malformed link reference: "${fullRef}"`); + } + + const {key: keyPart, ref: refPart} = regexMatch.groups; + + const match = + (keyPart + ? matchByDirectory(keyPart, refPart) + : matchByName(refPart)); + + if (match) { + return match; + } else { + return warnOrThrow(mode, + `Didn't match anything for ${colors.bright(fullRef)}`); + } +} + function findHelper({ referenceTypes, @@ -136,46 +163,37 @@ function findHelper({ cache.set(data, subcache); } - const regexMatch = fullRef.match(keyRefRegex); - if (!regexMatch) { - return warnOrThrow(mode, - `Malformed link reference: "${fullRef}"`); - } - - const {key: keyPart, ref: refPart} = regexMatch.groups; - - let match; - - if (keyPart) { - if (!referenceTypes.includes(keyPart)) { - return warnOrThrow(mode, - `Reference starts with "${keyPart}:", expected ` + - referenceTypes.map(type => `"${type}:"`).join(', ')); - } + return matchHelper(fullRef, { + mode, - match = subcache.byDirectory[refPart]; - } else { - const normalizedName = - refPart.toLowerCase(); - - match = subcache.byName[normalizedName]; - - if (!match && subcache.multipleNameMatches[normalizedName]) { - return warnOrThrow(mode, - `Multiple matches for reference "${fullRef}". Please resolve:\n` + - subcache.multipleNameMatches[normalizedName] - .map(match => `- ${inspect(match)}\n`) - .join('') + - `Returning null for this reference.`); - } - } - - if (!match) { - return warnOrThrow(mode, - `Didn't match anything for ${colors.bright(fullRef)}`); - } + matchByDirectory(referenceType, directory) { + if (!referenceTypes.includes(referenceType)) { + return warnOrThrow(mode, + `Reference starts with "${referenceType}:", expected ` + + referenceTypes.map(type => `"${type}:"`).join(', ')); + } - return match; + return subcache.byDirectory[directory]; + }, + + matchByName(name) { + const normalizedName = name.toLowerCase(); + const match = subcache.byName[normalizedName]; + + if (match) { + return match; + } else if (subcache.multipleNameMatches[normalizedName]) { + return warnOrThrow(mode, + `Multiple matches for reference "${fullRef}". Please resolve:\n` + + subcache.multipleNameMatches[normalizedName] + .map(match => `- ${inspect(match)}\n`) + .join('') + + `Returning null for this reference.`); + } else { + return null; + } + }, + }); }; } @@ -297,50 +315,38 @@ function findMixedHelper(config) { }); } - // TODO: Factor out this common behavior w/ findHelper - - const regexMatch = fullRef.match(keyRefRegex); - if (!regexMatch) { - return warnOrThrow(mode, - `Malformed link reference: "${fullRef}"`); - } - - const {key: keyPart, ref: refPart} = regexMatch.groups; - - let match; - - if (keyPart) { - if (!referenceTypes.includes(keyPart)) { - return warnOrThrow(mode, - `Reference starts with "${keyPart}:", expected ` + - referenceTypes.map(type => `"${type}:"`).join(', ')); - } + return matchHelper(fullRef, { + mode, - // TODO: Do something - match = null; - } else { - const normalizedName = - refPart.toLowerCase(); - - match = - byName[normalizedName]; - - if (!match && multipleNameMatches[normalizedName]) { - return warnOrThrow(mode, - `Multiple matches for reference "${fullRef}". Please resolve:\n` + - multipleNameMatches[normalizedName] - .map(match => `- ${inspect(match)}\n`) - .join('') + - `Returning null for this reference.`); - } - } - - if (!match) { - return warnOrThrow(mode, - `Didn't match anything for ${colors.bright(fullRef)}`); - } + matchByDirectory(referenceType, _directory) { + if (!referenceTypes.includes(referenceType)) { + return warnOrThrow(mode, + `Reference starts with "${referenceType}:", expected ` + + referenceTypes.map(type => `"${type}:"`).join(', ')); + } - return match; + // TODO: Do something + return null; + }, + + matchByName(name) { + const normalizedName = name.toLowerCase(); + const match = byName[normalizedName]; + + if (match) { + return match; + } else if (multipleNameMatches[normalizedName]) { + return warnOrThrow(mode, + `Multiple matches for reference "${fullRef}". Please resolve:\n` + + multipleNameMatches[normalizedName] + .map(match => `- ${inspect(match)}\n`) + .join('') + + `Returning null for this reference.`); + } else { + return null; + } + }, + }); }; } |