Guest User

Untitled

a guest
Nov 3rd, 2017
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. const findByFieldRegex = ( field ) => ( regex, list ) =>
  2. list.filter( obj => obj[ field ].match( regex ) )
  3.  
  4. const findBy = ( field ) => ({
  5. [ field ]: ( value ) => ({
  6. in: ( list ) => findByFieldRegex( field )( value, list )
  7. }),
  8. searchingFor: ( value ) => ({
  9. in: ( list ) => findByFieldRegex( field )( value, list )
  10. }),
  11. })
  12.  
  13.  
  14. const list = [
  15. { id: 1, name: 'Suissa', email: 'Suissa@gmail.com' },
  16. { id: 2, name: 'Jean', email: 'Jean@joaomail.com' },
  17. { id: 3, name: 'Carlo', email: 'Carlo@joaomail.com' },
  18. { id: 4, name: 'Nascimento', email: 'Nascimento@joaomail.com' },
  19. { id: 5, name: 'John', email: 'John@gmail.com' }
  20. ]
  21.  
  22. const gmail = /@gmail.com/gi
  23. const joaomail = /@joaomail.com/gi
  24.  
  25. console.log( 'gmail: ', findBy( 'email' ).email( gmail ).in( list ) )
  26. console.log( 'joaomail: ', findBy( 'email' ).searchingFor( joaomail ).in( list ) )
Add Comment
Please, Sign In to add comment