UNPKG

@mikezimm/fps-core-v7

Version:

Library of reusable core interfaces, types and constants migrated from fps-library-v2

83 lines 4.61 kB
import { mergeWordArrays } from "../../../../logic/Strings/getWordsFromString"; import { addItemIsA } from "./addItemIsA"; import { check4This, Check4 } from '../../../../logic/Links/CheckSearch'; /** * * @param items * @param SearchTypes - Pass in * @returns */ export function addSearchMeta2(items, SearchTypes) { if (!SearchTypes) return items; items.map((item) => { let { searchTitle, searchDesc, searchHref } = item.FPSItem.Search; /** * This could be added as secondary function after adding custom search code to catch any loose ends. */ //This if was added for the Standards Wiki Library where the title column is actually Title0 if (!searchTitle && item[`Title0`]) { searchTitle = item[`Title0`]; } if (!searchTitle && item.FileLeafRef) { searchTitle = item.FileLeafRef.substr(0, item.FileLeafRef.lastIndexOf('.')); } //Added for Std #139 which does not have a Title value. if (!searchDesc) { searchDesc = ''; } if (!searchHref) { if (item.ServerRedirectedEmbedUri) { searchHref = item.ServerRedirectedEmbedUri; } else if (item.FileRef) { searchHref = item.FileRef; } else if (item['File/ServerRelativeUrl']) { searchHref = item['File/ServerRelativeUrl']; } } let searchTypeIdx = SearchTypes.keys.indexOf(item.FPSItem.Search.type); let typeIdx = searchTypeIdx; if (searchTypeIdx === -1) { if (check4This(Check4.addSearchMeta2_Eq_true) === true && item.FPSItem.Search.type !== 'labels' && item.FPSItem.Search.type !== 'rigItems' && item.FPSItem.Search.type !== 'allLists') console.log('Invalid searchTypeIdx not found:', item.FPSItem.Search.type, SearchTypes.keys); } // 2023-02-13: This is needed because sometimes the type is not found and this will prevent crashes if (SearchTypes.objs[searchTypeIdx]) { const adjustIdx = SearchTypes.objs[searchTypeIdx].adjust ? SearchTypes.objs[searchTypeIdx].adjust : 0; /** * 2024-09-07: changed to ( adjustIdx === undefined ? 0 : adjustIdx ) to pass lint/build error * typeIdx = searchTypeIdx + ( typeof adjustIdx !== undefined ? adjustIdx : 0 ) ; * typeIdx = searchTypeIdx + adjustIdx; * error TS18048: 'adjustIdx' is possibly 'undefined' */ typeIdx = searchTypeIdx + (adjustIdx === undefined ? 0 : adjustIdx); if (SearchTypes.objs[searchTypeIdx].contentGroup) { item.FPSItem.contentGroup = SearchTypes.objs[searchTypeIdx].contentGroup; item = addItemIsA(item, `ItemIs${SearchTypes.objs[searchTypeIdx].contentGroup}Content`); } item.FPSItem.Search.primarySearchType = SearchTypes.objs[searchTypeIdx]; if (!item.FPSItem.Search.searchTypes) item.FPSItem.Search.searchTypes = []; if (!item.FPSItem.Search.allIsAKeys) item.FPSItem.Search.allIsAKeys = []; // Get existing item.SearchTypes that match the current one const SearchTypeIsAlreadyAdded = item.FPSItem.Search.searchTypes.filter((sType) => { return sType.key === SearchTypes.objs[searchTypeIdx].key; }); // Add current one if it does not already exist. if (SearchTypeIsAlreadyAdded.length === 0) item.FPSItem.Search.searchTypes.push(SearchTypes.objs[searchTypeIdx]); // Merge allIsAKeys from this Search Type with the Item's allIsAKeys item.FPSItem.Search.allIsAKeys = mergeWordArrays(SearchTypes.objs[searchTypeIdx].isAKeys, item.FPSItem.Search.allIsAKeys, false, 'asis'); } // This was added to insure all allIsKeys get pushed to meta if they have not already been pushed. item.FPSItem.Search.allIsAKeys.map((key) => { if (item.FPSItem.Search.meta.indexOf(key) < 0) item.FPSItem.Search.meta.push(key); }); item.FPSItem.Search.typeIdx = typeIdx > -1 ? typeIdx : SearchTypes.keys.length - 1; item.FPSItem.Search.searchTypeIdx = searchTypeIdx; item.FPSItem.Search.searchTitle = `${searchTitle}`; item.FPSItem.Search.searchDesc = `${searchDesc}`; item.FPSItem.Search.searchHref = `${searchHref}`; }); return items; } //# sourceMappingURL=addSearchMeta2.js.map