Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
chain-able-lego / src / fromPropsToLongestArray.ts
Size: Mime:
// @todo not sure if this is fine to import here or if we move this elsewhere
const { EMPTY_ARRAY, EMPTY_OBJ, isArray, isObj } = require('../../exotic')

if (process.env.NODE_ENV === 'development') {
  try {
    // exotic.EMPTY_ARRAY.isEmpty === true
    // exotic.EMPTY_ARRAY.isEmpty === true
    Object.defineProperty(Array.prototype, 'IS_EMPTY', {
      enumerable: false,
      configurable: false,
      get() {
        return this === EMPTY_ARRAY
      },
    })
    Object.defineProperty(Object.prototype, 'IS_EMPTY', {
      enumerable: false,
      configurable: false,
      get() {
        return this === EMPTY_OBJ
      },
    })
  } catch (notWritableException) {
    // ignore, dev
  }
}

/**
 * @param {Object} props
 * @return {Array<any>}
 */
const fromPropsToLongestArray = props => {
  // let best = ['empty']
  let best = []

  Object.keys(props)
    // should be able to sort based on common naming
    // should be able to do a more fancy reduce or sortby .length & .pop/find
    .forEach(key => {
      const list = props[key]
      if (isArray(list) === false) {
        return
      }

      if (list.length > best.length) {
        best = list
      }
    })

  return best
}
// @example
// const one = {
//   longest: [1],
//   shortest: [],
// }
// const two = {
//   longest: [1, 2, 3, 4],
//   shortest: exotic.EMPTY_ARRAY,
//   medium: [1, 2, 3]
// }

fromPropsToLongestArray.fromPropsToLongestArray = fromPropsToLongestArray
module.exports = fromPropsToLongestArray