File size: 1,244 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { includes, get } from 'lodash';
import { AspectRatios, AspectRatiosValues } from 'calypso/state/editor/image-editor/constants';

/**
 * Returns the default aspect ratio image editor should use.
 *
 * If only aspectRatio is specified and is valid, we return it. If only aspectRatios is specified and is valid,
 * we return its first item. If both aspectRatio and aspectRatios are specified, we return either:
 * 1. aspectRatio, if it is included in aspectRatios
 * 2. aspectRatios[ 0 ] if aspectRatio is not included in aspectRatios
 *
 * We return AspectRatios.FREE if no specified aspect ratio is valid.
 * @param   {string} aspectRatio  an aspect ratio which should be validated and used as default one for image editor
 * @param   {Array}  aspectRatios list of aspect ratios to be validated and used in image editor
 * @returns {string}              the default valid aspect ratio image editor should use
 */
export function getDefaultAspectRatio( aspectRatio = null, aspectRatios = AspectRatiosValues ) {
	if ( ! includes( aspectRatios, aspectRatio ) ) {
		aspectRatio = get( aspectRatios, '0', AspectRatios.FREE );
	}

	return includes( AspectRatiosValues, aspectRatio )
		? aspectRatio
		: getDefaultAspectRatio( aspectRatio );
}