UNPKG

@mikezimm/fps-core-v7

Version:

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

81 lines (80 loc) 3.52 kB
// import { check4This } from '@mikezimm/fps-pnp2/lib/services/sp/CheckSearch'; // import { IAnySourceItem, } from "../../../AnyContent/IAnyContent'; import { addItemIsA } from './addItemIsA'; import { ItemHasNoRecentViews, ItemWasPopularLifeTime, ItemWasPopularRecently } from '../../AnyContent/IsA/IFPSItemIsA'; import { check4This, Check4 } from '../../../../logic/Links/CheckSearch'; /** * addPopularityToItems will enhance popularity metrics by highlighting items with highest relative popularity of items provided * This also makes it easier to add higlights icons later * * items MUST have FPSItem.IsA * @param items * @param popPercent */ export function addPopularityToItems(items, popPercent = 0.2) { // Add zero recent views let hasPopularity = false; items.map(item => { if (item.ViewsLifeTime && (!item.ViewsRecent || item.ViewsRecent === 0)) { item = addItemIsA(item, ItemHasNoRecentViews); hasPopularity = true; } else if (item.ViewsLifeTime) hasPopularity = true; }); // No popularity found... just return the items as is if (hasPopularity === false) return items; // Do more complex view logic items = markPopular(items, 'Recent', popPercent); items = markPopular(items, 'LifeTime', popPercent); items.map(item => { if (item.PopularLifeTime) item = addItemIsA(item, ItemWasPopularLifeTime); if (item.PopularRecent) item = addItemIsA(item, ItemWasPopularRecently); }); return items; } /** * markPopular * @param finalItems * @param Recents * @param favKey */ export function markPopular(items, favKey, topPercent = 0.25) { const Pops = []; items.map(item => { // 2024-09-07: Added as '' to pass build error because it should always be a string if it exists. // 2025-01-21: Revised with new Search logic in fetch which changes results to correct data type // However, according to chatGPT, parseInt first converts numbers to strings before converting back so.... // if (item[`Views${favKey}`] && item[`Views${favKey}`]) Pops.push(parseInt(item[`Views${favKey}`] as '' )); if (item[`Views${favKey}`] && item[`Views${favKey}`]) Pops.push(item[`Views${favKey}`]); }); // No popularity found... just return the items as is if (Pops.length === 0) return items; // Sort the array in descending order thanks to chatGPT let Tops = Pops.filter(function (element) { return typeof element === 'number' && element > 1; }); Tops = Tops.sort(function (a, b) { return b - a; }); Tops = Tops.slice(0, Math.ceil(Pops.length * topPercent)); items.map((item) => { // 2024-09-07: Added as '' to pass build error because it should always be a string if it exists. if (item[`Views${favKey}`] && Tops.indexOf(item[`Views${favKey}`]) > -1) { item.FPSItem.Search.meta.push(`Popular${favKey}`); item[`Popular${favKey}`] = item[`Views${favKey}`]; } else if (item[`Views${favKey}`] && item[`Views${favKey}`] === 0) { item.FPSItem.Search.meta.push(`NoRecentViews`); } }); if (check4This(Check4.addSearchMeta2_Eq_true) === true) console.log(`markPopular - ${favKey} ~ 42`, Tops, favKey, topPercent, items); return items; } //# sourceMappingURL=markPopular.js.map