Skip to content

Instantly share code, notes, and snippets.

@Xotabu4
Last active July 28, 2017 11:56
Show Gist options
  • Save Xotabu4/c129b147abb0085140bba1e9820afd17 to your computer and use it in GitHub Desktop.
Save Xotabu4/c129b147abb0085140bba1e9820afd17 to your computer and use it in GitHub Desktop.
import * as fs from 'fs'
class User {
constructor(public firstName: string,
public lastName: string,
public id: number) { }
toString() {
return `First Name: ${this.firstName}, Last Name: ${this.lastName}, id: ${this.id}`
}
}
export function parseUsersFromCSV(pathToFile: string): User[] {
let lines = fs.readFileSync(pathToFile, 'utf8').split('\n')
console.log('Got lines from csv: ', lines)
lines = lines.slice(1, lines.length) // removing header since not needed
console.log('Got lines from csv without header: ', lines)
var users: User[] = []
for (let line of lines) {
// FirstName,LastName,Id
let splitted = line.split(',')
users.push(new User(splitted[0], splitted[1], +splitted[2]))
}
return users
}
let users = parseUsersFromCSV('./table.csv')
for (let user of users) {
console.log(user.toString())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment