This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function useReducer(reducer, initState) { | |
const [state, setState] = useState(initState); | |
const dispatch = useCallback((action) => { | |
const nextState = reducer(state, action) | |
setState(nextState) | |
}, [setState, state]) | |
return [state, dispatch]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
search.query("business") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
search.loadProviders(searchUsingTerm) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const searchUsingType: SearchResultProvider = (searchTerm) => { | |
const data = [ | |
{ | |
"term": "facebook", | |
"type": "social" | |
}, | |
{ | |
"term": "twitter", | |
"type": "social" | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
search.query("business") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface SearchResult { | |
term: string | |
type: string | |
} | |
type SearchResults = SearchResult[] | |
type SearchResultProvider = (searchTerm: string) => SearchResults | |
class SearchRecSys { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface SearchResult { | |
term: string | |
type: string | |
} | |
type SearchResults = SearchResult[] | |
type SearchResultProvider = (searchTerm: string) => SearchResults | |
class SearchRecSys { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const searchUsingTerm: SearchResultProvider = (searchTerm) => { | |
const data = [ | |
{ | |
"term": "business", | |
"type": "term" | |
}, | |
{ | |
"term": "business tips", | |
"type": "term" | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchRecSys { | |
//..... our other code | |
query: SearchResultProvider = (searchTerm) => { | |
const results = this.providers.reduce( | |
(searchResults, item) => { | |
return searchResults.concat(item(searchTerm, this.data)) | |
},[] | |
) | |
return results | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchRecSys { | |
//..... our other code | |
loadProviders = ( | |
providers: SearchResultProvider | SearchResultProvider[] | |
): void => { | |
this.providers = this.providers.concat(providers) | |
} | |
//..... our other code | |
} |