PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
jetpack / jetpack_vendor / automattic / jetpack-forms / src / modules / field-rating / helpers.js

helpers.js in Jetpack – WP Security, Backup, Speed, & Growth 16.3, at jetpack_vendor/automattic/jetpack-forms/src/modules/field-rating/helpers.js

46 lines 1.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Pure helper functions for rating field.
3 * Extracted to allow testing without Interactivity API dependencies.
4 */
5
6 /**
7 * Extract rating data from a field value.
8 *
9 * @param {object} value - The field value.
10 * @return {Object|null} Rating data object or null if not a rating field.
11 */
12 export function getRating( value ) {
13 if ( value?.type === 'rating' ) {
14 return {
15 rating: value.rating ?? 0,
16 maxRating: value.maxRating ?? 5,
17 iconStyle: value.iconStyle ?? 'stars',
18 };
19 }
20
21 return null;
22 }
23
24 /**
25 * Check if a value is a rating field value.
26 *
27 * @param {object} value - The field value.
28 * @return {boolean} True if this is a rating field value.
29 */
30 export function isRatingValue( value ) {
31 return value?.type === 'rating';
32 }
33
34 /**
35 * Get the display value for a rating field.
36 *
37 * @param {object} value - The field value.
38 * @return {string|null} The display value (e.g., "3/5") or null if not a rating.
39 */
40 export function getRatingDisplayValue( value ) {
41 if ( isRatingValue( value ) && value?.displayValue ) {
42 return value.displayValue;
43 }
44 return null;
45 }
46