@react-pakistan/util-functions 1.25.11 → 1.25.13

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.
Files changed (73) hide show
  1. package/constants/cache-time.d.ts +2 -0
  2. package/constants/cache-time.js +3 -1
  3. package/general/generate-article-schema.d.ts +49 -0
  4. package/general/generate-article-schema.js +100 -0
  5. package/general/generate-blog-schema.d.ts +7 -5
  6. package/general/generate-blog-schema.js +7 -5
  7. package/general/generate-book-schema.d.ts +57 -0
  8. package/general/generate-book-schema.js +109 -0
  9. package/general/generate-breadcrumb-schema.d.ts +22 -6
  10. package/general/generate-breadcrumb-schema.js +15 -4
  11. package/general/generate-carousel-schema.d.ts +51 -0
  12. package/general/generate-carousel-schema.js +172 -0
  13. package/general/generate-course-schema.d.ts +52 -0
  14. package/general/generate-course-schema.js +124 -0
  15. package/general/generate-dataset-schema.d.ts +64 -0
  16. package/general/generate-dataset-schema.js +156 -0
  17. package/general/generate-discussion-forum-schema.d.ts +38 -0
  18. package/general/generate-discussion-forum-schema.js +67 -0
  19. package/general/generate-education-schema.d.ts +67 -0
  20. package/general/generate-education-schema.js +137 -0
  21. package/general/generate-employer-aggregate-rating-schema.d.ts +28 -0
  22. package/general/generate-employer-aggregate-rating-schema.js +64 -0
  23. package/general/generate-event-schema.d.ts +62 -0
  24. package/general/generate-event-schema.js +214 -0
  25. package/general/generate-faq-schema.d.ts +15 -11
  26. package/general/generate-faq-schema.js +18 -6
  27. package/general/generate-image-metadata-schema.d.ts +35 -0
  28. package/general/generate-image-metadata-schema.js +73 -0
  29. package/general/generate-job-posting-schema.d.ts +48 -0
  30. package/general/generate-job-posting-schema.js +120 -0
  31. package/general/generate-local-business-schema.d.ts +41 -0
  32. package/general/generate-local-business-schema.js +75 -0
  33. package/general/generate-math-solver-schema.d.ts +44 -0
  34. package/general/generate-math-solver-schema.js +83 -0
  35. package/general/generate-movie-carousel-schema.d.ts +50 -0
  36. package/general/generate-movie-carousel-schema.js +160 -0
  37. package/general/generate-organization-schema.d.ts +9 -5
  38. package/general/generate-organization-schema.js +9 -5
  39. package/general/generate-profile-page-schema.d.ts +36 -0
  40. package/general/generate-profile-page-schema.js +82 -0
  41. package/general/generate-qa-page-schema.d.ts +33 -0
  42. package/general/generate-qa-page-schema.js +67 -0
  43. package/general/generate-recipe-schema.d.ts +48 -0
  44. package/general/generate-recipe-schema.js +104 -0
  45. package/general/generate-review-snippet-schema.d.ts +51 -0
  46. package/general/generate-review-snippet-schema.js +137 -0
  47. package/general/generate-shopping-loyalty-program-schema.d.ts +37 -0
  48. package/general/generate-shopping-loyalty-program-schema.js +59 -0
  49. package/general/generate-shopping-merchant-listing-schema.d.ts +52 -0
  50. package/general/generate-shopping-merchant-listing-schema.js +153 -0
  51. package/general/generate-shopping-merchant-return-policy-schema.d.ts +31 -0
  52. package/general/generate-shopping-merchant-return-policy-schema.js +68 -0
  53. package/general/generate-shopping-merchant-shipping-policy-schema.d.ts +35 -0
  54. package/general/generate-shopping-merchant-shipping-policy-schema.js +74 -0
  55. package/general/generate-shopping-overview-schema.d.ts +51 -0
  56. package/general/generate-shopping-overview-schema.js +157 -0
  57. package/general/generate-shopping-product-snippet-schema.d.ts +56 -0
  58. package/general/generate-shopping-product-snippet-schema.js +167 -0
  59. package/general/generate-shopping-variants-schema.d.ts +45 -0
  60. package/general/generate-shopping-variants-schema.js +143 -0
  61. package/general/generate-software-app-schema.d.ts +61 -0
  62. package/general/generate-software-app-schema.js +143 -0
  63. package/general/generate-speakable-schema.d.ts +63 -0
  64. package/general/generate-speakable-schema.js +139 -0
  65. package/general/generate-subscription-schema.d.ts +59 -0
  66. package/general/generate-subscription-schema.js +133 -0
  67. package/general/generate-vacation-rental-schema.d.ts +66 -0
  68. package/general/generate-vacation-rental-schema.js +135 -0
  69. package/general/generate-video-schema.d.ts +74 -0
  70. package/general/generate-video-schema.js +163 -0
  71. package/general/index.d.ts +30 -0
  72. package/general/index.js +30 -0
  73. package/package.json +1 -1
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Generate a merchant product listing as schema.org structured data.
3
+ *
4
+ * Returns an `ItemList` where each `ListItem.item` is a `Product` with
5
+ * optional `Offer` entries. Supports brand, seller, images, and basic
6
+ * offer normalization; pass `skipInvalidCurrency` to ignore currency
7
+ * validation for price fields.
8
+ *
9
+ * See `MerchantListingOptions` and `MerchantProduct` types for accepted
10
+ * fields.
11
+ * @returns A JSON-LD compatible `ItemList` of Products
12
+ */
13
+ interface BrandRef {
14
+ name: string;
15
+ url?: string;
16
+ }
17
+ interface MerchantRef {
18
+ name: string;
19
+ url?: string;
20
+ }
21
+ interface OfferItem {
22
+ price?: string | number;
23
+ priceCurrency?: string;
24
+ availability?: string;
25
+ url?: string;
26
+ priceValidUntil?: string;
27
+ itemCondition?: string;
28
+ seller?: MerchantRef;
29
+ }
30
+ interface MerchantProduct {
31
+ id?: string;
32
+ name: string;
33
+ url?: string;
34
+ image?: string | {
35
+ url: string;
36
+ width?: number;
37
+ height?: number;
38
+ };
39
+ description?: string;
40
+ brand?: string | BrandRef;
41
+ sku?: string;
42
+ offers?: OfferItem | OfferItem[];
43
+ }
44
+ interface MerchantListingOptions {
45
+ merchant?: MerchantRef;
46
+ name?: string;
47
+ url?: string;
48
+ products: MerchantProduct[];
49
+ skipInvalidCurrency?: boolean;
50
+ }
51
+ export declare const generateShoppingMerchantListingSchema: ({ merchant, name, url, products, skipInvalidCurrency, }: MerchantListingOptions) => any;
52
+ export {};
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ /**
3
+ * Generate a merchant product listing as schema.org structured data.
4
+ *
5
+ * Returns an `ItemList` where each `ListItem.item` is a `Product` with
6
+ * optional `Offer` entries. Supports brand, seller, images, and basic
7
+ * offer normalization; pass `skipInvalidCurrency` to ignore currency
8
+ * validation for price fields.
9
+ *
10
+ * See `MerchantListingOptions` and `MerchantProduct` types for accepted
11
+ * fields.
12
+ * @returns A JSON-LD compatible `ItemList` of Products
13
+ */
14
+ var __assign = (this && this.__assign) || function () {
15
+ __assign = Object.assign || function(t) {
16
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
17
+ s = arguments[i];
18
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
19
+ t[p] = s[p];
20
+ }
21
+ return t;
22
+ };
23
+ return __assign.apply(this, arguments);
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.generateShoppingMerchantListingSchema = void 0;
27
+ var formatImage = function (image) {
28
+ if (!image)
29
+ return undefined;
30
+ if (typeof image === 'string')
31
+ return image;
32
+ return __assign(__assign({ '@type': 'ImageObject', url: image.url }, (image.width && { width: image.width })), (image.height && { height: image.height }));
33
+ };
34
+ var formatBrand = function (b) {
35
+ if (!b)
36
+ return undefined;
37
+ if (typeof b === 'string')
38
+ return { '@type': 'Brand', name: b };
39
+ return __assign({ '@type': 'Brand', name: b.name }, (b.url && { url: b.url }));
40
+ };
41
+ var isValidCurrency = function (code) {
42
+ if (!code)
43
+ return false;
44
+ return /^[A-Z]{3}$/.test(code);
45
+ };
46
+ var AVAILABILITIES = [
47
+ 'InStock',
48
+ 'OutOfStock',
49
+ 'PreOrder',
50
+ 'PreSale',
51
+ 'Discontinued',
52
+ 'OnlineOnly',
53
+ 'InStoreOnly',
54
+ 'LimitedAvailability',
55
+ 'BackOrder',
56
+ ];
57
+ var normalizeAvailability = function (val) {
58
+ if (!val)
59
+ return undefined;
60
+ try {
61
+ var asUrl = String(val);
62
+ if (asUrl.startsWith('http')) {
63
+ var token = asUrl.replace(/https?:\/\/schema\.org\//i, '');
64
+ if (AVAILABILITIES.includes(token))
65
+ return "https://schema.org/".concat(token);
66
+ return undefined;
67
+ }
68
+ if (AVAILABILITIES.includes(asUrl))
69
+ return "https://schema.org/".concat(asUrl);
70
+ return undefined;
71
+ }
72
+ catch (_a) {
73
+ return undefined;
74
+ }
75
+ };
76
+ var generateShoppingMerchantListingSchema = function (_a) {
77
+ var merchant = _a.merchant, name = _a.name, url = _a.url, products = _a.products, _b = _a.skipInvalidCurrency, skipInvalidCurrency = _b === void 0 ? false : _b;
78
+ var itemList = products.map(function (p, i) {
79
+ var product = {
80
+ '@type': 'Product',
81
+ name: p.name,
82
+ };
83
+ if (p.id)
84
+ product['@id'] = p.id;
85
+ if (p.url)
86
+ product.url = p.url;
87
+ var img = formatImage(p.image);
88
+ if (img)
89
+ product.image = img;
90
+ if (p.description)
91
+ product.description = p.description;
92
+ var brand = formatBrand(p.brand);
93
+ if (brand)
94
+ product.brand = brand;
95
+ if (p.sku)
96
+ product.sku = p.sku;
97
+ if (p.offers) {
98
+ var offs = Array.isArray(p.offers) ? p.offers : [p.offers];
99
+ var mapped = offs
100
+ .map(function (o) {
101
+ if (o.priceCurrency && !isValidCurrency(o.priceCurrency))
102
+ return { __invalidCurrency: o.priceCurrency };
103
+ var of = { '@type': 'Offer' };
104
+ if (o.price != null)
105
+ of.price = o.price;
106
+ if (o.priceCurrency)
107
+ of.priceCurrency = o.priceCurrency;
108
+ if (o.availability) {
109
+ var n = normalizeAvailability(o.availability);
110
+ if (n)
111
+ of.availability = n;
112
+ }
113
+ if (o.url)
114
+ of.url = o.url;
115
+ if (o.seller)
116
+ of.seller = __assign(__assign({ '@type': 'Organization' }, (o.seller.name && { name: o.seller.name })), (o.seller.url && { url: o.seller.url }));
117
+ if (o.priceValidUntil)
118
+ of.priceValidUntil = o.priceValidUntil;
119
+ if (o.itemCondition)
120
+ of.itemCondition = o.itemCondition;
121
+ return of;
122
+ })
123
+ .filter(function (x) { return x && !('__invalidCurrency' in x); });
124
+ var hadInvalid = offs.some(function (o) { return o.priceCurrency && !isValidCurrency(o.priceCurrency); });
125
+ if (hadInvalid && !skipInvalidCurrency) {
126
+ var bad = offs.find(function (o) { return o.priceCurrency && !isValidCurrency(o.priceCurrency); });
127
+ throw new Error("Invalid currency code: ".concat(bad === null || bad === void 0 ? void 0 : bad.priceCurrency));
128
+ }
129
+ if (mapped.length === 1)
130
+ product.offers = mapped[0];
131
+ else if (mapped.length > 1)
132
+ product.offers = mapped;
133
+ }
134
+ return {
135
+ '@type': 'ListItem',
136
+ position: i + 1,
137
+ item: product,
138
+ };
139
+ });
140
+ var schema = {
141
+ '@context': 'https://schema.org',
142
+ '@type': 'ItemList',
143
+ itemListElement: itemList,
144
+ };
145
+ if (name)
146
+ schema.name = name;
147
+ if (url)
148
+ schema.mainEntityOfPage = { '@type': 'WebPage', '@id': url };
149
+ if (merchant)
150
+ schema.provider = __assign({ '@type': 'Organization', name: merchant.name }, (merchant.url && { url: merchant.url }));
151
+ return schema;
152
+ };
153
+ exports.generateShoppingMerchantListingSchema = generateShoppingMerchantListingSchema;
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Generate schema.org structured data for a merchant return policy
3
+ * (`MerchantReturnPolicy`).
4
+ *
5
+ * The utility supports normalizing the `returnPolicyCategory` token or
6
+ * URL and optionally validates currency codes for `returnFees`. Set
7
+ * `skipInvalidCurrency` to true to ignore currency validation.
8
+ *
9
+ * See `ReturnPolicyOptions` for available fields.
10
+ * @returns A JSON-LD compatible `MerchantReturnPolicy` object
11
+ */
12
+ interface MerchantRef {
13
+ name: string;
14
+ url?: string;
15
+ }
16
+ interface ReturnFees {
17
+ amount?: number | string;
18
+ currency?: string;
19
+ }
20
+ interface ReturnPolicyOptions {
21
+ name: string;
22
+ description?: string;
23
+ merchant?: MerchantRef;
24
+ returnPolicyCategory?: string;
25
+ returnMethods?: string[];
26
+ inStoreReturns?: boolean;
27
+ returnFees?: ReturnFees;
28
+ skipInvalidCurrency?: boolean;
29
+ }
30
+ export declare const generateShoppingReturnPolicySchema: ({ name, description, merchant, returnPolicyCategory, returnMethods, inStoreReturns, returnFees, skipInvalidCurrency, }: ReturnPolicyOptions) => any;
31
+ export {};
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ /**
3
+ * Generate schema.org structured data for a merchant return policy
4
+ * (`MerchantReturnPolicy`).
5
+ *
6
+ * The utility supports normalizing the `returnPolicyCategory` token or
7
+ * URL and optionally validates currency codes for `returnFees`. Set
8
+ * `skipInvalidCurrency` to true to ignore currency validation.
9
+ *
10
+ * See `ReturnPolicyOptions` for available fields.
11
+ * @returns A JSON-LD compatible `MerchantReturnPolicy` object
12
+ */
13
+ var __assign = (this && this.__assign) || function () {
14
+ __assign = Object.assign || function(t) {
15
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
16
+ s = arguments[i];
17
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
18
+ t[p] = s[p];
19
+ }
20
+ return t;
21
+ };
22
+ return __assign.apply(this, arguments);
23
+ };
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.generateShoppingReturnPolicySchema = void 0;
26
+ var isValidCurrency = function (code) {
27
+ if (!code)
28
+ return false;
29
+ return /^[A-Z]{3}$/.test(code);
30
+ };
31
+ var normalizePolicyCategory = function (val) {
32
+ if (!val)
33
+ return undefined;
34
+ var s = String(val);
35
+ if (s.startsWith('http'))
36
+ return s;
37
+ return "https://schema.org/".concat(s);
38
+ };
39
+ var generateShoppingReturnPolicySchema = function (_a) {
40
+ var name = _a.name, description = _a.description, merchant = _a.merchant, returnPolicyCategory = _a.returnPolicyCategory, returnMethods = _a.returnMethods, inStoreReturns = _a.inStoreReturns, returnFees = _a.returnFees, _b = _a.skipInvalidCurrency, skipInvalidCurrency = _b === void 0 ? false : _b;
41
+ var schema = {
42
+ '@context': 'https://schema.org',
43
+ '@type': 'MerchantReturnPolicy',
44
+ name: name,
45
+ };
46
+ if (description)
47
+ schema.description = description;
48
+ if (merchant)
49
+ schema.merchant = __assign({ '@type': 'Organization', name: merchant.name }, (merchant.url && { url: merchant.url }));
50
+ var cat = normalizePolicyCategory(returnPolicyCategory);
51
+ if (cat)
52
+ schema.returnPolicyCategory = cat;
53
+ if (returnMethods && returnMethods.length > 0)
54
+ schema.returnMethods = returnMethods;
55
+ if (typeof inStoreReturns === 'boolean')
56
+ schema.inStoreReturns = inStoreReturns;
57
+ if (returnFees) {
58
+ if (returnFees.currency && !isValidCurrency(returnFees.currency)) {
59
+ if (!skipInvalidCurrency)
60
+ throw new Error("Invalid currency code: ".concat(returnFees.currency));
61
+ }
62
+ else {
63
+ schema.returnShippingFees = __assign({ '@type': 'MonetaryAmount', value: returnFees.amount }, (returnFees.currency && { currency: returnFees.currency }));
64
+ }
65
+ }
66
+ return schema;
67
+ };
68
+ exports.generateShoppingReturnPolicySchema = generateShoppingReturnPolicySchema;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Generate schema.org JSON-LD for a merchant shipping policy or service
3
+ * (`ShippingService`).
4
+ *
5
+ * Supports provider information, shipping rates (with currency
6
+ * validation), transit time ranges, free shipping thresholds, and an
7
+ * optional `shippingSettingsLink`.
8
+ *
9
+ * See `ShippingPolicyOptions` for supported fields and the
10
+ * `skipInvalidCurrency` flag behaviour.
11
+ * @returns A JSON-LD compatible `ShippingService` object
12
+ */
13
+ interface MerchantRef {
14
+ name: string;
15
+ url?: string;
16
+ }
17
+ interface ShippingRate {
18
+ price?: number | string;
19
+ currency?: string;
20
+ shippingLabel?: string;
21
+ }
22
+ interface ShippingPolicyOptions {
23
+ name: string;
24
+ provider?: MerchantRef;
25
+ description?: string;
26
+ shippingDestination?: string;
27
+ transitTimeMinDays?: number;
28
+ transitTimeMaxDays?: number;
29
+ shippingRate?: ShippingRate;
30
+ shippingSettingsLink?: string;
31
+ freeShippingThreshold?: number | string;
32
+ skipInvalidCurrency?: boolean;
33
+ }
34
+ export declare const generateShoppingShippingPolicySchema: ({ name, provider, description, shippingDestination, transitTimeMinDays, transitTimeMaxDays, shippingRate, shippingSettingsLink, freeShippingThreshold, skipInvalidCurrency, }: ShippingPolicyOptions) => any;
35
+ export {};
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ /**
3
+ * Generate schema.org JSON-LD for a merchant shipping policy or service
4
+ * (`ShippingService`).
5
+ *
6
+ * Supports provider information, shipping rates (with currency
7
+ * validation), transit time ranges, free shipping thresholds, and an
8
+ * optional `shippingSettingsLink`.
9
+ *
10
+ * See `ShippingPolicyOptions` for supported fields and the
11
+ * `skipInvalidCurrency` flag behaviour.
12
+ * @returns A JSON-LD compatible `ShippingService` object
13
+ */
14
+ var __assign = (this && this.__assign) || function () {
15
+ __assign = Object.assign || function(t) {
16
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
17
+ s = arguments[i];
18
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
19
+ t[p] = s[p];
20
+ }
21
+ return t;
22
+ };
23
+ return __assign.apply(this, arguments);
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.generateShoppingShippingPolicySchema = void 0;
27
+ var isValidCurrency = function (code) {
28
+ if (!code)
29
+ return false;
30
+ return /^[A-Z]{3}$/.test(code);
31
+ };
32
+ var normalizeDestination = function (val) {
33
+ if (!val)
34
+ return undefined;
35
+ var s = String(val);
36
+ if (s.startsWith('http'))
37
+ return s;
38
+ return s;
39
+ };
40
+ var generateShoppingShippingPolicySchema = function (_a) {
41
+ var name = _a.name, provider = _a.provider, description = _a.description, shippingDestination = _a.shippingDestination, transitTimeMinDays = _a.transitTimeMinDays, transitTimeMaxDays = _a.transitTimeMaxDays, shippingRate = _a.shippingRate, shippingSettingsLink = _a.shippingSettingsLink, freeShippingThreshold = _a.freeShippingThreshold, _b = _a.skipInvalidCurrency, skipInvalidCurrency = _b === void 0 ? false : _b;
42
+ var schema = {
43
+ '@context': 'https://schema.org',
44
+ '@type': 'ShippingService',
45
+ name: name,
46
+ };
47
+ if (provider)
48
+ schema.provider = __assign({ '@type': 'Organization', name: provider.name }, (provider.url && { url: provider.url }));
49
+ if (description)
50
+ schema.description = description;
51
+ var dest = normalizeDestination(shippingDestination);
52
+ if (dest)
53
+ schema.shippingDestination = dest;
54
+ if (transitTimeMinDays != null || transitTimeMaxDays != null) {
55
+ schema.shippingDeliveryTime = __assign(__assign(__assign({ '@type': 'ShippingDeliveryTime' }, (transitTimeMinDays != null && {
56
+ transitTimeLabel: "".concat(transitTimeMinDays),
57
+ })), (transitTimeMinDays != null && { transitTimeMin: transitTimeMinDays })), (transitTimeMaxDays != null && { transitTimeMax: transitTimeMaxDays }));
58
+ }
59
+ if (shippingRate) {
60
+ if (shippingRate.currency && !isValidCurrency(shippingRate.currency)) {
61
+ if (!skipInvalidCurrency)
62
+ throw new Error("Invalid currency code: ".concat(shippingRate.currency));
63
+ }
64
+ else {
65
+ schema.shippingRate = __assign(__assign({ '@type': 'MonetaryAmount', value: shippingRate.price }, (shippingRate.currency && { currency: shippingRate.currency })), (shippingRate.shippingLabel && { name: shippingRate.shippingLabel }));
66
+ }
67
+ }
68
+ if (shippingSettingsLink)
69
+ schema.shippingSettingsLink = shippingSettingsLink;
70
+ if (freeShippingThreshold != null)
71
+ schema.freeShippingThreshold = freeShippingThreshold;
72
+ return schema;
73
+ };
74
+ exports.generateShoppingShippingPolicySchema = generateShoppingShippingPolicySchema;
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Generate schema.org `ItemList` JSON-LD for a shopping overview (Products).
3
+ *
4
+ * The function emits an `ItemList` where each `ListItem.item` is a
5
+ * `Product` object with optional `offers` and `aggregateRating`. Use
6
+ * `skipInvalidCurrency` to ignore invalid currency codes in offers.
7
+ *
8
+ * See `ShoppingOverviewOptions` and `ProductItem` for accepted fields.
9
+ * @returns A JSON-LD compatible `ItemList` of Product objects
10
+ */
11
+ interface BrandRef {
12
+ name: string;
13
+ url?: string;
14
+ }
15
+ interface ProductOffer {
16
+ price?: string | number;
17
+ priceCurrency?: string;
18
+ availability?: string;
19
+ url?: string;
20
+ }
21
+ interface ProductItem {
22
+ id?: string;
23
+ name: string;
24
+ url?: string;
25
+ image?: string | {
26
+ url: string;
27
+ width?: number;
28
+ height?: number;
29
+ };
30
+ description?: string;
31
+ brand?: string | BrandRef;
32
+ sku?: string;
33
+ aggregateRating?: {
34
+ ratingValue: number | string;
35
+ reviewCount?: number;
36
+ };
37
+ offers?: ProductOffer | ProductOffer[];
38
+ priceRange?: {
39
+ low?: string | number;
40
+ high?: string | number;
41
+ };
42
+ category?: string | string[];
43
+ }
44
+ interface ShoppingOverviewOptions {
45
+ name?: string;
46
+ url?: string;
47
+ products: ProductItem[];
48
+ skipInvalidCurrency?: boolean;
49
+ }
50
+ export declare const generateShoppingOverviewSchema: ({ name, url, products, skipInvalidCurrency, }: ShoppingOverviewOptions) => any;
51
+ export {};
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ /**
3
+ * Generate schema.org `ItemList` JSON-LD for a shopping overview (Products).
4
+ *
5
+ * The function emits an `ItemList` where each `ListItem.item` is a
6
+ * `Product` object with optional `offers` and `aggregateRating`. Use
7
+ * `skipInvalidCurrency` to ignore invalid currency codes in offers.
8
+ *
9
+ * See `ShoppingOverviewOptions` and `ProductItem` for accepted fields.
10
+ * @returns A JSON-LD compatible `ItemList` of Product objects
11
+ */
12
+ var __assign = (this && this.__assign) || function () {
13
+ __assign = Object.assign || function(t) {
14
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
15
+ s = arguments[i];
16
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
17
+ t[p] = s[p];
18
+ }
19
+ return t;
20
+ };
21
+ return __assign.apply(this, arguments);
22
+ };
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.generateShoppingOverviewSchema = void 0;
25
+ var formatImage = function (image) {
26
+ if (!image)
27
+ return undefined;
28
+ if (typeof image === 'string')
29
+ return image;
30
+ return __assign(__assign({ '@type': 'ImageObject', url: image.url }, (image.width && { width: image.width })), (image.height && { height: image.height }));
31
+ };
32
+ var formatBrand = function (b) {
33
+ if (!b)
34
+ return undefined;
35
+ if (typeof b === 'string')
36
+ return { '@type': 'Brand', name: b };
37
+ return __assign({ '@type': 'Brand', name: b.name }, (b.url && { url: b.url }));
38
+ };
39
+ var isValidCurrency = function (code) {
40
+ if (!code)
41
+ return false;
42
+ return /^[A-Z]{3}$/.test(code);
43
+ };
44
+ var AVAILABILITIES = [
45
+ 'InStock',
46
+ 'OutOfStock',
47
+ 'PreOrder',
48
+ 'PreSale',
49
+ 'Discontinued',
50
+ 'OnlineOnly',
51
+ 'InStoreOnly',
52
+ 'LimitedAvailability',
53
+ 'BackOrder',
54
+ ];
55
+ var normalizeAvailability = function (val) {
56
+ if (!val)
57
+ return undefined;
58
+ try {
59
+ var asUrl = String(val);
60
+ if (asUrl.startsWith('http')) {
61
+ var token = asUrl.replace(/https?:\/\/schema\.org\//i, '');
62
+ if (AVAILABILITIES.includes(token))
63
+ return "https://schema.org/".concat(token);
64
+ return undefined;
65
+ }
66
+ if (AVAILABILITIES.includes(asUrl))
67
+ return "https://schema.org/".concat(asUrl);
68
+ return undefined;
69
+ }
70
+ catch (_a) {
71
+ return undefined;
72
+ }
73
+ };
74
+ var generateShoppingOverviewSchema = function (_a) {
75
+ var name = _a.name, url = _a.url, products = _a.products, _b = _a.skipInvalidCurrency, skipInvalidCurrency = _b === void 0 ? false : _b;
76
+ var itemList = products.map(function (p, i) {
77
+ var _a;
78
+ var product = {
79
+ '@type': 'Product',
80
+ name: p.name,
81
+ };
82
+ if (p.id)
83
+ product['@id'] = p.id;
84
+ if (p.url)
85
+ product.url = p.url;
86
+ var img = formatImage(p.image);
87
+ if (img)
88
+ product.image = img;
89
+ if (p.description)
90
+ product.description = p.description;
91
+ var brand = formatBrand(p.brand);
92
+ if (brand)
93
+ product.brand = brand;
94
+ if (p.sku)
95
+ product.sku = p.sku;
96
+ if (p.category)
97
+ product.category = Array.isArray(p.category)
98
+ ? p.category.join(', ')
99
+ : p.category;
100
+ if (p.aggregateRating) {
101
+ product.aggregateRating = __assign({ '@type': 'AggregateRating', ratingValue: p.aggregateRating.ratingValue }, (p.aggregateRating.reviewCount != null && {
102
+ reviewCount: p.aggregateRating.reviewCount,
103
+ }));
104
+ }
105
+ if (p.priceRange) {
106
+ product.priceRange =
107
+ "".concat((_a = p.priceRange.low) !== null && _a !== void 0 ? _a : '').concat(p.priceRange.low != null && p.priceRange.high != null ? " - ".concat(p.priceRange.high) : '').trim();
108
+ }
109
+ if (p.offers) {
110
+ var offs = Array.isArray(p.offers) ? p.offers : [p.offers];
111
+ var mapped = offs
112
+ .map(function (o) {
113
+ if (o.priceCurrency && !isValidCurrency(o.priceCurrency))
114
+ return { __invalidCurrency: o.priceCurrency };
115
+ var of = { '@type': 'Offer' };
116
+ if (o.price != null)
117
+ of.price = o.price;
118
+ if (o.priceCurrency)
119
+ of.priceCurrency = o.priceCurrency;
120
+ if (o.availability) {
121
+ var n = normalizeAvailability(o.availability);
122
+ if (n)
123
+ of.availability = n;
124
+ }
125
+ if (o.url)
126
+ of.url = o.url;
127
+ return of;
128
+ })
129
+ .filter(function (x) { return x && !('__invalidCurrency' in x); });
130
+ var hadInvalid = offs.some(function (o) { return o.priceCurrency && !isValidCurrency(o.priceCurrency); });
131
+ if (hadInvalid && !skipInvalidCurrency) {
132
+ var bad = offs.find(function (o) { return o.priceCurrency && !isValidCurrency(o.priceCurrency); });
133
+ throw new Error("Invalid currency code: ".concat(bad === null || bad === void 0 ? void 0 : bad.priceCurrency));
134
+ }
135
+ if (mapped.length === 1)
136
+ product.offers = mapped[0];
137
+ else if (mapped.length > 1)
138
+ product.offers = mapped;
139
+ }
140
+ return {
141
+ '@type': 'ListItem',
142
+ position: i + 1,
143
+ item: product,
144
+ };
145
+ });
146
+ var schema = {
147
+ '@context': 'https://schema.org',
148
+ '@type': 'ItemList',
149
+ itemListElement: itemList,
150
+ };
151
+ if (name)
152
+ schema.name = name;
153
+ if (url)
154
+ schema.mainEntityOfPage = { '@type': 'WebPage', '@id': url };
155
+ return schema;
156
+ };
157
+ exports.generateShoppingOverviewSchema = generateShoppingOverviewSchema;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Create schema.org structured data for a single `Product` with optional
3
+ * `Review` and `Offer` entries.
4
+ *
5
+ * Supports brand and seller references, image normalization, review
6
+ * formatting and simple availability/price handling. Use
7
+ * `skipInvalidCurrency` to avoid throwing on bad currency codes.
8
+ *
9
+ * See `ProductSnippetOptions` for accepted fields.
10
+ * @returns A JSON-LD compatible `Product` object
11
+ */
12
+ interface BrandRef {
13
+ name: string;
14
+ url?: string;
15
+ }
16
+ interface SellerRef {
17
+ name?: string;
18
+ url?: string;
19
+ }
20
+ interface ReviewRating {
21
+ ratingValue: number | string;
22
+ bestRating?: number | string;
23
+ }
24
+ interface ReviewItem {
25
+ author?: string | {
26
+ name: string;
27
+ };
28
+ datePublished?: string;
29
+ reviewBody?: string;
30
+ reviewRating?: ReviewRating;
31
+ }
32
+ interface OfferItem {
33
+ price?: string | number;
34
+ priceCurrency?: string;
35
+ availability?: string;
36
+ url?: string;
37
+ seller?: SellerRef;
38
+ priceValidUntil?: string;
39
+ itemCondition?: string;
40
+ }
41
+ interface ProductSnippetOptions {
42
+ name: string;
43
+ description?: string;
44
+ image?: string | {
45
+ url: string;
46
+ width?: number;
47
+ height?: number;
48
+ };
49
+ sku?: string;
50
+ brand?: string | BrandRef;
51
+ reviews?: ReviewItem[];
52
+ offers?: OfferItem | OfferItem[];
53
+ skipInvalidCurrency?: boolean;
54
+ }
55
+ export declare const generateShoppingProductSnippetSchema: ({ name, description, image, sku, brand, reviews, offers, skipInvalidCurrency, }: ProductSnippetOptions) => any;
56
+ export {};