These are chat archives for airbnb/javascript

3rd
Dec 2016
Shaun Stanislaus
@shaunstanislaus
Dec 03 2016 16:05
guys, i have some queries. wondering why the import doesn't work with babel example.js | node https://gist.github.com/shaunstanislaus/6b46d0e120a29fb4643588df930e4efa#file-output-sh-L5
Shaun Stanislaus
@shaunstanislaus
Dec 03 2016 16:10
anyone there to help me out?
shiyuwudi
@shiyuwudi
Dec 03 2016 16:13
have you apply babel core and preset?
Shaun Stanislaus
@shaunstanislaus
Dec 03 2016 16:16
@shiyuwudi : apply babel core as in npm i -g babel-core ?
@shiyuwudi ?
shiyuwudi
@shiyuwudi
Dec 03 2016 16:19
@shaunstanislaus npm install babel-core babel-preset-es2015 --save-dev
@shaunstanislaus run above at the same lvl with your package.json
Shaun Stanislaus
@shaunstanislaus
Dec 03 2016 16:21
@shiyuwudi : thanks it work. so i need to install babel-core & babel-preset-es2015 in order to use import function?
shiyuwudi
@shiyuwudi
Dec 03 2016 16:23
@shaunstanislaus i think so if you insist use es6 syntax import, or you may use var fs = require('fs'), a legacy one
Shaun Stanislaus
@shaunstanislaus
Dec 03 2016 16:36
@shiyuwudi , how come my output are not being split and seperated with commas?
shiyuwudi
@shiyuwudi
Dec 03 2016 16:54
@shaunstanislaus try replace things in map with "line=>line.split(' ').join(',')"
@shaunstanislaus if you use two spaces to divide a word, split two spaces
Shaun Stanislaus
@shaunstanislaus
Dec 03 2016 17:00
@shiyuwudi
shaunstanislaus@Master:~/workspace/javascript_test
% babel example.js | node                                                                                         16-12-04 - 0:59:45
SyntaxError: example.js: Unexpected token, expected , (8:0)
  6 |   .map(line=>line.split(' ').join(',')
  7 |
> 8 | console.log('output', output)
    | ^
  9 |
@shiyuwudi
import fs from 'fs'

var output = fs.readFileSync('data.txt', 'utf8')
  .trim()
  .split('\n')
  .map(line=>line.split(' ').join(',')

console.log('output', output)
shiyuwudi
@shiyuwudi
Dec 03 2016 17:02
check if you miss a bracket after join(',')
Shaun Stanislaus
@shaunstanislaus
Dec 03 2016 17:03
oh ya
lol
@shiyuwudi thank you!

@shiyuwudi
but the output is

output [ 'mark,johansson,blender,200,1']

how do you make it

output [ 'mark johansson', 'blender' , '200' , '1']
shiyuwudi
@shiyuwudi
Dec 03 2016 17:06
@shaunstanislaus try split with two spaces, and make sure your original text the same format
Shaun Stanislaus
@shaunstanislaus
Dec 03 2016 17:14

@shiyuwudi

shaunstanislaus@Master:~/workspace/javascript_test
% babel example.js | node                                                                                         16-12-04 - 1:11:51
output [ 'mark johansson, waffle iron, 80, 2',
  'mark johansson, blender, 200, 1',
  'mark johansson, knife, 10, 4',
  'Nikita Smith, waffle iron, 80, 1',
  'Nikita Smith, knife, 10, 2',
  'Nikita Smith, pot, 20, 3' ]
.map(line=>line.split('  ').join(', '))

but how do i make each string with ' ' so that the output would be
['name', 'object', 'value', 'value' ]
currently its [name, object, value, value ]

shiyuwudi
@shiyuwudi
Dec 03 2016 17:21
@shaunstanislaus line=>line.split(' ').map(word=>\'${word}\').join(',')
shiyuwudi
@shiyuwudi
Dec 03 2016 17:26
blahblah.map(line=>line.split('  ').map(word=>`\'${word}\'`).join(','))
Shaun Stanislaus
@shaunstanislaus
Dec 03 2016 17:38
@shiyuwudi guess what, i copy rewrote the data in excel and paste it into data.txt and poof! it has the ' ' automatically with right tabs i guess. what are your settings for your tabs in your editor? are you using atom or sublime?