@factorypure/client-helpers 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +37 -1
- package/dist/index.js +51 -16
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,37 @@
|
|
|
1
|
-
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const scrapeResultsSchema: any;
|
|
3
|
+
export type ScrapeResultsType = z.infer<typeof scrapeResultsSchema>;
|
|
4
|
+
export type ScrapeFiltersType = {
|
|
5
|
+
found_id_exclusions: number[];
|
|
6
|
+
showHighPriceOutliers: boolean;
|
|
7
|
+
showLowPriceOutliers: boolean;
|
|
8
|
+
dayWindow: number | null;
|
|
9
|
+
competitor_exclusions: string[];
|
|
10
|
+
match_values: string[];
|
|
11
|
+
skip_skus: string[];
|
|
12
|
+
vendor_search_exclusions: string[];
|
|
13
|
+
search_exclusions: string[];
|
|
14
|
+
};
|
|
15
|
+
export type FilterOptionsType = {
|
|
16
|
+
exclude_linked_variants_enabled?: boolean;
|
|
17
|
+
found_id_exclusions_enabled?: boolean;
|
|
18
|
+
price_filter_enabled?: boolean;
|
|
19
|
+
date_filter_enabled?: boolean;
|
|
20
|
+
competitor_filter_enabled?: boolean;
|
|
21
|
+
duplicates_filter_enabled?: boolean;
|
|
22
|
+
search_index_enabled?: boolean;
|
|
23
|
+
skip_skus_enabled?: boolean;
|
|
24
|
+
wattage_exclusions_enabled?: boolean;
|
|
25
|
+
search_exclusions_enabled?: boolean;
|
|
26
|
+
};
|
|
27
|
+
export declare const filterScrapeResults: ({ scrapeResults, variant, filters, filterOptions, }: {
|
|
28
|
+
scrapeResults: ScrapeResultsType[];
|
|
29
|
+
variant: {
|
|
30
|
+
id: number;
|
|
31
|
+
title: string;
|
|
32
|
+
sku: string;
|
|
33
|
+
price: number;
|
|
34
|
+
};
|
|
35
|
+
filters: ScrapeFiltersType;
|
|
36
|
+
filterOptions?: FilterOptionsType;
|
|
37
|
+
}) => z.infer<any>[];
|
package/dist/index.js
CHANGED
|
@@ -3,10 +3,44 @@ import { subDays } from "date-fns";
|
|
|
3
3
|
import { Index } from "flexsearch";
|
|
4
4
|
// @ts-ignore
|
|
5
5
|
import EnglishPreset from "flexsearch/lang/en";
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
export const scrapeResultsSchema = z.object({
|
|
9
|
+
id: z.number(),
|
|
10
|
+
scrape_id: z.number(),
|
|
11
|
+
variant_id: z.number(),
|
|
12
|
+
type: z.string(),
|
|
13
|
+
format: z.nullable(z.any()),
|
|
14
|
+
position: z.number(),
|
|
15
|
+
title: z.string(),
|
|
16
|
+
found_product_id: z.number(),
|
|
17
|
+
product_link: z.string(),
|
|
18
|
+
inline_link: z.nullable(z.any()),
|
|
19
|
+
immersive_product_page_token: z.string(),
|
|
20
|
+
serpapi_immersive_product_api: z.string(),
|
|
21
|
+
source: z.string(),
|
|
22
|
+
source_icon: z.string(),
|
|
23
|
+
multiple_sources: z.number(),
|
|
24
|
+
price: z.string(),
|
|
25
|
+
extracted_price: z.number(),
|
|
26
|
+
old_price: z.nullable(z.any()),
|
|
27
|
+
extracted_old_price: z.nullable(z.any()),
|
|
28
|
+
rating: z.nullable(z.any()),
|
|
29
|
+
reviews: z.nullable(z.any()),
|
|
30
|
+
snippet: z.nullable(z.any()),
|
|
31
|
+
thumbnail: z.string(),
|
|
32
|
+
serpapi_thumbnail: z.string(),
|
|
33
|
+
tag: z.nullable(z.any()),
|
|
34
|
+
extensions: z.nullable(z.any()),
|
|
35
|
+
delivery: z.nullable(z.any()),
|
|
36
|
+
created_at: z.string(),
|
|
37
|
+
store_id: z.number(),
|
|
38
|
+
match_score: z.number().nullable(),
|
|
39
|
+
});
|
|
6
40
|
const TOO_CHEAP_MULTIPLIER = 0.75;
|
|
7
41
|
const TOO_EXPENSIVE_MULTIPLIER = 1.25;
|
|
8
42
|
const wattages = Array.from({ length: 41 }, (_, i) => (5000 + i * 500).toString());
|
|
9
|
-
export const filterScrapeResults = ({ scrapeResults,
|
|
43
|
+
export const filterScrapeResults = ({ scrapeResults, variant, filters, filterOptions = {
|
|
10
44
|
exclude_linked_variants_enabled: true,
|
|
11
45
|
found_id_exclusions_enabled: true,
|
|
12
46
|
price_filter_enabled: true,
|
|
@@ -20,13 +54,13 @@ export const filterScrapeResults = ({ scrapeResults, product, variant, filters,
|
|
|
20
54
|
}, }) => {
|
|
21
55
|
let filteredResults = scrapeResults;
|
|
22
56
|
if (filterOptions.exclude_linked_variants_enabled) {
|
|
23
|
-
filteredResults = filterLinkedElsewhereResults(filteredResults, variant);
|
|
57
|
+
filteredResults = filterLinkedElsewhereResults(filteredResults, variant.id);
|
|
24
58
|
}
|
|
25
59
|
if (filterOptions.found_id_exclusions_enabled) {
|
|
26
60
|
filteredResults = filterFoundProductIdExclusions(filteredResults, filters.found_id_exclusions);
|
|
27
61
|
}
|
|
28
62
|
if (filterOptions.price_filter_enabled) {
|
|
29
|
-
filteredResults = filterPriceOutliers(filteredResults, variant, filters.showHighPriceOutliers, filters.showLowPriceOutliers);
|
|
63
|
+
filteredResults = filterPriceOutliers(filteredResults, variant.price, filters.showHighPriceOutliers, filters.showLowPriceOutliers);
|
|
30
64
|
}
|
|
31
65
|
if (filterOptions.date_filter_enabled) {
|
|
32
66
|
filteredResults = filterDateWindow(filteredResults, filters.dayWindow);
|
|
@@ -38,11 +72,12 @@ export const filterScrapeResults = ({ scrapeResults, product, variant, filters,
|
|
|
38
72
|
filteredResults = filterDuplicateResults(filteredResults);
|
|
39
73
|
}
|
|
40
74
|
if (filterOptions.search_index_enabled) {
|
|
41
|
-
filteredResults = handleIndexSearch(filteredResults, filters, variant,
|
|
75
|
+
filteredResults = handleIndexSearch(filteredResults, filters, variant, filterOptions);
|
|
42
76
|
}
|
|
43
77
|
return filteredResults;
|
|
44
78
|
};
|
|
45
|
-
const handleIndexSearch = (dataToSearch, filters, variant,
|
|
79
|
+
const handleIndexSearch = (dataToSearch, filters, variant, filterOptions) => {
|
|
80
|
+
const { title, sku } = variant;
|
|
46
81
|
function customEncoder(content) {
|
|
47
82
|
const tokens = [];
|
|
48
83
|
const str = content.toLowerCase();
|
|
@@ -68,12 +103,12 @@ const handleIndexSearch = (dataToSearch, filters, variant, product, filterOption
|
|
|
68
103
|
const nots = [];
|
|
69
104
|
if (filters.skip_skus.length && filterOptions.skip_skus_enabled) {
|
|
70
105
|
const formatted = filters.skip_skus
|
|
71
|
-
.filter((sku) => sku.toLowerCase() !==
|
|
106
|
+
.filter((sku) => sku.toLowerCase() !== sku.toLowerCase())
|
|
72
107
|
.map((sku) => ` ${sku} `);
|
|
73
108
|
nots.push(...formatted);
|
|
74
109
|
}
|
|
75
|
-
const
|
|
76
|
-
const titleWithoutSku =
|
|
110
|
+
const lowerCaseTitle = title.toLowerCase();
|
|
111
|
+
const titleWithoutSku = lowerCaseTitle.replace(sku.toLowerCase(), "");
|
|
77
112
|
const wattageTerms = Array.from(titleWithoutSku.matchAll(/\b(\d{4,5})w\b/gi)).map((match) => match[1]);
|
|
78
113
|
const wattagesToExclude = wattages.filter((wattage) => !wattageTerms.includes(wattage));
|
|
79
114
|
if (filterOptions.wattage_exclusions_enabled) {
|
|
@@ -92,9 +127,9 @@ const handleIndexSearch = (dataToSearch, filters, variant, product, filterOption
|
|
|
92
127
|
}
|
|
93
128
|
if (filters.vendor_search_exclusions.length &&
|
|
94
129
|
filterOptions.skip_skus_enabled) {
|
|
95
|
-
nots.push(...filters.vendor_search_exclusions.filter((sku) => sku.toLowerCase() !==
|
|
130
|
+
nots.push(...filters.vendor_search_exclusions.filter((sku) => sku.toLowerCase() !== sku.toLowerCase()));
|
|
96
131
|
}
|
|
97
|
-
if (
|
|
132
|
+
if (title.toLowerCase().includes("dual fuel")) {
|
|
98
133
|
// nots.push('tri fuel', 'trifuel', 'tri-fuel')
|
|
99
134
|
}
|
|
100
135
|
let final = null;
|
|
@@ -124,7 +159,7 @@ const handleIndexSearch = (dataToSearch, filters, variant, product, filterOption
|
|
|
124
159
|
// MUST INCLUDE SKU???
|
|
125
160
|
final = final.and({
|
|
126
161
|
index: index,
|
|
127
|
-
query:
|
|
162
|
+
query: sku,
|
|
128
163
|
resolve: false,
|
|
129
164
|
suggest: true,
|
|
130
165
|
});
|
|
@@ -142,10 +177,10 @@ const handleIndexSearch = (dataToSearch, filters, variant, product, filterOption
|
|
|
142
177
|
});
|
|
143
178
|
return resultsArray;
|
|
144
179
|
};
|
|
145
|
-
const filterLinkedElsewhereResults = (results,
|
|
180
|
+
const filterLinkedElsewhereResults = (results, variantId) => {
|
|
146
181
|
const filtered = results.filter((result) => {
|
|
147
182
|
const productLinkedElsewhere = result.linked_variant_ids?.length > 0 &&
|
|
148
|
-
!result.linked_variant_ids.includes(
|
|
183
|
+
!result.linked_variant_ids.includes(variantId);
|
|
149
184
|
if (productLinkedElsewhere) {
|
|
150
185
|
return false;
|
|
151
186
|
}
|
|
@@ -164,12 +199,12 @@ const filterFoundProductIdExclusions = (results, found_id_exclusions) => {
|
|
|
164
199
|
});
|
|
165
200
|
return filtered;
|
|
166
201
|
};
|
|
167
|
-
const filterPriceOutliers = (results,
|
|
202
|
+
const filterPriceOutliers = (results, variantPrice, showHighPriceOutliers, showLowPriceOutliers) => {
|
|
168
203
|
const filtered = results.filter((item) => {
|
|
169
204
|
const showMoreExpensive = showHighPriceOutliers === true;
|
|
170
|
-
const isMoreExpensive = item.extracted_price >
|
|
205
|
+
const isMoreExpensive = item.extracted_price > variantPrice * TOO_EXPENSIVE_MULTIPLIER;
|
|
171
206
|
const showTooCheap = showLowPriceOutliers === true;
|
|
172
|
-
const isTooCheap = item.extracted_price <
|
|
207
|
+
const isTooCheap = item.extracted_price < variantPrice * TOO_CHEAP_MULTIPLIER;
|
|
173
208
|
if (isMoreExpensive && !showMoreExpensive) {
|
|
174
209
|
return false;
|
|
175
210
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@factorypure/client-helpers",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"@types/date-fns": "^2.5.3",
|
|
23
23
|
"date-fns": "^4.1.0",
|
|
24
|
-
"flexsearch": "^0.8.212"
|
|
24
|
+
"flexsearch": "^0.8.212",
|
|
25
|
+
"zod": "^4.3.5"
|
|
25
26
|
}
|
|
26
27
|
}
|