1// copied from https://github.com/react-native-community/cli/blob/48136adfb814d335e957e22129d049c4a05c8759/packages/cli/src/commands/bundle/filterPlatformAssetScales.ts
2
3const ALLOWED_SCALES = {
4  ios: [1, 2, 3],
5};
6
7function filterPlatformAssetScales(platform, scales) {
8  const whitelist = ALLOWED_SCALES[platform];
9  if (!whitelist) {
10    return scales;
11  }
12  const result = scales.filter(scale => whitelist.indexOf(scale) > -1);
13  if (result.length === 0 && scales.length > 0) {
14    // No matching scale found, but there are some available. Ideally we don't
15    // want to be in this situation and should throw, but for now as a fallback
16    // let's just use the closest larger image
17    const maxScale = whitelist[whitelist.length - 1];
18    for (const scale of scales) {
19      if (scale > maxScale) {
20        result.push(scale);
21        break;
22      }
23    }
24
25    // There is no larger scales available, use the largest we have
26    if (result.length === 0) {
27      result.push(scales[scales.length - 1]);
28    }
29  }
30  return result;
31}
32
33module.exports = filterPlatformAssetScales;
34