Guest User

Untitled

a guest
Oct 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. const fs = require( 'fs' )
  2.  
  3. const begin = '|'
  4. const removingThis = '|DT'
  5. const file = './input.txt'
  6.  
  7. const readFile = ( file ) => fs.readFileSync( file, { encoding: 'utf8' } )
  8. const splitingByLines = ( text ) => text.split( '\n' )
  9. const splitByColumn = ( text ) => text.split( '|' )
  10. const forGetColumns = ( line ) => line.startsWith( '|DT' )
  11.  
  12. const toColumns = line =>
  13. line.split( '|' )
  14. .map( col => col.trim() )
  15. .filter( col => col != '' )
  16.  
  17. const getHeader = ( text ) =>
  18. splitingByLines( text )
  19. .filter( forGetColumns )
  20. .map( toColumns )[ 0 ]
  21.  
  22. const fileOpen = readFile( file )
  23. const header = getHeader( fileOpen )
  24.  
  25. const toFormatedText = ( columns ) => ( obj, header, i ) =>
  26. Object.assign( obj, { [ header ]: columns[ i ] } )
  27.  
  28. const getColumns = ( line ) =>
  29. splitByColumn( line ).slice( 1 ).map( col => col.trim() )
  30.  
  31. const filterMyLinesWith = ( begin, removingThis ) => ( list ) =>
  32. list.filter( line => line.startsWith( begin ) && !line.startsWith( removingThis ) )
  33. .map( line => line.replace( '\r', '' ) )
  34. .map( line => header.reduce( toFormatedText( getColumns( line ) ), {} ) )
  35.  
  36. const getLinesWith = ( begin, removingThis ) => ( file ) =>
  37. filterMyLinesWith( begin, removingThis )
  38. ( splitingByLines( readFile( file ) ) )
  39.  
  40. const result = getLinesWith( begin, removingThis )( file )
  41.  
  42. console.log( result )
Add Comment
Please, Sign In to add comment