# All files / utils orderBy.ts

78.13% Statements 75/96
54.02% Branches 47/87
73.33% Functions 11/15
78.95% Lines 75/95

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
    1891x
    1x
    1x
    1x

    1x
    2x

    2x
    2x
    8x

    2x

    1x

    1x
    15x
    12x
    12x
    12x
    12x

    12x
    12x
    12x
    12x

    12x

    12x

    7x

    5x

    5x

    3x

    1x
    12x
    12x
    12x
    12x
    12x

    12x
    15x

    15x
    15x
    15x
    12x
    12x

    1x
    2x

    1x

    1x

    1x
    2x

    2x

    2x
    2x
    2x

    2x
    8x

    2x

    1x
    2x
    2x
    3x

    3x

    2x
    2x

    2x

    2x
    8x
    12x
    8x

    4x

    8x

    2x
    12x

    1x

    2x

    2x
    1x

    2x

    2x

     import {get} from './get'
    import {isLength} from './isLength'
    import {isSymbol} from './isSymbol'
    import {isString} from './isString'

    const baseSortBy = (array, comparer) => {
      let {length} = array

      array.sort(comparer)
      while (length--) {
        array[length] = array[length].value
      }
      return array
    }

    const identity = value => value

    const compareAscending = (value, other) => {
      if (value !== other) {
        const valIsDefined = value !== undefined
        const valIsNull = value === null
        const valIsReflexive = value === value
        const valIsSymbol = isSymbol(value)

        const othIsDefined = other !== undefined
        const othIsNull = other === null
        const othIsReflexive = other === other
        const othIsSymbol = isSymbol(other)

        const val =
          typeof value === 'string' ? value.localeCompare(other) : value - other

        if (
          (!othIsNull && !othIsSymbol && !valIsSymbol && val > 0) ||
          (valIsSymbol &&
            othIsDefined &&
            othIsReflexive &&
            !othIsNull &&
            !othIsSymbol) ||
          (valIsNull && othIsDefined && othIsReflexive) ||
          (!valIsDefined && othIsReflexive) ||
          !valIsReflexive
        ) {
          return 1
        }
        Eif (
          (!valIsNull && !valIsSymbol && !othIsSymbol && val < 0) ||
          (othIsSymbol &&
            valIsDefined &&
            valIsReflexive &&
            !valIsNull &&
            !valIsSymbol) ||
          (othIsNull && valIsDefined && valIsReflexive) ||
          (!othIsDefined && valIsReflexive) ||
          !othIsReflexive
        ) {
          return -1
        }
      }
      return 0
    }

    const compareMultiple = (object, other, orders) => {
      let index = -1
      const objCriteria = object.criteria
      const othCriteria = other.criteria
      const length = objCriteria.length
      const ordersLength = orders.length

      while (++index < length) {
        const order = index < ordersLength ? orders[index] : null
        const cmpFn =
          order && typeof order === 'function' ? order : compareAscending
        const result = cmpFn(objCriteria[index], othCriteria[index])
        if (result) {
          Eif (order && typeof order !== 'function') {
            return result * (order == 'desc' ? -1 : 1)
          }
          return result
        }
      }
      return object.index - other.index
    }

    const isArrayLike = value => {
      return value != null && typeof value !== 'function' && isLength(value.length)
    }

    const baseFor = (object, iteratee, keysFunc) => {
      const iterable = Object(object)
      const props = keysFunc(object)
      let {length} = props
      let index = -1

      while (length--) {
        const key = props[++index]
        if (iteratee(iterable[key], key, iterable) === false) {
          break
        }
      }
      return object
    }

    const baseForOwn = (object, iteratee) => {
      return object && baseFor(object, iteratee, Object.keys)
    }

    const baseEach = (collection, iteratee) => {
      Iif (collection == null) {
        return collection
      }
      Iif (!isArrayLike(collection)) {
        return baseForOwn(collection, iteratee)
      }
      const length = collection.length
      const iterable = Object(collection)
      let index = -1

      while (++index < length) {
        Iif (iteratee(iterable[index], index, iterable) === false) {
          break
        }
      }
      return collection
    }

    const baseOrderBy = (collection, iteratees, orders) => {
      Eif (iteratees.length) {
        iteratees = iteratees.map(iteratee => {
          Iif (Array.isArray(iteratee)) {
            return value =>
              get(value, iteratee.length === 1 ? iteratee[0] : iteratee)
          }

          return iteratee
        })
      } else {
        iteratees = [identity]
      }

      let criteriaIndex = -1
      let eachIndex = -1

      const result = Array.isArray(collection) ? new Array(collection.length) : []

      baseEach(collection, value => {
        const criteria = iteratees.map(iteratee => {
          if (isString(iteratee)) {
            return typeof value === 'function' ? value(iteratee) : value[iteratee]
          }
          return iteratee(value)
        })

        result[++eachIndex] = {
          criteria,
          index: ++criteriaIndex,
          value,
        }
      })

      return baseSortBy(result, (object, other) =>
        compareMultiple(object, other, orders)
      )
    }

    type OrderByIteratees =
      | Function
      | Array<string>
      | Array<Function>
      | Array<Array<string>>
      | Array<object>

    export const orderBy = <X>(
      collection: Array<X> | object,
      iteratees: OrderByIteratees,
      orders: Array<string>
    ): Array<X> => {
      Iif (collection == null) {
        return []
      }
      if (!Array.isArray(iteratees)) {
        iteratees = iteratees == null ? [] : [iteratees]
      }
      Iif (!Array.isArray(orders)) {
        orders = orders == null ? [] : [orders]
      }
      return baseOrderBy(collection, iteratees, orders)
    }