# All files / src/SuperSelect SuperSelect.utils.ts

100% Statements 89/89
100% Branches 37/37
100% Functions 19/19
100% Lines 82/82

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
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    2103x

    3x
    174x

    174x

    3x
    3x
    29x
    29x

    29x
    90x
    90x

    90x
    21x
    69x
    49x
    20x
    2x

    18x

    3x
    3x
    36x
    36x
    36x
    36x

    36x
    223x
    76x

    115x

    76x

    147x

    3x

    3x

    50x
    168x
    168x

    168x
    168x

    168x

    168x
    168x

    168x

    50x
    20x

    76x

    30x

    14x
    30x
    30x

    30x
    10x

    4x

    3x

    7x
    7x
    7x

    7x
    7x

    7x

    76x

    3x
    228x

    3x

    414x

    3x

    71x
    13x

    58x
    21x

    37x

    3x
    52x

    3x

    73x
    20x

    53x

    53x

    3x
    4x
    35x
    35x

    35x

    35x

     import { isOptgroupSymbol } from './SuperSelect.constants'
    import {
      SuperSelectConfig,
      SuperSelectOptgroup,
      SuperSelectOption,
    } from './SuperSelect.types'

    export const getGroupNameIsValid = (groupName: unknown) => {
      const isGroupDefined = ![undefined, null, ''].includes(groupName as string)

      return isGroupDefined && typeof groupName === 'string' && groupName.length > 0
    }

    export const getOptionsSortingAlgorithm =
      ({
        getOptionIsBatch,
        getOptionLabel,
      }: {
        getOptionIsBatch: NonNullable<SuperSelectConfig['getOptionIsBatch']>
        getOptionLabel: NonNullable<SuperSelectConfig['getOptionLabel']>
      }) =>
      (a: SuperSelectOption, b: SuperSelectOption) => {
        const isOptionBatch1 = getOptionIsBatch(a)
        const isOptionBatch2 = getOptionIsBatch(b)

        // move batched options up
        if (isOptionBatch1 && isOptionBatch2) {
          return compareStrings(getOptionLabel(a), getOptionLabel(b))
        } else if (isOptionBatch1) {
          return -1
        } else if (isOptionBatch2) {
          return 1
        }

        return compareStrings(getOptionLabel(a), getOptionLabel(b))
      }

    export const getBatchOptionFormatter =
      ({
        value,
        multiple,
        getOptionIsBatch,
        getOptionValue,
      }: {
        value: SuperSelectConfig['value']
        multiple: NonNullable<SuperSelectConfig['multiple']>
        getOptionIsBatch: NonNullable<SuperSelectConfig['getOptionIsBatch']>
        getOptionValue: NonNullable<SuperSelectConfig['getOptionValue']>
      }) =>
      (option: SuperSelectOption) => {
        if (isMultiple(multiple, value) && getOptionIsBatch(option)) {
          const val = getOptionValue(option)

          const disabled = Array.isArray(val) && val.every((v) => value.includes(v))

          return {
            ...option,
            disabled,
          }
        }
        return option
      }

    export const RESERVED_UNGROUPED_OPTGROUP_NAME =
      'RESERVED_UNGROUPED_OPTGROUP_NAME'

    export const collectGroupsInOrderOfOccurrence = (
      opts: SuperSelectOption[],
      getOptionGroup: NonNullable<SuperSelectConfig['getOptionGroup']>
    ): {
      groups: string[]
      groupedOptions: Record<string, SuperSelectOption[]>
    } => {
      const output: {
        groups: string[]
        groupedOptions: Record<string, SuperSelectOption[]>
      } = opts.reduce(
        ({ groups, groupedOptions }, option) => {
          const groupName = getOptionGroup(option)

          // if group name is invalid, consider the option ungrouped
          const isGroupNameValid = getGroupNameIsValid(groupName)
          const adjustedGroupName = isGroupNameValid
            ? groupName
            : RESERVED_UNGROUPED_OPTGROUP_NAME

          const groupOptions = groupedOptions[adjustedGroupName] ?? []

          // push/assign for performance considerations
          groupOptions.push(option)
          groupedOptions[adjustedGroupName] = groupOptions

          return {
            groups: groups.includes(adjustedGroupName)
              ? groups
              : [...groups, adjustedGroupName],
            groupedOptions,
          }
        },
        {
          groups: [],
          groupedOptions: {},
        }
      )

      // if ungrouped options were found, lift them to the top
      if (output.groups.includes(RESERVED_UNGROUPED_OPTGROUP_NAME)) {
        return {
          groupedOptions: output.groupedOptions,
          groups: [
            RESERVED_UNGROUPED_OPTGROUP_NAME,
            ...output.groups.filter(
              (group) => group !== RESERVED_UNGROUPED_OPTGROUP_NAME
            ),
          ],
        }
      }

      return output
    }

    function findParentOptgroup(list: SuperSelectOption[], index: number) {
      for (let i = index; i >= 0; i--) {
        const option = list[i]
        const isOptgroup = getOptionIsOptgroup(option)

        if (isOptgroup) {
          return option.id
        }
      }
      // no parent optgroup was found, so the option is considered ungrouped
      return RESERVED_UNGROUPED_OPTGROUP_NAME
    }

    export function reorder(
      options: SuperSelectOption[],
      startIndex: number,
      endIndex: number
    ) {
      const nextOptions = [...options]
      const [removed] = nextOptions.splice(startIndex, 1)
      nextOptions.splice(endIndex, 0, removed)

      const prevGroup = findParentOptgroup(options, startIndex)
      const nextGroup = findParentOptgroup(nextOptions, endIndex)

      return { nextOptions, prevGroup, nextGroup }
    }

    function compareStrings(a: string, b: string) {
      return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' })
    }

    export function isMultiple<T>(multiple: boolean, value: T | T[]): value is T[] {
      return multiple && Array.isArray(value)
    }

    export const getOptionIsOptgroup = (
      item: SuperSelectOption
    ): item is SuperSelectOptgroup => {
      return Boolean(item[isOptgroupSymbol])
    }

    export const sortOptgroups = (groupA: string, groupB: string) => {
      // always put ungrouped options on top
      if (groupA === RESERVED_UNGROUPED_OPTGROUP_NAME) {
        return -1
      }
      if (groupB === RESERVED_UNGROUPED_OPTGROUP_NAME) {
        return 1
      }
      return compareStrings(groupA, groupB)
    }

    export function getIsAllOptionsUngrouped(groups: string[]) {
      return groups.length === 1 && groups[0] === RESERVED_UNGROUPED_OPTGROUP_NAME
    }

    export function createOptgroup(
      groupName: string,
      groupOptions: SuperSelectOption[]
    ) {
      // ungrouped options should not have a group header
      if (groupName === RESERVED_UNGROUPED_OPTGROUP_NAME) {
        return groupOptions
      }

      const optgroupHeader: SuperSelectOptgroup = {
        [isOptgroupSymbol]: true,
        id: groupName,
        label: groupName,
      }

      return [optgroupHeader, ...groupOptions]
    }

    export function removeEmptyOptGroups(options: SuperSelectOption[]) {
      return options.filter((opt, index, arr) => {
        const currentElementIsLast = index === arr.length - 1
        const currentElementIsOptgroup = getOptionIsOptgroup(opt)
        const nextElementIsOptgroup =
          !currentElementIsLast && getOptionIsOptgroup(arr[index + 1])

        return !(
          currentElementIsOptgroup &&
          (nextElementIsOptgroup || currentElementIsLast)
        )
      })
    }