@thg-altitude/schemaorg 1.0.32 → 1.0.34

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.
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Maps product data to schema-friendly format for nutrition products
3
+ * Extracts gender and age information from Mars API data structure
4
+ */
5
+ export function mapProductSchemaData(productData) {
6
+ let suggestedGender = null;
7
+ let suggestedAge = null;
8
+
9
+ // First, try to extract from Mars API structure (productContent.productContentStrictLanguage.propertyNameToValueMap)
10
+ const marsData = productData?.productContent?.productContentStrictLanguage?.propertyNameToValueMap;
11
+ if (marsData) {
12
+ // Extract gender from Mars data
13
+ if (marsData.gender && Array.isArray(marsData.gender) && marsData.gender.length > 0) {
14
+ suggestedGender = marsData.gender[0]; // Take first value: "Men"
15
+ }
16
+
17
+ // Extract age from Mars data
18
+ if (marsData.googleAgeGroup && Array.isArray(marsData.googleAgeGroup) && marsData.googleAgeGroup.length > 0) {
19
+ suggestedAge = marsData.googleAgeGroup[0]; // Take first value: "adult"
20
+ }
21
+ }
22
+
23
+ // Fallback: try to extract from content array (your current structure)
24
+ if (!suggestedGender || !suggestedAge) {
25
+ if (productData?.content && Array.isArray(productData.content)) {
26
+ // Helper function to find content item by key
27
+ const findContentItem = (key) => {
28
+ return productData.content.find(item => item.key === key);
29
+ };
30
+
31
+ // Helper function to extract value from content item
32
+ const extractValue = (contentItem) => {
33
+ if (!contentItem?.value) return null;
34
+
35
+ if (contentItem.value.stringValue) {
36
+ return contentItem.value.stringValue;
37
+ }
38
+
39
+ if (contentItem.value.stringListValue && Array.isArray(contentItem.value.stringListValue)) {
40
+ if (contentItem.value.stringListValue.length > 0 && contentItem.value.stringListValue[0]) {
41
+ return contentItem.value.stringListValue[0];
42
+ }
43
+ }
44
+
45
+ return null;
46
+ };
47
+
48
+ // Extract gender from content array if not found in Mars data
49
+ if (!suggestedGender) {
50
+ const genderItem = findContentItem('gender') || findContentItem('Gender') || findContentItem('targetGender');
51
+ if (genderItem) {
52
+ suggestedGender = extractValue(genderItem);
53
+ }
54
+ }
55
+
56
+ // Extract age from content array if not found in Mars data
57
+ if (!suggestedAge) {
58
+ const ageItem = findContentItem('googleAgeGroup') || findContentItem('ageGroup') || findContentItem('targetAge') || findContentItem('age');
59
+ if (ageItem) {
60
+ suggestedAge = extractValue(ageItem);
61
+ }
62
+ }
63
+ }
64
+ }
65
+
66
+ // If no gender/age found anywhere, provide reasonable defaults for nutrition products
67
+ if (!suggestedGender && !suggestedAge) {
68
+ return {
69
+ suggestedGender: "Unisex",
70
+ suggestedAge: "Adult"
71
+ };
72
+ }
73
+
74
+ // Return the extracted values
75
+ const result = {};
76
+ if (suggestedGender) result.suggestedGender = suggestedGender;
77
+ if (suggestedAge) result.suggestedAge = suggestedAge;
78
+
79
+ return result;
80
+ }
81
+
82
+ /**
83
+ * Enhanced version with additional nutrition-specific properties
84
+ */
85
+ export function mapProductSchemaDataEnhanced(productData) {
86
+ const basicMapping = mapProductSchemaData(productData);
87
+
88
+ if (!productData?.content || !Array.isArray(productData.content)) {
89
+ return basicMapping;
90
+ }
91
+
92
+ // Helper function to find and extract content values
93
+ const findAndExtract = (key) => {
94
+ const item = productData.content.find(item => item.key === key);
95
+ if (!item?.value) return null;
96
+
97
+ if (item.value.stringValue) return item.value.stringValue;
98
+ if (item.value.stringListValue && Array.isArray(item.value.stringListValue)) {
99
+ return item.value.stringListValue[0];
100
+ }
101
+ return null;
102
+ };
103
+
104
+ return {
105
+ ...basicMapping,
106
+ brand: findAndExtract('brand'),
107
+ diet: findAndExtract('Diet'),
108
+ range: findAndExtract('range'),
109
+ // Add any other fields you need from the content array
110
+ };
111
+ }
112
+
113
+ /**
114
+ * Simple function to strip HTML tags from text
115
+ */
116
+ export function stripHtml(html) {
117
+ if (!html) return '';
118
+ return html.replace(/<[^>]*>/g, '');
119
+ }