# All files / src/helpers format-default.ts

100% Statements 26/26
100% Branches 17/17
100% Functions 5/5
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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

    48x

    48x
    379x

    379x
    379x
    11x

    379x
    379x
    27x

    379x
    379x
    23x

    379x
    4927x
    268x

    379x

    758x

    758x
    552x

    206x
    206x
    780x
    72x

    206x
    159x

    47x

     import { StyleSheet, ViewStyle } from 'react-native';
    import { MapPropsFunction } from './format';

    const propsToDisplay = [
      'testID',
      'nativeID',
      'accessibilityElementsHidden',
      'accessibilityViewIsModal',
      'importantForAccessibility',
      'accessibilityRole',
      'accessibilityLabel',
      'accessibilityLabelledBy',
      'accessibilityHint',
      'placeholder',
      'value',
      'defaultValue',
      'title',
    ];

    /**
     * Preserve props that are helpful in diagnosing test failures, while stripping rest
     */
    export const defaultMapProps: MapPropsFunction = (props) => {
      const result: Record<string, unknown> = {};

      const styles = StyleSheet.flatten(props.style as ViewStyle);
      if (styles?.display === 'none') {
        result.style = { display: 'none' };
      }

      const accessibilityState = removeUndefinedKeys(props.accessibilityState);
      if (accessibilityState !== undefined) {
        result.accessibilityState = accessibilityState;
      }

      const accessibilityValue = removeUndefinedKeys(props.accessibilityValue);
      if (accessibilityValue !== undefined) {
        result.accessibilityValue = accessibilityValue;
      }

      propsToDisplay.forEach((propName) => {
        if (propName in props) {
          result[propName] = props[propName];
        }
      });

      return result;
    };

    function isObject(value: unknown): value is Record<string, unknown> {
      return typeof value === 'object' && value !== null && !Array.isArray(value);
    }

    function removeUndefinedKeys(prop: unknown) {
      if (!isObject(prop)) {
        return prop;
      }

      const result: Record<string, unknown> = {};
      Object.entries(prop).forEach(([key, value]) => {
        if (value !== undefined) {
          result[key] = value;
        }
      });

      // If object does not have any props we will ignore it.
      if (Object.keys(result).length === 0) {
        return undefined;
      }

      return result;
    }