From c73d62f6bc6d49798d21efec40dc862583b3347c Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Sat, 23 Jan 2021 14:39:04 -0600 Subject: [PATCH] =?UTF-8?q?Move=20makeSearchableList=20to=20it=E2=80=99s?= =?UTF-8?q?=20own=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build-lists.js | 84 +------------------------------------- helpers/categories.js | 2 + helpers/searchable-list.js | 84 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 82 deletions(-) create mode 100644 helpers/searchable-list.js diff --git a/build-lists.js b/build-lists.js index 7fbc755..582cddf 100644 --- a/build-lists.js +++ b/build-lists.js @@ -10,6 +10,7 @@ import { buildVideoPayload, buildAppBenchmarkPayload } from './helpers/build-pay import { categories, getAppCategory } from './helpers/categories.js' import { getAppEndpoint, getVideoEndpoint } from './helpers/app-derived.js' +import { makeSearchableList } from './helpers/searchable-list.js' // Setup dotenv dotenv.config() @@ -149,87 +150,6 @@ class BuildLists { return } - // Converts a list into a smaller searchable list - // similar to a table/spreadsheet - makeSearchableList ( list ) { - let firstLoop = true - - const searchableList = new Set() - - const tableHeader = new Set() - - // const searchableKeys = new Set([ - // 'name', - // 'text', - // 'lastUpdated', - // 'endpoint' - // ]) - - const makeSearchable = new Map([ - [ - 'name', - item => { - // console.log('Running name method', item) - return item.name - } - ], - [ - 'text', - item => item.text - ], - [ - 'endpoint', - item => item.endpoint - ], - [ - 'category', - app => { - return getAppCategory( app ).id - } - ] - ]) - - list.forEach( ( searchableItem ) => { - // If this is the first items - // then store the keys - if ( firstLoop ) { - Object.keys(searchableItem).forEach( key => { - // console.log(key, makeSearchable.has(key)) - - if ( !makeSearchable.has(key) ) return - - // Add to table header so we can loop it later on - tableHeader.add( key ) - }) - - // Add keys to table head - searchableList.add( Array.from( tableHeader ) ) - - firstLoop = false - } - // This could cause an issue if the keys - // are out of order - - // console.log('tableHeader', tableHeader) - - const tableRow = [] - - // Loop through keys from table header - // and push them to this row - tableHeader.forEach( key => { - // console.log('searchableValue', key, searchableItem[key], makeSearchable.get(key)(searchableItem) ) - - tableRow.push( makeSearchable.get(key)(searchableItem) ) - }) - - searchableList.add( tableRow ) - - return - }) - - return searchableList - } - // Save app lists to JSON saveAppLists = async function () { @@ -246,7 +166,7 @@ class BuildLists { await this.saveList( listOptions ) - const searchableList = this.makeSearchableList( this.lists[listOptions.name] ) + const searchableList = makeSearchableList( this.lists[listOptions.name] ) // console.log('searchableList', searchableList) diff --git a/helpers/categories.js b/helpers/categories.js index 738a9f9..534cd58 100644 --- a/helpers/categories.js +++ b/helpers/categories.js @@ -1,3 +1,5 @@ +// Universal JS imports only + // Contains all types of properies to keep data consistent export const categoryTemplate = { diff --git a/helpers/searchable-list.js b/helpers/searchable-list.js new file mode 100644 index 0000000..95b3bd7 --- /dev/null +++ b/helpers/searchable-list.js @@ -0,0 +1,84 @@ +// Universal JS imports only +import { getAppCategory } from './categories.js' + + +// Converts a list into a smaller searchable list +// similar to a table/spreadsheet +export function makeSearchableList ( listSet ) { + let firstLoop = true + + const searchableList = new Set() + + const tableHeader = new Set() + + // const searchableKeys = new Set([ + // 'name', + // 'text', + // 'lastUpdated', + // 'endpoint' + // ]) + + const makeSearchable = new Map([ + [ + 'name', + item => { + // console.log('Running name method', item) + return item.name + } + ], + [ + 'text', + item => item.text + ], + [ + 'endpoint', + item => item.endpoint + ], + [ + 'category', + app => { + return getAppCategory( app ).id + } + ] + ]) + + listSet.forEach( ( searchableItem ) => { + // If this is the first items + // then store the keys + if ( firstLoop ) { + Object.keys(searchableItem).forEach( key => { + // console.log(key, makeSearchable.has(key)) + + if ( !makeSearchable.has(key) ) return + + // Add to table header so we can loop it later on + tableHeader.add( key ) + }) + + // Add keys to table head + searchableList.add( Array.from( tableHeader ) ) + + firstLoop = false + } + // This could cause an issue if the keys + // are out of order + + // console.log('tableHeader', tableHeader) + + const tableRow = [] + + // Loop through keys from table header + // and push them to this row + tableHeader.forEach( key => { + // console.log('searchableValue', key, searchableItem[key], makeSearchable.get(key)(searchableItem) ) + + tableRow.push( makeSearchable.get(key)(searchableItem) ) + }) + + searchableList.add( tableRow ) + + return + }) + + return searchableList +}