Skip to content

Commit dc1bfb7

Browse files
evanyan13Evan Yan
andauthored
[CI-4335] Export all utils functions (#142)
* Export utils format * Update imports * Relocate util functions * Update naming convention --------- Co-authored-by: Evan Yan <evan.yan@constructor.io>
1 parent 971fad8 commit dc1bfb7

File tree

8 files changed

+243
-232
lines changed

8 files changed

+243
-232
lines changed

src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
BrowseRequestType,
2020
FacetOption as ApiFacetOption,
2121
} from '@constructor-io/constructorio-client-javascript/lib/types';
22-
import { MakeOptional } from './utils/typeHelpers';
2322

2423
export {
2524
Nullable,
@@ -328,3 +327,9 @@ export type IncludeRenderProps<ComponentProps, ChildrenFunctionProps> = Componen
328327
export type IncludeRawResponse<TransformedType, OriginalType> = TransformedType & {
329328
rawResponse?: OriginalType;
330329
};
330+
331+
/**
332+
* Given a Type T and a set of keys K (pipe-delimited string), make those keys optional.
333+
*/
334+
export type MakeOptional<Type, Keys extends string & keyof Partial<Type>> = Omit<Type, Keys> &
335+
Partial<Pick<Type, Keys>>;

src/utils.tsx

Lines changed: 0 additions & 223 deletions
This file was deleted.

src/utils/common.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Function to emulate pausing between interactions
2+
export function sleep(ms) {
3+
// eslint-disable-next-line
4+
return new Promise((resolve) => setTimeout(resolve, ms));
5+
}
6+
7+
/* istanbul ignore next */
8+
export const logger = (error: any) => {
9+
try {
10+
if (typeof process !== 'undefined' && process?.env?.LOGGER) {
11+
// eslint-disable-next-line no-console
12+
console.log(error);
13+
}
14+
} catch (e) {
15+
// process variable is not available and logger should not be active
16+
}
17+
};
18+
19+
// eslint-disable-next-line @cspell/spellchecker
20+
export function tryCatchify(func: Function) {
21+
return (...args: any) => {
22+
try {
23+
return func(...args);
24+
} catch (e) {
25+
logger(e);
26+
}
27+
return undefined;
28+
};
29+
}
30+
31+
export function removeNullValuesFromObject(obj: Object) {
32+
const filteredListOfEntries = Object.entries(obj).filter(([, val]) => val != null);
33+
34+
return Object.fromEntries(filteredListOfEntries);
35+
}

src/utils/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export * from './formatters';
2+
export * from './itemFieldGetters';
3+
export * from './styleHelpers';
4+
export * from './transformers';
5+
export * from './typeHelpers';
6+
export * from './urlHelpers';
7+
export * from './requestConfigsHelpers';
8+
export * from './common';
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { SearchParameters } from '@constructor-io/constructorio-client-javascript/lib/types';
2+
import {
3+
RequestConfigs,
4+
RequestQueryParams,
5+
PlpSearchDataResults,
6+
PlpSearchDataRedirect,
7+
PlpBrowseData,
8+
} from '../types';
9+
import { removeNullValuesFromObject } from './common';
10+
import { isPlpBrowseDataResults, isPlpSearchDataResults } from './typeHelpers';
11+
12+
export function getSearchParamsFromRequestConfigs(requestConfigs: RequestConfigs): {
13+
query: string;
14+
searchParams: SearchParameters;
15+
} {
16+
const { query = '', filterValue, filterName, ...rest } = requestConfigs;
17+
const searchParams = removeNullValuesFromObject(rest);
18+
19+
return { query, searchParams };
20+
}
21+
22+
export function getBrowseParamsFromRequestConfigs(requestConfigs: RequestConfigs): {
23+
filterName: string;
24+
filterValue: string;
25+
queryParams: RequestQueryParams;
26+
} {
27+
const { query, filterValue = '', filterName = '', ...queryParams } = requestConfigs;
28+
29+
return { filterName, filterValue, queryParams: removeNullValuesFromObject(queryParams) };
30+
}
31+
32+
export function checkIsSearchPage(requestConfigs: RequestConfigs) {
33+
const { query } = getSearchParamsFromRequestConfigs(requestConfigs);
34+
35+
return !!query;
36+
}
37+
38+
export function checkIsBrowsePage(requestConfigs: RequestConfigs) {
39+
const { filterName, filterValue } = getBrowseParamsFromRequestConfigs(requestConfigs);
40+
41+
return !!filterName && !!filterValue;
42+
}
43+
44+
export type PageType = 'search' | 'browse' | 'unknown';
45+
46+
export function getPageType(requestConfigs: RequestConfigs): PageType {
47+
if (checkIsSearchPage(requestConfigs)) {
48+
return 'search';
49+
}
50+
if (checkIsBrowsePage(requestConfigs)) {
51+
return 'browse';
52+
}
53+
return 'unknown';
54+
}
55+
56+
export function getPlpContainerCnstrcDataAttributes(
57+
data: PlpSearchDataResults | PlpSearchDataRedirect | PlpBrowseData | null,
58+
requestConfigs: RequestConfigs,
59+
) {
60+
if (!data || (!isPlpSearchDataResults(data) && !isPlpBrowseDataResults(data))) return {};
61+
62+
const { filterName, filterValue } = requestConfigs;
63+
const pageType = getPageType(requestConfigs);
64+
let dataCnstrc: Record<`data-cnstrc-${string}`, string | number | boolean> = {};
65+
66+
switch (pageType) {
67+
case 'browse':
68+
dataCnstrc = {
69+
'data-cnstrc-browse': true,
70+
'data-cnstrc-num-results': data.response.totalNumResults,
71+
'data-cnstrc-filter-name': filterName!,
72+
'data-cnstrc-filter-value': filterValue!,
73+
};
74+
break;
75+
76+
case 'search':
77+
dataCnstrc = {
78+
'data-cnstrc-search': true,
79+
'data-cnstrc-num-results': data.response.totalNumResults,
80+
};
81+
break;
82+
83+
case 'unknown':
84+
dataCnstrc = {};
85+
break;
86+
87+
default:
88+
break;
89+
}
90+
91+
return dataCnstrc;
92+
}

0 commit comments

Comments
 (0)