From 245ce84ef7361334f244d91627b8b7cf201df4f7 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 14 May 2021 13:44:40 -0300 Subject: rename "search" fns to "find", move to utils --- src/util/find.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/util/find.js (limited to 'src/util') diff --git a/src/util/find.js b/src/util/find.js new file mode 100644 index 0000000..aef3d3b --- /dev/null +++ b/src/util/find.js @@ -0,0 +1,50 @@ +function findHelper(keys, dataProp, findFn) { + return (ref, {wikiData}) => { + if (!ref) return null; + ref = ref.replace(new RegExp(`^(${keys.join('|')}):`), ''); + + const found = findFn(ref, wikiData[dataProp]); + if (!found) { + logWarn`Didn't match anything for ${ref}! (${keys.join(', ')})`; + } + + return found; + }; +} + +function matchDirectory(ref, data) { + return data.find(({ directory }) => directory === ref); +} + +function matchDirectoryOrName(ref, data) { + let thing; + + thing = matchDirectory(ref, data); + if (thing) return thing; + + thing = data.find(({ name }) => name === ref); + if (thing) return thing; + + thing = data.find(({ name }) => name.toLowerCase() === ref.toLowerCase()); + if (thing) { + logWarn`Bad capitalization: ${'\x1b[31m' + ref} -> ${'\x1b[32m' + thing.name}`; + return thing; + } + + return null; +} + +const find = { + album: findHelper(['album', 'album-commentary'], 'albumData', matchDirectoryOrName), + artist: findHelper(['artist', 'artist-gallery'], 'artistData', matchDirectoryOrName), + flash: findHelper(['flash'], 'flashData', matchDirectory), + group: findHelper(['group', 'group-gallery'], 'groupData', matchDirectoryOrName), + listing: findHelper(['listing'], 'listingSpec', matchDirectory), + newsEntry: findHelper(['news-entry'], 'newsData', matchDirectory), + staticPage: findHelper(['static'], 'staticPageData', matchDirectory), + tag: findHelper(['tag'], 'tagData', (ref, data) => + matchDirectoryOrName(ref.startsWith('cw: ') ? ref.slice(4) : ref, data)), + track: findHelper(['track'], 'trackData', matchDirectoryOrName) +}; + +export default find; -- cgit 1.3.0-6-gf8a5