updated comments for better autocomplete
This commit is contained in:
parent
b1aea42784
commit
8989914726
|
@ -1,10 +1,10 @@
|
|||
import { getAvgRating } from '$helpers';
|
||||
import type { Product, Tag } from '$types';
|
||||
|
||||
/* Default value for filter selectors, saved in store */
|
||||
/** Default value for filter selectors, saved in store */
|
||||
export const defaultFilter = { selectedCat: 0, selectedRating: 0 };
|
||||
|
||||
/* Filters products by matching tags according to passed value */
|
||||
/** Filters products by matching tags according to passed value */
|
||||
export const filterByCat = (products: Product[], value: string, tags: Tag[]): Product[] =>
|
||||
products.filter((product) => {
|
||||
try {
|
||||
|
@ -18,6 +18,6 @@ export const filterByCat = (products: Product[], value: string, tags: Tag[]): Pr
|
|||
}
|
||||
});
|
||||
|
||||
/* Filters by rating greater than what is passed in */
|
||||
/** Filters by rating greater than what is passed in */
|
||||
export const filterByRating = (products: Product[], value: string | number) =>
|
||||
products.filter((product) => product.rating && getAvgRating(product.rating) >= Number(value));
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import type { Rating } from '$types';
|
||||
|
||||
/* Average of all ratings of a product */
|
||||
/** Average of all ratings of a product */
|
||||
export const getAvgRating = (ratings: Rating[]): number => {
|
||||
if (!ratings) return 0;
|
||||
if (ratings.length === 1) return ratings[0].rating;
|
||||
|
|
|
@ -2,21 +2,21 @@ import { normalize, parseSlug, parseName } from '.';
|
|||
import type { UrlObject } from 'url';
|
||||
import type { Product } from '$types';
|
||||
|
||||
/* Gets parameter from URL of field. Defaults to 'product' */
|
||||
/** Gets parameter from URL of field. Defaults to 'product' */
|
||||
export const getProdParam = (field = 'product'): string =>
|
||||
new URLSearchParams(window.location.search).get(field) || '';
|
||||
|
||||
/* Finds a product in products store with matching name as parameter. */
|
||||
/** Finds a product in products store with matching name as parameter. */
|
||||
export const findProdFromParam = (
|
||||
param: URLSearchParams,
|
||||
$products: Product[]
|
||||
): Product | Record<string, never> =>
|
||||
$products.find(({ name }) => normalize(name) === parseSlug(param)) || {};
|
||||
|
||||
/* Resets params and navigates back to index */
|
||||
/** Resets params and navigates back to index */
|
||||
export const resetParams = () => history.pushState({}, '', new URL(window.location.origin));
|
||||
|
||||
/* Updates a url with search parameters with name of product parameter. */
|
||||
/** Updates a url with search parameters with name of product parameter. */
|
||||
export const setUrlParam = (url: URL, product: Product): UrlObject => {
|
||||
url.searchParams.set('product', parseName(product.name));
|
||||
return url;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
/* Removes casing and trims trailing/leading whitespace from string */
|
||||
/** Removes casing and trims trailing/leading whitespace from string */
|
||||
export const normalize = (str: string): string => str.toLowerCase().trim();
|
||||
|
||||
/* Parses a slug to return normalized name with all dashes replaced with spaces */
|
||||
export const parseSlug = (slug: string): string => normalize(slug).replaceAll('-', ' ');
|
||||
/** Parses a slug to return normalized name with all dashes replaced with spaces */
|
||||
export const parseSlug = (slug: string): string => normalize(slug).replaceAll('-', ' ');
|
||||
|
||||
/* Parses a name to return normalized slug with all spaces replaced with dashes */
|
||||
export const parseName = (name: string):string => normalize(name).replaceAll(' ', '-');
|
||||
/** Parses a name to return normalized slug with all spaces replaced with dashes */
|
||||
export const parseName = (name: string): string => normalize(name).replaceAll(' ', '-');
|
||||
|
|
Loading…
Reference in New Issue