summaryrefslogtreecommitdiff
path: root/node_modules/@jet-app/app-store/tmp/src/foundation/media/attributes.js
blob: 992a56ef9582a3cc20e1a7e8275d270eb2554cef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { isNothing } from "@jet/environment/types/optional";
import * as serverData from "../json-parsing/server-data";
// region Generic Attribute retrieval
// region Attribute retrieval
/**
 * Retrieve the specified attribute from the data, coercing it to a JSONData dictionary
 *
 * @param data The data from which to retrieve the attribute.
 * @param attributePath The path of the attribute.
 * @param defaultValue The object to return if the path search fails.
 * @returns The dictionary of data
 */
export function attributeAsDictionary(data, attributePath, defaultValue) {
    if (isNothing(data)) {
        return null;
    }
    return serverData.asDictionary(data.attributes, attributePath, defaultValue);
}
/**
 * Retrieve the specified attribute from the data, coercing it to an Interface
 *
 * @param data The data from which to retrieve the attribute.
 * @param attributePath The path of the attribute.
 * @param defaultValue The object to return if the path search fails.
 * @returns The dictionary of data as an interface
 */
export function attributeAsInterface(data, attributePath, defaultValue) {
    return attributeAsDictionary(data, attributePath, defaultValue);
}
/**
 * Retrieve the specified attribute from the data as an array, coercing to a JSONValue array
 *
 * @param data The data from which to retrieve the attribute.
 * @param attributePath The path of the attribute.
 * @returns {any[]} The attribute value as an array.
 */
export function attributeAsArray(data, attributePath) {
    if (serverData.isNull(data)) {
        return [];
    }
    return serverData.asArray(data.attributes, attributePath);
}
/**
 * Retrieve the specified attribute from the data as an array, coercing to an empty array if the object is not an array.
 *
 * @param data The data from which to retrieve the attribute.
 * @param attributePath The path of the attribute.
 * @returns {any[]} The attribute value as an array.
 */
export function attributeAsArrayOrEmpty(data, attributePath) {
    var _a;
    return (_a = attributeAsArray(data, attributePath)) !== null && _a !== void 0 ? _a : [];
}
/**
 * Retrieve the specified attribute from the data as a string.
 *
 * @param data The data from which to retrieve the attribute.
 * @param attributePath The object path for the attribute.
 * @param policy The validation policy to use when resolving this value.
 * @returns {string} The attribute value as a string.
 */
export function attributeAsString(data, attributePath, policy = "coercible") {
    if (serverData.isNull(data)) {
        return null;
    }
    return serverData.asString(data.attributes, attributePath, policy);
}
/**
 * Retrieve the specified attribute from the data as a boolean.
 *
 * @param data The data from which to retrieve the attribute.
 * @param attributePath The path of the attribute.
 * @param policy The validation policy to use when resolving this value.
 * @returns {boolean} The attribute value as a boolean.
 */
export function attributeAsBoolean(data, attributePath, policy = "coercible") {
    if (serverData.isNull(data)) {
        return null;
    }
    return serverData.asBoolean(data.attributes, attributePath, policy);
}
/**
 * Retrieve the specified attribute from the data as a boolean, which will be `false` if the attribute does not exist.
 *
 * @param data The data from which to retrieve the attribute.
 * @param attributePath The path of the attribute.
 * @returns {boolean} The attribute value as a boolean, coercing to `false` if the value is not present..
 */
export function attributeAsBooleanOrFalse(data, attributePath) {
    if (serverData.isNull(data)) {
        return false;
    }
    return serverData.asBooleanOrFalse(data.attributes, attributePath);
}
/**
 * Retrieve the specified attribute from the data as a number.
 *
 * @param data The data from which to retrieve the attribute.
 * @param attributePath The path of the attribute.
 * @param policy The validation policy to use when resolving this value.
 * @returns {boolean} The attribute value as a number.
 */
export function attributeAsNumber(data, attributePath, policy = "coercible") {
    if (serverData.isNull(data)) {
        return null;
    }
    return serverData.asNumber(data.attributes, attributePath, policy);
}
export function hasAttributes(data) {
    return !serverData.isNull(serverData.asDictionary(data, "attributes"));
}
/**
 * The canonical way to detect if an item from Media API is hydrated or not.
 *
 * @param data The data from which to retrieve the attributes.
 */
export function isNotHydrated(data) {
    return !hasAttributes(data);
}
// region Custom Attributes
/**
 * Performs conversion for a custom variant of given attribute, if any are available.
 * @param attribute Attribute to get custom attribute key for, if any.
 */
export function attributeKeyAsCustomAttributeKey(attribute) {
    return customAttributeMapping[attribute];
}
/**
 * Whether or not given custom attributes key allows fallback to default page with AB testing treatment within a nondefault page.
 * This is to allow AB testing to affect only icons within custom product pages.
 */
export function attributeAllowsNonDefaultTreatmentInNonDefaultPage(customAttribute) {
    return customAttribute === "customArtwork" || customAttribute === "customIconArtwork"; // Only the icon artwork.
}
/**
 * Defines mapping of attribute to custom attribute.
 */
const customAttributeMapping = {
    artwork: "customArtwork",
    iconArtwork: "customIconArtwork",
    screenshotsByType: "customScreenshotsByType",
    promotionalText: "customPromotionalText",
    videoPreviewsByType: "customVideoPreviewsByType",
    customScreenshotsByTypeForAd: "customScreenshotsByTypeForAd",
    customVideoPreviewsByTypeForAd: "customVideoPreviewsByTypeForAd",
    customDeepLink: "customDeepLink",
};
// endregion
//# sourceMappingURL=attributes.js.map