# All files / extensions parseFlex.js

0% Statements 0/24
0% Branches 0/16
0% Functions 0/3
0% Lines 0/23

    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

     import { css } from 'styled-components';

    const parseFlexChar = char => {
      if (char === '0') {
        return 'flex: 0 0 auto !important;';
      }

      return `flex: ${char} 0 0 !important;`;
    };

    export default flex => {
      let itemIdx = 0;
      return flex
        .split('')
        .reduce((lines, char, idx) => {
          if (char === ' ') {
            return lines;
          }

          if (flex[idx - 1] === ' ' && flex[idx + 1] === ' ') {
            lines.push(`& > *:nth-child(${itemIdx + 1}) {
            margin-left: auto !important;
            margin-right: auto !important;
            ${parseFlexChar(char)}
          }`);
          } else if (flex[idx - 1] === ' ') {
            lines.push(`& > *:nth-child(${itemIdx + 1}) {
            margin-left: auto !important;
            margin-right: 15px !important;
            ${parseFlexChar(char)}
          }`);
          } else if (flex[idx + 1] === ' ') {
            lines.push(`& > *:nth-child(${itemIdx + 1}) {
            margin-right: auto !important;
            margin-left: 15px !important;
            ${parseFlexChar(char)}
          }`);
          } else {
            lines.push(`& > *:nth-child(${itemIdx + 1}) {
            margin-right: 15px !important;
            margin-left: 15px !important;
            ${parseFlexChar(char)}
          }`);
          }

          if (idx === 0) {
            lines.push(
              `& > *:nth-child(${itemIdx + 1}) { margin-left: 0 !important; }`,
            );
          }

          if (idx === flex.length - 1) {
            lines.push(
              `& > *:nth-child(${itemIdx + 1}) { margin-right: 0 !important; }`,
            );
          }

          itemIdx = itemIdx + 1;
          return lines;
        }, [])
        .join('\n');
    };