How do you chain functions using lodash?

前端 未结 4 1598
伪装坚强ぢ
伪装坚强ぢ 2020-12-23 19:36

I have an object that looks like

var foundUser = {
    charData: []
}

which then I load an object from a database using mysql and then I ca

4条回答
  •  北海茫月
    2020-12-23 19:59

    Have you ever tried lodash/fp? It comes with all the same functions, but they are curried and none of them mutates the input.

    Because of that, you can compose them in a really nice way.

    Example:

    import moment from 'moment'
    import { compose, filter, groupBy, size, sortBy } from 'lodash/fp'
    
    const fromAdmin = filter({ type: 'admin' })
    const groupedByDate = groupBy(message => moment(message.createdAt).format('YYYYMMDD'))
    const sorted = sortBy('createdAt')
    const unreadByUser = filter({ isReadByUser: false })
    
    const groupedMessages = compose(
      groupedByDate,
      sorted,
      unreadByUser,
      fromAdmin,
    )(messages)
    

提交回复
热议问题